/[drupal]/contributions/modules/openresort/terms/terms.module
ViewVC logotype

Contents of /contributions/modules/openresort/terms/terms.module

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


Revision 1.6 - (show annotations) (download) (as text)
Wed Jan 2 22:14:26 2008 UTC (22 months, 3 weeks ago) by marcingy
Branch: MAIN
CVS Tags: DRUPAL-5--1-15-1, DRUPAL-5--1-15-2, DRUPAL-5--1-16-6, DRUPAL-5--1-16-4, DRUPAL-5--1-16-5, DRUPAL-5--1-16-2, DRUPAL-5--1-16-3, DRUPAL-5--1-16-1, DRUPAL-5--1-15, DRUPAL-5--1-16, HEAD
Changes since 1.5: +11 -11 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id: $
3 /**
4 * Theme business terms page
5 */
6 function theme_terms_view($nid, $sections) {
7 $output = '<div class="terms">';
8 foreach ($sections as $sid => $section) {
9 if (strlen($section->name) > 0) {
10 $output .= '<h2 class="title">'. $section->name .'</h2>';
11 }
12 $output .= '<p class="description">'. $section->description .'</p>';
13 foreach ($section->terms as $term) {
14 if (strlen($term->description) > 0) {
15 $output .= '<p class="term">'. $term->description .'</p>';
16 }
17 else {
18 $output .= '<p class="term">'. $term->name .'</p>';
19 }
20 }
21 }
22 $output .= '</div>';
23
24 return $output;
25 }
26
27 /**
28 * Implementation of hook_menu().
29 *
30 */
31 function terms_menu($may_cache) {
32 global $user;
33 $items = array();
34
35 if ($may_cache) {
36 $items[] = array('path' => 'admin/business/terms',
37 'title' => t('Terms and conditions'),
38 'callback' => 'terms_overview_sections',
39 'access' => user_access('administer businesses'));
40 $items[] = array('path' => 'admin/business/terms/list',
41 'title' => t('list'),
42 'type' => MENU_DEFAULT_LOCAL_TASK,
43 'weight' => -10);
44 $items[] = array('path' => 'admin/business/terms/add/section',
45 'title' => t('Add section'),
46 'callback' => 'terms_admin_section_edit',
47 'access' => user_access('administer businesses'),
48 'type' => MENU_LOCAL_TASK);
49 $items[] = array('path' => 'admin/business/terms/edit/section',
50 'title' => t('Edit section'),
51 'callback' => 'terms_admin_section_edit',
52 'access' => user_access('administer businesses'),
53 'type' => MENU_CALLBACK);
54 $items[] = array('path' => 'admin/business/terms/edit/term',
55 'title' => t('Edit conditon'),
56 'callback' => 'terms_admin_term_edit',
57 'access' => user_access('administer businesses'),
58 'type' => MENU_CALLBACK);
59 }
60 else {
61 if (is_numeric(arg(3))) {
62 $items[] = array('path' => 'admin/business/terms/' . arg(3),
63 'callback' => 'terms_overview_terms',
64 'callback arguments' => array(arg(3)),
65 'access' => user_access('Administer businesses'),
66 'type' => MENU_CALLBACK);
67 $items[] = array('path' => 'admin/business/terms/' . arg(3) . '/list',
68 'title' => t('list'),
69 'type' => MENU_DEFAULT_LOCAL_TASK,
70 'weight' => -10);
71 $items[] = array('path' => 'admin/business/terms/' . arg(3) . '/add/term',
72 'title' => t('Add term'),
73 'callback' => 'terms_form_term',
74 'callback arguments' => array(array('sid' => arg(3))),
75 'access' => user_access('administer businesses'),
76 'type' => MENU_LOCAL_TASK);
77 }
78 if (arg(0) == 'node' && is_numeric(arg(1))) {
79 $node = node_load(array('nid' => arg(1)));
80 }
81 if ($node && $node->type == 'business' && $node->ecommerce) {
82 $items[] = array('path' => 'node/'. $node->nid .'/terms',
83 'title' => t('Terms and conditions'),
84 'callback' => 'terms_view',
85 'callback arguments' => array(arg(1)));
86 $access = (business_access('update', $node));
87 $items[] = array('path' => 'node/'. $node->nid .'/edit/terms',
88 'title' => t('Terms and conditions'),
89 'callback' => 'terms_business_policy',
90 'callback arguments' => array(arg(1), arg(5), arg(4)),
91 'access' => $access,
92 'type' => MENU_IS_LOCAL_TASK,
93 'weight' => 5);
94 }
95 }
96 return $items;
97 }
98
99 /**
100 * List and a business' terms
101 */
102 function terms_view($nid = null) {
103
104 business_breadcrumb_by_id($nid);
105 // TODO multiple terms
106 $terms = terms_get_node_terms($nid);
107 $sections = array();
108 foreach (terms_get_sections() as $section) {
109 $section_terms = array();
110 if ($terms[$section->sid]) {
111 $term = terms_get_term($terms[$section->sid]->tid);
112 $section_terms[] = $term;
113 $sections[$section->sid] = $section;
114 $sections[$section->sid]->terms = $section_terms;
115 }
116 }
117
118 return theme('terms_view', $nid, $sections);
119 }
120
121 /**
122 * List and manage sections for administration.
123 */
124 function terms_overview_sections() {
125 $sections = terms_get_sections();
126 $rows = array();
127 foreach ($sections as $section) {
128 $types = array();
129 foreach ($section->nodes as $type) {
130 $node_type = business_get_type_name($type);
131 $types[] = $node_type ? $node_type : $type;
132 }
133 $rows[] = array('name' => l(check_plain($section->name), "admin/business/terms/edit/section/$section->sid"),
134 'type' => implode(', ', $types),
135 'list' => l(t('list terms'), "admin/business/terms/$section->sid"),
136 'add' => l(t('add terms'), "admin/business/terms/$section->sid/add/term")
137 );
138 }
139 if (empty($rows)) {
140 $rows[] = array(array('data' => t('No sections available.'), 'colspan' => '5', 'class' => 'message'));
141 }
142 $header = array(t('Policy sections'), t('Business types'), array('data' => t('Operations'), 'colspan' => '3'));
143
144 return theme('table', $header, $rows, array('id' => 'terms'));
145 }
146
147 /**
148 * Display a list of all available terms in a section, with options to edit
149 * each one.
150 */
151 function terms_overview_terms($sid) {
152 $destination = drupal_get_destination();
153
154 $header = array(t('Condition name'), t('Operations'));
155 $section = terms_get_section($sid);
156
157 drupal_set_title(check_plain($section->name));
158
159 $result = db_query('SELECT * FROM {business_terms} WHERE sid = %d ORDER BY isdefault DESC, weight', $sid);
160 $rows = array();
161 while ($term = db_fetch_object($result)) {
162 $term->isdefault ? $default = '<span class="form-required" title="This is the default term.">*</span></label>' : $default ='';
163 $rows[] = array(check_plain($term->name) . $default,
164 l(t('edit'), "admin/business/terms/edit/term/$term->tid", array(), $destination));
165 }
166 if (!$rows) {
167 $rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
168 }
169
170 return theme('table', $header, $rows, array('id' => 'terms'));
171 }
172
173 /**
174 * List and manage sections from within a business listing.
175 */
176 function terms_business_policy($nid = NULL, $op = NULL, $tid = NULL) {
177 if ($op = 'remove' && is_numeric($tid)) {
178 terms_business_policy_remove($nid, $tid);
179 }
180
181 business_breadcrumb_by_id($nid);
182 drupal_set_title(t("Terms and conditions"));
183
184 $sections = terms_get_sections();
185 $terms = terms_get_node_terms($nid);
186 $rows = array();
187 foreach ($sections as $section) {
188 if (! empty($terms[$section->sid])) {
189 foreach ($terms[$section->sid] as $tid => $term) {
190 if ($term) {
191 if (user_access('administer businesses')) {
192 $rows[] = array('name' => l(check_plain($section->name), "admin/business/terms/edit/section/" . $section->sid),
193 'value' => l(check_plain($term->name), "admin/business/terms/edit/term/" . $term->tid, !empty($term->description) ? array('title' => $term->description) : array()),
194 'remove' => $term->isdefault ? t('default') : l(t('remove'), "node/" . $nid . "/edit/terms/" . $term->tid . "/remove")
195 );
196 }
197 else {
198 $rows[] = array('name' => check_plain($section->name),
199 'value' => l(check_plain($term->name), $_GET['q'], !empty($term->description) ? array('title' => $term->description) : array()),
200 'remove' => $term->isdefault ? t('default') : l(t('remove'), "node/" . $nid . "/edit/terms/" . $term->tid . "/remove")
201 );
202 }
203 }
204 }
205 }
206 }
207 if (empty($rows)) {
208 $rows[] = array(array('data' => t('No policy set.'), 'colspan' => '5', 'class' => 'message'));
209 }
210 $header = array(t('Policy sections'), t('Current policy'), array('data' => t('Operations'), 'colspan' => '3'));
211
212 $output = theme('table', $header, $rows, array('id' => 'terms'));
213 $output .= drupal_get_form('terms_form_policy', array("nid" => $nid, "tid" => $tid));
214 return $output;
215 }
216
217 /**
218 * Removes a policy from a business listing.
219 */
220 function terms_business_policy_remove($nid, $tid) {
221 db_query('DELETE FROM {business_terms_relation} WHERE nid = %d AND tid = %d', $nid, $tid);
222 drupal_set_message(t('Policy removed'));
223 drupal_goto('node/' . $nid . '/edit/terms');
224 }
225
226 /**
227 * Page to add or edit a section
228 */
229 function terms_admin_section_edit($sid = NULL) {
230 if ($_POST['op'] == t('Delete') || $_POST['edit']['confirm']) {
231 return _terms_confirm_del_section($sid);
232 }
233 elseif ($sid) {
234 $section = (array)terms_get_section($sid);
235 drupal_set_title(check_plain($section->name));
236 }
237 return terms_form_section($section);
238 }
239
240 /**
241 * Page to list terms for a section
242 */
243 function terms_admin_term_edit($tid = NULL) {
244 if ($_POST['op'] == t('Delete') || $_POST['edit']['confirm']) {
245 return _terms_confirm_del_term($tid);
246 }
247 elseif ($tid) {
248 $term = (array)terms_get_term($tid);
249 }
250 return terms_form_term($term);
251 }
252
253 /**
254 * Display form for adding and editing vocabularies.
255 */
256 function terms_form_section($edit = array()) {
257
258 return drupal_get_form('terms_form_sections', $edit);
259 }
260
261 function terms_form_sections($edit = array()) {
262 $form['name'] = array('#type' => 'textfield',
263 '#title' => t('Section name'),
264 '#default_value' => $edit['name'],
265 '#maxlength' => 64,
266 '#description' => t('The name for this section in the terms and conditions. Example: "Smoking policy".'),
267 '#required' => TRUE,
268 );
269 $form['description'] = array('#type' => 'textarea',
270 '#title' => t('Description'),
271 '#default_value' => $edit['description'],
272 '#description' => t('Description of the section. Example: "Goods and Services Tax (GST) applies to most goods and services purchased in Canada. Visitors to Canada may be eligible for a refund."'),
273 );
274 $form['multiple'] = array('#type' => 'checkbox',
275 '#title' => t('Multiple selections'),
276 '#default_value' => $edit['multiple'],
277 '#description' => t('Allow a business to select multiple terms. This is useful for where there can be more than one valid policy, like specifying hot tub rules.')
278 );
279 $form['nodes'] = array('#type' => 'checkboxes',
280 '#title' => t('Types'),
281 '#default_value' => $edit['nodes'],
282 '#options' => business_get_types(),
283 '#description' => t('A list of business types you want to associate with this type of condition.'),
284 '#required' => TRUE,
285 );
286 $form['weight'] = array('#type' => 'weight',
287 '#title' => t('Weight'),
288 '#default_value' => $edit['weight'],
289 '#description' => t('In listings, the heavier sections will sink and the lighter sections will be positioned nearer the top.'),
290 );
291
292 // Add extra section form elements.
293 $extra = module_invoke_all('terms', 'form', 'section');
294 if (is_array($extra)) {
295 foreach ($extra as $key => $element) {
296 $extra[$key]['#weight'] = isset($extra[$key]['#weight']) ? $extra[$key]['#weight'] : -18;
297 }
298 $form = array_merge($form, $extra);
299 }
300
301 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
302 if ($edit['sid']) {
303 $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
304 $form['sid'] = array('#type' => 'value', '#value' => $edit['sid']);
305 $form['module'] = array('#type' => 'value', '#value' => $edit['module']);
306 }
307 return $form;
308 }
309
310 /**
311 * Accept the form submission for a section and save the results.
312 */
313 function terms_form_sections_submit($form_id, $form_values) {
314 // Fix up the nodes array to remove unchecked nodes.
315 $form_values['nodes'] = array_filter($form_values['nodes']);
316 switch (terms_save_section($form_values)) {
317 case SAVED_NEW:
318 drupal_set_message(t('Created new section %name.', array('%name' => theme('placeholder', $form_values['name']))));
319 break;
320 case SAVED_UPDATED:
321 drupal_set_message(t('Updated section %name.', array('%name' => theme('placeholder', $form_values['name']))));
322 break;
323 }
324 return 'admin/business/terms';
325 }
326
327 function terms_save_section(&$edit) {
328 $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
329
330 if ($edit['sid'] && $edit['name']) {
331 db_query("UPDATE {business_terms_sections} SET name = '%s', description = '%s', multiple = %d, weight = %d WHERE sid = %d", $edit['name'], $edit['description'], $edit['multiple'], $edit['weight'], $edit['sid']);
332 db_query("DELETE FROM {business_section_node_types} WHERE sid = %d", $edit['sid']);
333 foreach ($edit['nodes'] as $type => $selected) {
334 db_query("INSERT INTO {business_section_node_types} (sid, type) VALUES (%d, '%s')", $edit['sid'], $type);
335 }
336 module_invoke_all('terms', 'update', 'section', $edit);
337 $status = SAVED_UPDATED;
338 }
339 else if ($edit['sid']) {
340 $status = terms_del_section($edit['sid']);
341 }
342 else {
343 $edit['sid'] = db_next_id('{business_terms_sections}_sid');
344 db_query("INSERT INTO {business_terms_sections} (sid, name, description, multiple, weight) VALUES (%d, '%s', '%s', %d, %d)", $edit['sid'], $edit['name'], $edit['description'], $edit['multiple'], $edit['weight']);
345 foreach ($edit['nodes'] as $type => $selected) {
346 db_query("INSERT INTO {business_section_node_types} (sid, type) VALUES (%d, '%s')", $edit['sid'], $type);
347 }
348 module_invoke_all('terms', 'insert', 'section', $edit);
349 $status = SAVED_NEW;
350 }
351
352 cache_clear_all();
353
354 return $status;
355 }
356
357 function terms_del_section($sid) {
358 $section = (array) terms_get_section($sid);
359
360 db_query('DELETE FROM {business_terms_sections} WHERE sid = %d', $sid);
361 db_query('DELETE FROM {business_section_node_types} WHERE sid = %d', $sid);
362 $result = db_query('SELECT tid FROM {business_terms} WHERE sid = %d', $sid);
363 while ($term = db_fetch_object($result)) {
364 terms_del_term($term->tid);
365 }
366
367 module_invoke_all('terms', 'delete', 'section', $section);
368
369 cache_clear_all();
370
371 return SAVED_DELETED;
372 }
373
374 function _terms_confirm_del_section($sid) {
375
376 return drupal_get_form('_terms_confirm_del_section_form',$sid);
377 }
378
379 function _terms_confirm_del_section_form($sid) {
380 $section = terms_get_section($sid);
381
382 $form['type'] = array('#type' => 'value', '#value' => 'section');
383 $form['sid'] = array('#type' => 'value', '#value' => $sid);
384 $form['name'] = array('#type' => 'value', '#value' => $section->name);
385 return confirm_form($form,t('Are you sure you want to delete the section %title?',array('%title' => theme('placeholder', $section->name))),'admin/business/terms', t('Deleting a section will delete all the terms in it. This action cannot be undone.'),t('Delete'),t('Cancel'));
386 }
387
388 function terms_form_term($edit = array()) {
389
390 return drupal_get_form('terms_form_terms', $edit);
391 }
392
393 function terms_form_terms($edit = array()) {
394 $section_id = isset($edit['sid']) ? $edit['sid'] : arg(4);
395 $section = terms_get_section($section_id);
396 drupal_set_title(check_plain($section->name));
397
398 $form['name'] = array('#type' => 'textfield',
399 '#title' => t('Condition name'),
400 '#default_value' => $edit['name'],
401 '#maxlength' => 64,
402 '#description' => t('The short wording of the condition, kept as consise and unambiguous as possible. It will only be seen by administrators. Example: "10:00 AM".'),
403 '#required' => TRUE
404 );
405 $form['description'] = array('#type' => 'textarea',
406 '#title' => t('Customer text'),
407 '#default_value' => $edit['description'],
408 '#description' => t('This is the text seen by the customer.'),
409 '#required' => TRUE
410 );
411 $form['isdefault'] = array('#type' => 'checkbox',
412 '#title' => t('Default term'),
413 '#default_value' => $edit['isdefault'],
414 '#description' => t('Select if this is the default term to be used for the current section.')
415 );
416 $form['weight'] = array('#type' => 'weight',
417 '#title' => t('Weight'),
418 '#default_value' => $edit['weight'],
419 '#description' => t('In listings, the heavier terms will sink and the lighter terms will be positioned nearer the top.')
420 );
421
422 // Add extra term form elements.
423 $extra = module_invoke_all('terms', 'term', 'section');
424 if (is_array($extra)) {
425 foreach ($extra as $key => $element) {
426 $extra[$key]['#weight'] = isset($extra[$key]['#weight']) ? $extra[$key]['#weight'] : -18;
427 }
428 $form = array_merge($form, $extra);
429 }
430
431
432 $form['sid'] = array('#type' => 'value', '#value' => $section->sid);
433 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
434
435 if ($edit['tid']) {
436 $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
437 $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
438 }
439 else {
440 $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
441 }
442
443 return $form;
444 }
445
446 /**
447 * Accept the form submission for a term and save the result.
448 */
449 function terms_form_terms_submit($form_id, $form_values) {
450 switch (terms_save_term($form_values)) {
451 case SAVED_NEW:
452 drupal_set_message(t('Created new term %term.', array('%term' => theme('placeholder', $form_values['name']))));
453 break;
454 case SAVED_UPDATED:
455 drupal_set_message(t('The term %term has been updated.', array('%term' => theme('placeholder', $form_values['name']))));
456 break;
457 }
458 return 'admin/business/terms';
459 }
460
461 function terms_save_term(&$edit) {
462 if ($edit['tid'] && $edit['name']) {
463 if ($edit['isdefault']) {
464 db_query("UPDATE {business_terms} SET isdefault = 0 WHERE sid = %d", $edit['sid']);
465 }
466 db_query("UPDATE {business_terms} SET name = '%s', description = '%s', isdefault = %d, weight = %d WHERE tid = %d", $edit['name'], $edit['description'], $edit['isdefault'], $edit['weight'], $edit['tid']);
467 module_invoke_all('terms', 'update', 'term', $edit);
468 $status = SAVED_UPDATED;
469 }
470 else if ($edit['tid']) {
471 return terms_del_term($edit['tid']);
472 }
473 else {
474 $edit['tid'] = db_next_id('{business_terms}_tid');
475 if ($edit['isdefault']) {
476 db_query("UPDATE {business_terms} SET isdefault = 0 WHERE sid = %d", $edit['sid']);
477 }
478 db_query("INSERT INTO {business_terms} (tid, name, description, sid, isdefault, weight) VALUES (%d, '%s', '%s', %d, %d, %d)", $edit['tid'], $edit['name'], $edit['description'], $edit['sid'], $edit['isdefault'], $edit['weight']);
479 module_invoke_all('terms', 'insert', 'term', $edit);
480 $status = SAVED_NEW;
481 }
482
483 return $status;
484 }
485
486 function terms_del_term($tid) {
487 $term = (array) terms_get_term($tid);
488
489 db_query('DELETE FROM {business_terms} WHERE tid = %d', $tid);
490 db_query('DELETE FROM {business_terms_relation} WHERE tid = %d', $tid);
491 module_invoke_all('terms', 'delete', 'term', $term);
492
493 cache_clear_all();
494 return SAVED_DELETED;
495 }
496
497 function _terms_confirm_del_term($tid) {
498 return drupal_get_form('_terms_confirm_del_term_form',$tid);
499 }
500
501 function _terms_confirm_del_term_form($tid) {
502 $term = terms_get_term($tid);
503
504 $form['type'] = array('#type' => 'value', '#value' => 'term');
505 $form['name'] = array('#type' => 'value', '#value' => $term->name);
506 $form['tid'] = array('#type' => 'value', '#value' => $tid);
507 return confirm_form($form,t('Are you sure you want to delete the term %title?',array('%title' => theme('placeholder', $term->name))),'admin/business/terms',t('Deleting a term will remove it from any business\'s terms and conditions. This action cannot be undone.'),t('Delete'),t('Cancel'));
508 }
509
510 function terms_term_confirm_delete_submit($form_id, $form_values) {
511 terms_del_term($form_values['tid']);
512 drupal_set_message(t('Deleted term %name.', array('%name' => theme('placeholder', $form_values['name']))));
513 return 'admin/business/terms';
514 }
515
516 /**
517 * Adds a form to select a section and term to add
518 */
519 function terms_form_policy($edit = array()) {
520 $form['tid'] = array(
521 '#type' => 'select',
522 '#title' => t('Add new policy'),
523 '#options' => terms_form_policy_select(),
524 '#multiple' => FALSE,
525 '#required' => TRUE,
526 );
527 $form['nid'] = array('#type' => 'value', '#value' => $edit['nid']);
528 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
529 return $form;
530 }
531
532 /**
533 * Accept the form submission for a section and save the results.
534 */
535 function terms_form_policy_submit($form_id, $form_values) {
536 switch (terms_save_policy($form_values)) {
537 case SAVED_NEW:
538 drupal_set_message(t('Policy added'));
539 break;
540 case SAVED_UPDATED:
541 drupal_set_message(t('Policy updated'));
542 break;
543 }
544 return 'node/' . $form_values['nid'] . '/edit/terms';
545 }
546
547 function terms_save_policy(&$edit) {
548 $term = terms_get_term($edit['tid']);
549 $terms = terms_get_node_terms($edit['nid'], false);
550 $section = terms_get_section($term->sid);
551 if (! $section->multiple && $terms[$term->sid]) {
552 // the term might not exist if it was a default, so double-check
553 $old_tid = db_result(db_query("SELECT r.tid FROM {business_terms_relation} r INNER JOIN {business_terms} t ON r.tid = t.tid WHERE r.nid = %d AND t.sid = %d", $edit['nid'], $term->sid));
554 if ($old_tid) {
555 db_query("UPDATE {business_terms_relation} SET nid = '%d', tid = '%d' WHERE nid = %d AND tid = %d", $edit['nid'], $edit['tid'], $edit['nid'], $old_tid);
556 }
557 else {
558 db_query("INSERT INTO {business_terms_relation} (nid, tid) VALUES (%d, '%s')", $edit['nid'], $edit['tid']);
559 }
560 $status = SAVED_UPDATED;
561 }
562 else {
563 db_query("INSERT INTO {business_terms_relation} (nid, tid) VALUES (%d, '%s')", $edit['nid'], $edit['tid']);
564 $status = SAVED_NEW;
565 }
566 return $status;
567 }
568
569 /**
570 * Implementation of hook_checkoutapi().
571 */
572 function terms_checkoutapi(&$txn, $op, $post_op = NULL, $arg4 = NULL) {
573 if ($txn == 'terms') return TRUE;
574 switch ($op) {
575 case 'form':
576 drupal_set_title(t('Booking terms and conditions'));
577 $form = terms_checkout_form($txn);
578 $form[] = array('#type' => 'submit', '#default_value' => t('Agree'));
579 return $form;
580 case 'validate' :
581 break;
582 case 'save':
583 $txn->screen++;
584 break;
585 }
586 }
587
588 /**
589 * The form to display the terms and conditions in the checkout procedure.
590 */
591 function terms_checkout_form($txn) {
592
593 foreach ($txn->items as $key => $item) {
594 $terms = terms_get_node_terms($item->nid);
595 foreach (terms_get_sections() as $section) {
596 if (! empty($terms[$section->sid])) {
597 $form['terms']['section-' . $section->sid]['name'] = array('#value' => '<h2 class="title">' . $section->name.'</h2>');
598 $form['terms']['section-' . $section->sid]['description'] = array('#value' => '<p class="description">'. $section->description .'</p>');
599 foreach ($terms[$section->sid] as $tid => $term) {
600 if ($term->description) {
601 $form['terms']['section-' . $section->sid]['term-' . $tid]['value'] = array('#value' => '<p class="value">'. $term->description .'</p>');
602 }
603 else {
604 $form['terms']['section-' . $section->sid]['term-' . $tid]['value'] = array('#value' => '<p class="value">'. $term->name .'</p>');
605 }
606 }
607 }
608 }
609 $form['terms']['#tree'] = TRUE;
610 }
611 return $form;
612 }
613
614 /**
615 * Return the section object matching a section ID.
616 */
617 function terms_get_section($sid) {
618 static $sections = array();
619
620 if (!array_key_exists($sid, $sections)) {
621 $result = db_query('SELECT s.*, n.type FROM {business_terms_sections} s LEFT JOIN {business_section_node_types} n ON s.sid = n.sid WHERE s.sid = %d ORDER BY s.weight, s.name', $sid);
622 $node_types = array();
623 while ($section = db_fetch_object($result)) {
624 $node_types[] = $section->type;
625 unset($section->type);
626 $section->nodes = $node_types;
627 $sections[$sid] = $section;
628 }
629 }
630
631 return $sections[$sid];
632 }
633
634 /**
635 * Return an array of all sections.
636 *
637 * @param $type
638 * If set, return only those sections associated with the business type.
639 */
640 function terms_get_sections($type = NULL) {
641 if ($type) {
642 $result = db_query("SELECT s.*, n.type FROM {business_terms_sections} s LEFT JOIN {business_section_node_types} n ON s.sid = n.sid WHERE n.type = '%s' ORDER BY s.weight, s.name", $type);
643 }
644 else {
645 $result = db_query('SELECT s.*, n.type FROM {business_terms_sections} s LEFT JOIN {business_section_node_types} n ON s.sid = n.sid ORDER BY s.weight, s.name');
646 }
647
648 $sections = array();
649 $node_types = array();
650 while ($section = db_fetch_object($result)) {
651 $node_types[$section->sid][] = $section->type;
652 unset($section->type);
653 $section->nodes = $node_types[$section->sid];
654 $sections[$section->sid] = $section;
655 }
656
657 return $sections;
658 }
659
660 /**
661 * Return the term object matching a term ID.
662 */
663 function terms_get_term($tid) {
664 return db_fetch_object(db_query('SELECT * FROM {business_terms} WHERE tid = %d', $tid));
665 }
666
667 /**
668 * Returns the complete list of terms, or just those in a given section
669 */
670 function terms_get_terms($sid = null) {
671 if ($sid) {
672 $result = db_query('SELECT * FROM {business_terms} WHERE sid = %d ORDER BY weight, name', $sid);
673 }
674 else {
675 $result = db_query('SELECT * FROM {business_terms} ORDER BY weight, name');
676 }
677 $terms = array();
678 while ($term = db_fetch_object($result)) {
679 $terms[$term->sid][$term->tid] = $term;
680 }
681 return $terms;
682 }
683
684 /**
685 * Return the default terms for a given node.
686 */
687 function terms_get_node_defaults($nid) {
688 $node = node_load($nid);
689 $terms = array();
690
691 foreach (terms_get_sections($node->ptype) as $sid => $section) {
692 $term = terms_get_default_term($sid);
693 $terms[$sid][$term->tid] = $term;
694 }
695 return $terms;
696 }
697
698 /**
699 * Return the default term for a given section.
700 */
701 function terms_get_default_term($sid) {
702 $result = db_query('SELECT * FROM {business_terms} WHERE sid = %d AND isdefault = 1', $sid);
703 return db_fetch_object($result);
704 }
705
706 /**
707 * Return the terms for a business.
708 */
709 function terms_get_node_terms($nid, $include_defaults = true) {
710
711 $include_defaults ? $default_terms = terms_get_node_defaults($nid) : $default_terms = array();
712 $sections = terms_get_sections();
713
714 $result = db_query('SELECT t.* FROM {business_terms} t INNER JOIN {business_terms_relation} r ON t.tid = r.tid WHERE r.nid = %d', $nid);
715 while ($term = db_fetch_object($result)) {
716 $terms[$term->sid][$term->tid] = $term;
717 }
718 // now add any defaults for missing terms
719 foreach (terms_get_sections() as $sid => $sections) {
720 if (! $terms[$sid] && $default_terms[$sid]) {
721 foreach ($default_terms[$sid] as $tid => $term) {
722 if ($term) {
723 $terms[$sid][$tid] = $term;
724 }
725 }
726 }
727 }
728 return $terms;
729 }
730
731 /**
732 * Return the terms for a business, this time without the policy section as an array key
733 * TODO - combine with terms_get_node_terms.
734 * only used by taxes module
735 */
736 function terms_get_node_terms_only($nid, $include_defaults = true, $include_sid = true) {
737
738 $include_defaults ? $default_terms = terms_get_node_defaults($nid) : $default_terms = array();
739 $sections = terms_get_sections();
740
741 $result = db_query('SELECT t.* FROM {business_terms} t INNER JOIN {business_terms_relation} r ON t.tid = r.tid WHERE r.nid = %d', $nid);
742 while ($term = db_fetch_object($result)) {
743 $terms[$term->sid][$term->tid] = $term;
744 }
745 // now add any defaults for missing terms
746 foreach (terms_get_sections() as $sid => $sections) {
747 if (! $terms[$sid] && $default_terms[$sid]) {
748 foreach ($default_terms[$sid] as $tid => $term) {
749 if ($term) {
750 $terms[$sid][$tid] = $term;
751 }
752 }
753 }
754 }
755 // remove the sections
756 $tmp = array();
757 foreach (terms_get_sections() as $sid => $sections) {
758 if($terms[$sid]){
759 foreach ($terms[$sid] as $tid => $term) {
760 $tmp[$tid] = $term;
761 }
762 }
763 }
764 return $tmp;
765 }
766
767 /**
768 * Generate a set of options for selecting a section from all sections. Can be
769 * passed to form_select.
770 */
771 function terms_form_policy_select($type = null, $exists = 0) {
772 $sections = terms_get_sections($type);
773 $terms = terms_get_terms();
774 $options = array();
775 $options[] = t("Select a policy and condition");
776 foreach ($sections as $sid => $section) {
777 $options[$section->name] = array();
778 foreach ($terms[$section->sid] as $tid => $term) {
779 $options[$section->name][$tid] = $term->name;
780 }
781 }
782 return $options;
783 }
784

  ViewVC Help
Powered by ViewVC 1.1.2