/[drupal]/contributions/modules/autonode/autonode.module
ViewVC logotype

Contents of /contributions/modules/autonode/autonode.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Apr 26 17:04:32 2007 UTC (2 years, 7 months ago) by snufkin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +40 -25 lines
File MIME type: text/x-php
Validation moved to hook_field, now settings page works. autonode_create_node() cleanup.
1 <?php
2 // $Id: autonode.module,v 1.2 2007/04/26 14:11:39 snufkin Exp $
3
4 /**
5 * @file
6 * Defines a field type for autoreferencing one node from another.
7 * Autoreference: if typed node doesnt exist it will create one according
8 * to the settings
9 */
10
11 /**
12 * Implementation of hook_menu() based nodereference_menu
13 */
14 function autonode_menu($may_cache) {
15 $items = array();
16
17 if ($may_cache) {
18 $items[] = array('path' => 'autonode/autocomplete', 'title' => t('autonode reference autocomplete'),
19 'callback' => 'autonode_autocomplete', 'access' => user_access('access content'), 'type' => MENU_CALLBACK);
20 }
21
22 return $items;
23 }
24
25 /**
26 * Implementation of hook_field_info()
27 */
28 function autonode_field_info() {
29 return array(
30 'autonode' => array('label' => 'Autonode Reference'),
31 );
32 }
33
34
35 /**
36 * Implementation of hook_field_settings()
37 */
38 function autonode_field_settings($op, $field) {
39 switch ($op) {
40 case 'database columns':
41 $columns = array(
42 'code_types' => array(
43 'type' => 'varchar',
44 'not null' => TRUE,
45 'default' => "''",
46 'sortable' => TRUE,
47 ),
48 'nid' => array(
49 'type' => 'int',
50 'not null' => TRUE,
51 'default' => '0',
52 ),
53 );
54 $columns['code_types']['length'] = isset($field['maxlength']) ? $field['maxlength'] : 12; //TODO make this dependant on the type of code we have in
55 return $columns;
56
57 case 'form':
58 $form = array();
59 $form['referenceable_types'] = array(
60 '#type' => 'checkboxes',
61 '#title' => t('Content types that can be referenced'),
62 '#multiple' => TRUE,
63 '#required' => TRUE,
64 '#default_value' => isset($field['referenceable_types']) ? $field['referenceable_types'] : array(),
65 '#options' => node_get_types('names'),
66 );
67 $form['code_types']['#tree'] = TRUE;
68 $form['code_types']['airport'] = array(
69 '#collapsible' => TRUE,
70 '#type' => 'fieldset',
71 '#title' => t('Airport code types'),
72 '#description' => t('Select which airport code do you want to use for reference.'),
73 );
74 $form['code_types']['airport']['codes'] = array(
75 '#type' => 'radios',
76 '#required' => TRUE,
77 '#title' => t('Available code types'),
78 '#default_value' => $field['code_types']['airport']['codes'] ? $field['code_types']['airport']['codes'] : '',
79 '#options' => array(
80 'iata' => t('3 letter IATA code (BUD, WAW etc)'),
81 'icao' => t('4 letter ICAO code (LHBP, EPWA etc)'),
82 ),
83 );
84
85 return $form;
86 case 'save':
87 return array('referenceable_types', 'code_types', );
88 }
89 }
90
91 // hook_field
92
93 function autonode_field($op, &$node, $field, &$items, $teaser, $page) {
94 switch($op) {
95 case 'validate':
96 foreach ($items as $delta => $item) {
97 $error_field = $field['field_name'] .']['. $delta .'][node_name';
98 if($item['nid'] == -1) {
99 form_set_error($error_field, t('%name : this %code doesn\'t exist in the main database. Please contact the administrator so we can add it.', array('%name' => t($item['node_name']), '%code' => t($field['widget']['label']) ) ));
100 }
101 }
102 return;
103 case 'submit':
104 foreach($items as $delta => $item) {
105 if($nid == 0) { /* then we successfully assigned in widget/process */
106 autonode_create_node($item['node_name'], $setup, 1);
107 }
108 }
109 }
110 }
111
112
113 /**
114 * Implementation of hook_field_formatter_info().
115 */
116 function autonode_field_formatter_info() {
117 return array(
118 'default' => array(
119 'label' => 'Default',
120 'field types' => array('autonode'),
121 ),
122 'plain' => array(
123 'label' => 'Plain text',
124 'field types' => array('autonode'),
125 ),
126 );
127 }
128
129
130 /**
131 * Implementation of hook_field_formatter().
132 */
133 function autonode_field_formatter($field, $item, $formatter, $node) {
134 $text = '';
135 if (!empty($item['nid'])) {
136 $referenced_node = node_load($item['nid']);
137 if ($referenced_node) {
138 $text = l($referenced_node->title, 'node/'. $referenced_node->nid);
139 }
140 }
141
142 switch ($formatter) {
143 case 'plain':
144 return strip_tags($text);
145
146 default:
147 return $text;
148 }
149 }
150
151
152 // hook_widget_info
153
154 function autonode_widget_info () {
155 return array(
156 'airport_codes' => array(
157 'label' => t('Define by airport codes'),
158 'field types' => array('autonode', ),
159 /** note the space beween field and types
160 * otherwise content_admin.inc errors
161 */
162 ),
163 );
164 }
165
166 // hook_widget_settings
167 /**
168 * comment to self: widget_settings cant reach outside to check the whole field.
169 * $widget is going to look like :
170 * Array
171 * (
172 * [default_value] =>
173 * [default_value_php] =>
174 * [maxlength] => 12
175 * [type] => airport_codes
176 * [weight] => 0
177 * [label] => Airport Codefield
178 * [description] =>
179 * )
180 * Here we cant just grab a maxlength according to what type of widget we picked.
181 * this can be done in widget(), because then we receive the $field variable
182 */
183
184 /**
185 * Implementation of hook_widget
186 */
187
188 function autonode_widget($op, &$node, $field, &$items) {
189 switch($op) {
190 case 'prepare form values':
191 foreach ($items as $delta => $item) {
192 if (isset($items[$delta]['nid'])) { //empty() will be true for 0
193 $val = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $items[$delta]['nid']));
194 $items[$delta]['default node_name'] = !(isset($val)) || $val == 0 ? 'NAN' : $val;
195 }
196 }
197 break;
198
199 case 'form' :
200 $code = $field['code_types']['airport']['codes'];
201 switch($code) { // hardcoded widget length
202 case 'iata':
203 $length = 3;
204 break;
205 case 'icao':
206 $length = 4;
207 break;
208 default:
209 $length = 12;
210 break;
211 }
212
213 $form = array();
214 $form[$field['field_name']] = array('#tree' => TRUE); // This is important. delete it, and you're dead
215 $form[$field['field_name']][0]['node_name'] = array(
216 '#type' => 'textfield',
217 '#title' => t($field['widget']['label'] . ', ' . strtoupper($code) ),
218 '#autocomplete_path' => 'autonode/autocomplete/'.$field['field_name'],
219 '#default_value' => $items[0]['default node_name'],
220 '#maxlength' => $length,
221 '#size' => $length+1,
222 '#required' => $field['required'],
223 '#description' => t($field['widget']['description']),
224 );
225 return $form;
226 case 'validate':
227 /*foreach ($items as $delta => $item) {
228 $error_field = $field['field_name'] .']['. $delta .'][node_name';
229
230 if(!empty($item['node_name'])) {
231 if(autonode_create_node($item['node_name'], $setup, 0) == '-1') { // we pass what's in the field
232 form_set_error($error_field, t('%name : this %code doesn\'t exist in the main database. Please contact the administrator so we can add it.', array('%name' => t($item['node_name']), '%code' => t($field['widget']['label']) ) ));
233 }
234 }
235 }*/
236 return;
237 case 'process form values':
238 /* here we assign the nid to $items */
239 foreach ($items as $delta => $item) {
240 if (!empty($item['node_name'])) {
241 $nid = autonode_create_node($item['node_name'], $setup, 0);
242 }
243 if($nid != "") { /* if we succeeded in assigning a $nid above */
244 $items[$delta]['nid'] = $nid;
245 $items[$delta]['error_field'] = $field['field_name'] . '][' .$delta . '][node_name';
246 $items[$delta]['code_types'] = $field['code_types']['airport']['codes'];
247 }
248 //unset($items[$delta]['node_name']); // we need the representation
249 elseif($delta>0) {
250 unset($items[$delta]);
251 }
252 }
253 break;
254 }
255 }
256
257 function autonode_autocomplete($field_name, $string = '') {
258 $fields = content_fields();
259 $field = $fields[$field_name];
260 $matches = array();
261
262 foreach (_nodereference_potential_references( $field, TRUE, $string) as $row) {
263 $matches[$row->node_title] = _nodereference_item($field, $row, TRUE);
264 }
265 print drupal_to_js($matches);
266 exit();
267 }
268
269 /**
270 * This function handles the automatic node creation.
271 * $entry determines if it is a real entry, or just a test.
272 * creates a node using $title as unique identifier in the reference database
273 *
274 * if $entry is true,
275 * return -1: means the referred node doesnt exist in the reference database
276 * 0: we are to write the first article,
277 * nid: existing reference, returns nid
278 *
279 * $setup['type']: the type column in {node} for our content type
280 */
281 function autonode_create_node($title, $setup, $entry) { //TODO: $title -> $field_name
282 $setup = array();
283 $setup['type'] = 'story'; //this should be like in nodereference, referenceable_types
284 $title = strtoupper($title); //airport code specific, they are capital letters
285
286
287 if($entry) { /* we create an entry if necessary */
288 if(autonode_create_node($title, $setup, 0) == -1) {
289 //this is deep shit, should be filtered out with validate
290 return;
291 }
292
293 if(autonode_create_node($title, $setup, 0) == 0) { //test if we create, or just refer
294 $node = array('type' => $setup['type']);
295 $values['title'] = $title;
296 $values['body'] = 'This is the body text!';
297 $values['name'] = 'drupal';
298 drupal_execute('story_node_form', $values, $node);
299 $nid = autonode_query_nid($title, $setup['type']);
300 }
301
302 else { /* we asked for insertion, but no need, just refer. return the refer nid */
303 $nid = autonode_query_nid($title, $setup['type']);
304 }
305
306 }
307
308 else { /* $entry = 0, checking if entry we try to refer exists */
309 $nid = autonode_query_nid($title, $setup['type']);
310 $nid = $nid ? $nid : in_database($title); /* real nid if exists, 0 if new, -1 if not in db */
311 }
312 return($nid);
313 }
314
315 /**
316 * custom sql query
317 * returns the nid by title, type.
318 * if nothing found $nid = ""
319 * this should be used only if we are looking for ONE nid.
320 */
321 function autonode_query_nid($title, $type) {
322 $nid = db_result(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.title='%s' AND n.type='%s'"), $title, $type ));
323 return $nid;
324 }
325
326 /**
327 * check the imported reference database for entries
328 * we should get here only if there is no entry in the
329 * drupal database already
330 */
331 function in_database($id) {
332 $database = array('WAW', 'BUD', 'AAA');
333 if(in_array($id, $database)) {
334 return(0);
335 }
336 else {
337 return(-1);
338 }
339 }

  ViewVC Help
Powered by ViewVC 1.1.2