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

Diff of /contributions/modules/pay2publish/pay2publish.module

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

revision 1.1.4.1, Fri May 16 13:48:32 2008 UTC revision 1.1.4.2, Tue Jun 10 16:12:34 2008 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: pay2publish.module,v 1.1.2.1 2008/05/16 13:48:32 robertogerola Exp $
3    
4    /**
5     * @file
6     *This module assigns custom taxonomy terms to nodes upon payment. Terms have
7     * configurable expiration periods. User renewal and upgrade, and email notifications
8     * supported.
9     */
10    
11    /**
12     * Implementation of hook_perm().
13     */
14    function pay2publish_perm() {
15      return array('administer pay2publish');
16    }
17    
18    /**
19     * Implementation of hook_link().
20     *
21     * This hook is extended with $type = 'pay2publish terms' to allow themes to
22     * print lists of terms associated with a node. Themes can print pay2publish
23     * links with:
24     *
25     * if (module_exists('pay2publish')) {
26     *   $this->links(pay2publish_link('pay2publish terms', $node));
27     * }
28     * not used yet.
29     */
30    function pay2publish_link($type, $node = NULL) {
31      if ($type == 'pay2publish terms' && $node != NULL) {
32        $links = array();
33        if (array_key_exists('pay2publish', $node)) {
34          foreach ($node->pay2publish as $term) {
35            $links[] = l($term->name, pay2publish_node_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
36          }
37        }
38        return $links;
39      }
40    }
41    
42    function pay2publish_node_path($term) {
43      $pay2publish = pay2publish_get_pay2publish($term->vid);
44      if ($pay2publish->module != 'pay2publish' && $path = module_invoke($pay2publish->module, 'term_path', $term)) {
45        return $path;
46      }
47      return 'pay2publish/node/'. $term->tid;
48    }
49    
50    /**
51     * Implementation of hook_user().
52     */
53    function pay2publish_user($op, &$edit, &$useru) {
54    global $user;
55    if($user->uid == $useru->uid or $user->uid == 1)
56      if ($op == 'view') {
57        $header = array(t('Listing'), t('Level'), t('Months'), t('(days)'), t('Price in E.'), t('Operations'));
58        $destination = drupal_get_destination();
59        $nodeu = pay2publish_get_nodes_u($useru->uid);
60        while ($u = db_fetch_object($nodeu)){
61          $pay2publish = pay2publish_get_pay2publish($u->vid);
62              $n = node_load($u->nid);
63          $rows[] = array(
64                      l($n->title, "node/$n->nid"),
65                      $pay2publish->name,
66                      ($u->duration_m>0)?$u->duration_m:'-',
67                      $u->duration,
68                      $u->cost,
69                      ($u->cost == 0) ? l(t('update'), "pay2publish/edit/node/$u->tid", array(), $destination) : (($u->payment == 0) ? l(t('pay'), "cart/add/$u->nid", array(), $destination).' '.l(t('edit'), "pay2publish/edit/node/$u->tid", array(), $destination)/*.' '.l(t('cancel'), "pay2publish/del/node/$u->tid", array(), $destination)*/: 'It is paid '.l(t('renew'), "cart/add/$u->nid", array(), $destination)));
70            }
71            if ($rows) {
72              $items[] = array('title' => t('Payment'),
73          'value' => theme('table', $header, $rows, array('id' => 'pay2publish')),
74          'class' => 'payment',
75          );
76          return array(t('Payment') => $items);
77            }
78      }
79    }
80    
81    /**
82     * Implementation of hook_menu().
83     */
84    function pay2publish_menu($may_cache) {
85            global $user;
86    
87      $items = array();
88    
89      if (!$may_cache) {
90        $items[] = array('path' => 'admin/pay2publish',
91          'title' => t('Pay 2 Publish'),
92          'callback' => 'pay2publish_overview_actions',
93          'access' => user_access('administer pay2publish'));
94    
95        $items[] = array('path' => 'admin/pay2publish/list',
96          'title' => t('list'),
97          'type' => MENU_DEFAULT_LOCAL_TASK,
98          'weight' => -10);
99    
100        $items[] = array('path' => 'admin/pay2publish/settings',
101            'title' => t('notifications settings'),
102                  'callback' => 'drupal_get_form',
103                  'callback arguments' => 'pay2publish_admin_settings',
104            'access' => user_access('administer pay2publish'),
105            'weight' => 10,
106            'type' => MENU_LOCAL_TASK);
107    
108        $items[] = array('path' => 'admin/pay2publish/add/duration',
109          'title' => t('add duration'),
110          'callback' => 'drupal_get_form',
111          'callback arguments' => 'pay2publish_form_duration',
112          'access' => user_access('administer pay2publish'),
113          'type' => MENU_LOCAL_TASK);
114    
115        $items[] = array('path' => 'admin/pay2publish/edit/duration',
116          'title' => t('edit duration'),
117          'callback' => 'drupal_get_form',
118          'callback arguments' => array('pay2publish_form_duration', arg(4)),
119          'access' => user_access('administer pay2publish'),
120          'type' => MENU_CALLBACK);
121    
122        $items[] = array('path' => 'admin/pay2publish/delete/duration',
123          'title' => t('delete duration'),
124          'callback' => 'drupal_get_form',
125          'callback arguments' => array('pay2publish_confirm_delete_duration', arg(4)),
126          'access' => user_access('administer pay2publish'),
127          'type' => MENU_CALLBACK);
128    
129        $items[] = array('path' => 'admin/pay2publish/add/action',
130          'title' => t('add action'),
131          'callback' => 'drupal_get_form',
132          'callback arguments' => 'pay2publish_form_action',
133          'access' => user_access('administer pay2publish'),
134          'type' => MENU_LOCAL_TASK);
135    
136        $items[] = array('path' => 'admin/pay2publish/edit/action',
137          'title' => t('edit action'),
138          'callback' => 'drupal_get_form',
139          'callback arguments' => array('pay2publish_form_action', arg(4)),
140          'access' => user_access('administer pay2publish'),
141          'type' => MENU_CALLBACK);
142    
143        $items[] = array('path' => 'admin/pay2publish/delete/action',
144          'title' => t('delete action'),
145          'callback' => 'drupal_get_form',
146          'callback arguments' => array('pay2publish_confirm_delete_action', arg(4)),
147          'access' => user_access('administer pay2publish'),
148          'type' => MENU_CALLBACK);
149    
150                    $items[] = array('path' => 'admin/pay2publish/edit/node',
151          'title' => t('edit node'),
152          'callback' => 'drupal_get_form',
153          'callback arguments' => array('pay2publish_form_node', arg(4)),
154          'access' => user_access('administer pay2publish'),
155          'type' => MENU_CALLBACK);
156    
157        $items[] = array('path' => 'admin/pay2publish/delete/node',
158          'title' => t('delete node'),
159          'callback' => 'drupal_get_form',
160          'callback arguments' => array('pay2publish_confirm_delete_node', arg(4)),
161          'access' => user_access('administer pay2publish'),
162          'type' => MENU_CALLBACK);
163    
164        if (is_numeric(arg(2))) {
165          $items[] = array('path' => 'admin/pay2publish/' . arg(2),
166            'title' => t('list node'),
167            'callback' => 'pay2publish_overview_nodes',
168            'callback arguments' => array(arg(2)),
169            'access' => user_access('administer pay2publish'),
170            'type' => MENU_CALLBACK);
171    
172          $items[] = array('path' => 'admin/pay2publish/' . arg(2) . '/list',
173            'title' => t('list'),
174            'type' => MENU_DEFAULT_LOCAL_TASK,
175            'weight' => -10);
176    
177          $items[] = array('path' => 'admin/pay2publish/' . arg(2) . '/add/node',
178            'title' => t('add node'),
179            'callback' => 'drupal_get_form',
180            'callback arguments' => array('pay2publish_add_nodes', 'vid' => arg(2), 'nid' => arg(5)),
181            'access' => user_access('administer pay2publish'),
182            'type' => MENU_LOCAL_TASK);
183    
184          $items[] = array('path' => 'admin/pay2publish/' . arg(2) . '/return',
185            'title' => t('list actions'),
186            'callback' => 'pay2publish_ret',
187            'callback arguments' => array(arg(2)),
188            'access' => user_access('administer pay2publish'),
189            'weight' => -11,
190            'type' => MENU_LOCAL_TASK);
191    
192        }
193      }
194    
195      if (is_numeric(arg(3))){
196                    $n = pay2publish_get_node(arg(3));
197        $items[] = array('path' => 'pay2publish/del/node/' . arg(3),
198            'title' => t('del node'),
199                  'callback' => 'drupal_get_form',
200            'callback arguments' => array('pay2publish_del_node_u', arg(3)),
201            'access' => (user_access('access content') and ($user->uid == $n->uid)),
202            'type' => MENU_CALLBACK);
203    
204        $items[] = array('path' => 'pay2publish/edit/node/' . arg(3),
205            'title' => t('update node'),
206                  'callback' => 'drupal_get_form',
207            'callback arguments' => array('pay2publish_edit_node_u', arg(3)),
208            'access' => (user_access('access content') and ($user->uid == $n->uid)),
209            'type' => MENU_CALLBACK);
210    
211            $items[] = array('path' => 'pay2publish/payment/node/' . arg(3),
212            'title' => t('payment node'),
213                  'callback' => 'drupal_get_form',
214            'callback arguments' => array('pay2publish_checkout_form', arg(3)),
215            'access' => (user_access('access content') and ($user->uid == $n->uid)),
216            'type' => MENU_CALLBACK);
217      }
218    
219      return $items;
220    }
221    
222    function pay2publish_edit_node_u($tid) {
223      drupal_add_js(drupal_get_path('module', 'pay2publish') . '/pay2publish.js', 'module', 'header', FALSE);
224            global $user;
225      $term = pay2publish_get_node($tid);
226      $pay2publish = pay2publish_get_pay2publish($term->vid);
227      $node = node_load($term->nid);
228      $c = db_query(db_rewrite_sql("SELECT v.* FROM {pay2publish} v INNER JOIN {pay2publish_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
229            $mes = '';
230      while ($pay2publish = db_fetch_object($c)) {
231                    $deftype[$pay2publish->vid] = $pay2publish->name.' (Cost of month:'.$pay2publish->cost.')';
232                    if ($pay2publish->cost == 0) $def = $pay2publish->vid;
233      }
234            $durations = pay2publish_get_duration();
235            $defdu[0] = 'Unlimited';
236      foreach ($durations as $term) {
237        $defdu[$term->vid] = $term->name;
238      }
239            if ($node->nid) {
240        $default_node = pay2publish_get_node_n_s($node->nid);
241                    $default_nodes = $default_node->vid;
242                    $default_durati = pay2publish_get_duration_vid($default_node->duration_m);
243                    $default_duration = $default_durati->vid;
244            }
245      $form['pay2publish'] = array(
246        '#type' => 'radios',
247        '#title' => t('Paid advertising'),
248        '#default_value' => $default_nodes,
249        '#options' => $deftype,
250        '#required' => FALSE,
251              '#weight' => -1,
252      );
253      $form['pay2publish_duration'] = array(
254        '#type' => 'radios',
255        '#title' => t('Duration'),
256        '#default_value' => $default_duration,
257        '#options' => $defdu,
258        '#required' => FALSE,
259            '#weight' => 0,
260      );
261      $form['#attributes']['onclick'] = 'pay2publish_checkstate('. $def .')';
262      $form['tid'] = array('#type' => 'value', '#value' => $tid);
263      $form['des'] = array('#type' => 'value', '#value' => $_GET['destination'] ? $_GET['destination'] : 'user/'. $user->uid);
264      return confirm_form($form, t('Edit duration'),
265                      $_GET['destination'] ? $_GET['destination'] : 'user/'. $user->uid,
266                      t(''),
267                      t('OK'),
268                      t('Cancel'));
269    
270    }
271    
272    function pay2publish_edit_node_u_submit($form_id, $form_values) {
273      if (is_numeric($form_values['tid'])){
274            $term = pay2publish_get_node($form_values['tid']);
275            $pay2publish = pay2publish_get_pay2publish($form_values['pay2publish']);
276            $node = node_load($term->nid);
277        pay2publish_del_node($form_values['tid']);
278    
279          $edit['nid'] = $node->nid;
280                $edit['nvid'] = $node->vid;
281          $edit['name'] = $node->title;
282          $edit['uid'] = $node->uid;
283          $edit['vid'] = $pay2publish->vid;
284          $durations = pay2publish_get_duration(is_numeric($form_values['pay2publish_duration']) ? $form_values['pay2publish_duration'] : 0);
285              if ($durations->duration){
286            $edit['duration'] = $durations->duration*31;
287            $edit['duration_m'] = $durations->duration;
288                $edit['cost'] = $pay2publish->cost*$durations->duration;
289              }else{
290            $edit['duration'] = $pay2publish->duration;
291            $edit['duration_m'] = 0;
292                $edit['cost'] = $pay2publish->cost;
293              }
294          if (module_exists('taxonomy') and !module_exists('category')) $rrt1 = taxonomy_get_term($pay2publish->taxtermend);
295          if (module_exists('category')) $rrt2 = category_get_category($pay2publish->cattermend);
296          if (($rrt1->name)or($rrt2->title))
297                $edit['act'] = $rrt1->name.(($rrt1->name)?(($rrt2->title)?', ':'').$rrt2->title:$rrt2->title);
298              else
299                $edit['act'] = '';
300              $edit['payment'] = 0;
301              pay2publish_save_node($edit);
302          drupal_set_message("Save: ".$node->title);
303      }
304      return $form_values['des'];
305    }
306    
307    function pay2publish_admin_settings($edit = array()) {
308     $form['mail_first'] = array('#type' => 'fieldset',
309        '#title' => t('Mail First Notification'),
310      );
311      $form['mail_first']['mail_notification_day'] = array('#type' => 'textfield',
312        '#title' => t('Mail Notification Day'),
313        '#default_value' => variable_get('pay2publish_mail_notification_day', 5),
314        '#maxlength' => 64,
315        '#description' => '',
316        '#required' => TRUE
317      );
318      $form['mail_first']['mail_notification_subject'] = array('#type' => 'textfield',
319        '#title' => t('Mail Notification Subject'),
320        '#default_value' => variable_get('pay2publish_mail_notification_subject','The period of the publication comes to an end'),
321        '#maxlength' => 64,
322        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
323        '#required' => TRUE
324      );
325      $form['mail_first']['mail_notification_body'] = array('#type' => 'textarea',
326        '#title' => t('Mail Notification Body'),
327        '#default_value' => variable_get('pay2publish_mail_notification_body',"The period of the publication\n%node_title\n%node_url\ncomes to an end in %node_day_end days\n\n%date\n%site %url"),
328        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
329            '#required' => TRUE
330      );
331    
332     $form['mail_last'] = array('#type' => 'fieldset',
333        '#title' => t('Mail Last Notification'),
334      );
335      $form['mail_last']['mail_last_notification_subject'] = array('#type' => 'textfield',
336        '#title' => t('Mail Notification Subject'),
337        '#default_value' => variable_get('pay2publish_mail_last_notification_subject','The period of the publication comes to an end'),
338        '#maxlength' => 64,
339        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
340        '#required' => TRUE
341      );
342      $form['mail_last']['mail_last_notification_body'] = array('#type' => 'textarea',
343        '#title' => t('Mail Notification Body'),
344        '#default_value' => variable_get('pay2publish_mail_last_notification_body',"The period of the publication\n%node_title\n%node_url\ncomes to an end in %node_day_end days\n\n%date\n%site %url"),
345        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
346            '#required' => TRUE
347      );
348    
349     $form['mail_end'] = array('#type' => 'fieldset',
350        '#title' => t('Mail Expired Notification'),
351      );
352      $form['mail_end']['mail_end_notification_subject'] = array('#type' => 'textfield',
353        '#title' => t('Mail Notification Subject'),
354        '#default_value' => variable_get('pay2publish_mail_end_notification_subject','The period of the publication comes to an end'),
355        '#maxlength' => 64,
356        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
357        '#required' => TRUE
358      );
359      $form['mail_end']['mail_end_notification_body'] = array('#type' => 'textarea',
360        '#title' => t('Mail Notification Body'),
361        '#default_value' => variable_get('pay2publish_mail_end_notification_body',"The period of the publication\n%node_title\n%node_url\ncomes to an end in %node_day_end days\n\n%date\n%site %url"),
362        '#description' => t('Customize the body of the Mail Notification.') .' '. t('Available variables are:') .' %username, %usermail, %site, %url, %date, %node_title, %node_id, %node_url, %node_day_end.',
363            '#required' => TRUE
364      );
365    
366      return system_settings_form($form);
367    }
368    
369    /**
370     * List and manage actions.
371     */
372    function pay2publish_overview_actions() {
373      $actions = pay2publish_get_actions();
374      $rows = array();
375      foreach ($actions as $pay2publish) {
376        $types = array();
377        foreach ($pay2publish->nodes as $type) {
378          $node_type = node_get_types('name', $type);
379          $types[] = $node_type ? $node_type : $type;
380        }
381        if (module_exists('taxonomy') and !module_exists('category')){
382              $rrt1 = taxonomy_get_term($pay2publish->taxtermstart);
383              $rrt2 = taxonomy_get_term($pay2publish->taxtermend);
384          if (!$rrt1->name) $rrt1->name = 'none';
385          if (!$rrt2->name) $rrt2->name = 'none';
386            $tax = array('taxtermstart' => $rrt1->name, 'taxtermend' => $rrt2->name);
387            }else $tax = array();
388    
389            if (module_exists('category')){
390              $rrt1 = category_get_category($pay2publish->cattermstart);
391              $rrt2 = category_get_category($pay2publish->cattermend);
392          if (!$rrt1->title) $rrt1->title = 'none';
393          if (!$rrt2->title) $rrt2->title = 'none';
394            $cat = array('cattermstart' => $rrt1->title, 'cattermend' => $rrt2->title);
395            }else $cat = array();
396    
397            $rows[] = array_merge(array('name' => check_plain($pay2publish->name), 'type' => implode(', ', $types)), $tax, $cat, array(
398          /*'duration' => $pay2publish->duration,*/ 'cost' => $pay2publish->cost,
399          'edit' => l(t('edit actions'), "admin/pay2publish/edit/action/$pay2publish->vid"),
400          'list' => l(t('list nodes'), "admin/pay2publish/$pay2publish->vid"),
401          'add' => l(t('add nodes'), "admin/pay2publish/$pay2publish->vid/add/node"))
402        );
403      }
404      if (empty($rows)) {
405        $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '4', 'class' => 'message'));
406      }
407        if (module_exists('taxonomy') and !module_exists('category')){
408            $tax = array(t('Taxonomy Payment'), t('Taxonomy Default'));
409            }else $tax = array();
410            if (module_exists('category')){
411            $cat = array(t('Category Payment'), t('Category Default'));
412            }else $cat = array();
413      $header = array_merge(array(t('Name'), t('Type')), $tax, $cat, array(/*t('Duration'),*/ t('Cost'), array('data' => t('Operations'), 'colspan' => '3')));
414    
415    
416      $durations = pay2publish_get_duration();
417      $rowsd = array();
418      foreach ($durations as $duration) {
419    
420            $rowsd[] = array('name' => check_plain($duration->name),
421          'duration' => $duration->duration,
422          'edit' => l(t('edit duration'), "admin/pay2publish/edit/duration/$duration->vid")
423        );
424      }
425      if (empty($rows)) {
426        $rowsd[] = array(array('data' => t('No items available.'), 'colspan' => '3', 'class' => 'message'));
427      }
428    
429      $headerd = array(t('Name'), t('Duration'), t('Operations'));
430    
431    
432      return theme('table', $header, $rows, array('id' => 'pay2publish')).'<br><h1 class="title">Duration</h1>'.theme('table', $headerd, $rowsd, array('id' => 'pay2publish'));
433    }
434    
435    /**
436     * List and manage nodes.
437     */
438    function pay2publish_overview_nodes($vid) {
439      $destination = drupal_get_destination();
440    
441      $header = array(t('Name'), t('User'), t('Actions'), t('Months'), t('Duration'), t('Cost'), t('Payment'), t('Operations'));
442      $pay2publish = pay2publish_get_pay2publish($vid);
443    
444      drupal_set_title(check_plain($pay2publish->name));
445      $start_from      = $_GET['page'] ? $_GET['page'] : 0;
446      $total_entries   = 0;  // total count for pager
447      $page_increment  = 25; // number of tids per page
448      $displayed_count = 0;  // number of tids shown
449    
450      $result = db_query('SELECT * FROM {pay2publish_node} WHERE vid = %d', $vid);
451      while ($tnode = db_fetch_object($result)){
452        $total_entries++;
453        if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) { continue; }
454            $user = user_load(array('uid'=>$tnode->uid));
455    //      $rrt1 = taxonomy_get_term($tnode->taxterm);
456            if (!$tnode->act) $tnode->act = 'none';
457        $rows[] = array(l($tnode->name, "node/$tnode->nid"), $user->name, $tnode->act, ($tnode->duration_m>0)?$tnode->duration_m:'-', $tnode->duration, $tnode->cost, ($tnode->payment == 0) ? 'no' : 'yes', l(t('edit'), "admin/pay2publish/edit/node/$tnode->tid", array()));
458        $displayed_count++;
459      }
460    
461      if (!$rows) {
462        $rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
463      }
464    
465      $GLOBALS['pager_page_array'][] = $start_from;  // FIXME
466      $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; // FIXME
467    
468      if ($total_entries >= $page_increment) {
469        $rows[] = array(array('data' => theme('pager', NULL, $page_increment), 'colspan' => '2'));
470      }
471    
472      return theme('table', $header, $rows, array('id' => 'pay2publish'));
473    }
474    
475    /**
476     * Display form for adding and editing actions.
477     */
478    function pay2publish_form_action($vid=0) {
479      $edit = (array)pay2publish_get_pay2publish($vid);
480      $form['name'] = array('#type' => 'textfield',
481        '#title' => t('Level Name'),
482        '#default_value' => $edit['name'],
483        '#maxlength' => 64,
484        '#description' => t('The name for this Pay 2 Publish level. Example: "Basic" or "Silver".'),
485        '#required' => TRUE,
486      );
487    
488      $form['description'] = array('#type' => 'textarea',
489        '#title' => t('Description'),
490        '#default_value' => $edit['description'],
491        '#description' => t('Description of the level (may be used by other modules, if you dont know what to write just skip it).'),
492      );
493    
494      $form['nodes'] = array('#type' => 'checkboxes',
495        '#title' => t('Types'),
496        '#default_value' => $edit['nodes'],
497        '#options' => node_get_types('names'),
498        '#description' => t('Check the node types you want to associate with this level.'),
499        '#required' => TRUE,
500      );
501    
502      if (module_exists('taxonomy') and !module_exists('category')){
503        $options[0] = '<none>';
504        $taxarray = array_merge($options, taxonomy_form_all());
505        $form['taxtermstart'] = array(
506          '#title' => t('Upgrade to'),
507          '#type' => 'select',
508          '#options' => $taxarray,
509          '#default_value' => $edit['taxtermstart'],
510          '#description' => t('Select the taxonomy level term to attach to node after payment.'),
511        );
512        $form['taxtermend'] = array(
513          '#title' => t('Downgrade to'),
514          '#type' => 'select',
515          '#options' => $taxarray,
516          '#default_value' => $edit['taxtermend'],
517          '#description' => t('Select the taxonomy level term to attach to the node after payment has been processed.'),
518        );
519      }
520      if (module_exists('category')){
521       $options[0] = '<none>';
522       $catarray = array_merge ($options, pay2publish_category_form_all());
523       $form['cattermstart'] = array(
524         '#title' => t('Upgrade to'),
525         '#type' => 'select',
526         '#options' => $catarray,
527         '#default_value' => $edit['cattermstart'],
528         '#description' => t('Select the category level term to attach to node after payment.'),
529       );
530       $form['cattermend'] = array(
531         '#title' => t('Downgrade to'),
532         '#type' => 'select',
533         '#options' => $catarray,
534         '#default_value' => $edit['cattermend'],
535         '#description' => t('Select the default (basic) category level term to downgrade the node to once payment period expires.'),
536       );
537      }
538      $form['cost'] = array('#type' => 'textfield',
539        '#title' => t('Price'),
540        '#default_value' => $edit['cost'],
541        '#description' => t('The final price for this level. The value specified in eCommerce settings applies.'),
542        '#maxlength' => 255,
543      );
544    
545      $form['weight'] = array('#type' => 'weight',
546        '#title' => t('Weight'),
547        '#default_value' => $edit['weight'],
548        '#description' => t('In listings, the nodes tagged with heavier levels will sink and nodes with the lighter ones will be positioned closer the top.'),
549      );
550    
551      $extra = module_invoke_all('pay2publish', 'form', 'pay2publish');
552      if (is_array($extra)) {
553        foreach ($extra as $key => $element) {
554          $extra[$key]['#weight'] = isset($extra[$key]['#weight']) ? $extra[$key]['#weight'] : -18;
555        }
556        $form = array_merge($form, $extra);
557      }
558    
559      $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
560      if ($edit['vid']) {
561        $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
562        $form['vid'] = array('#type' => 'value', '#value' => $edit['vid']);
563        $form['module'] = array('#type' => 'value', '#value' => $edit['module']);
564      }
565      return $form;
566    }
567    
568    /**
569     * Accept the form submission for a pay2publish and save the results.
570     */
571    function pay2publish_form_action_submit($form_id, $form_values) {
572      $op = $form_values['op'];
573      $vid = $form_values['vid'];
574      if($op == t('Delete')) {
575        drupal_goto('admin/pay2publish/delete/action/' . $vid);
576      }
577    
578      // Fix up the nodes array to remove unchecked nodes.
579      $form_values['nodes'] = array_filter($form_values['nodes']);
580      switch (pay2publish_save_taxonomy_action($form_values)) {
581      case SAVED_NEW:
582        drupal_set_message(t('Created new pay2publish %name.', array('%name' => $form_values['name'])));
583        break;
584      case SAVED_UPDATED:
585        drupal_set_message(t('Updated pay2publish %name.', array('%name' => $form_values['name'])));
586        break;
587      }
588      return 'admin/pay2publish';
589    }
590    
591    function pay2publish_save_taxonomy_action(&$edit) {
592      $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
593    
594      if ($edit['vid'] && $edit['name']) {
595        db_query("UPDATE {pay2publish} SET name = '%s', description = '%s', taxtermstart = %d, taxtermend = %d, cattermstart = %d, cattermend = %d, duration = %d, cost = '%s', weight = %d WHERE vid = %d", $edit['name'], $edit['description'], $edit['taxtermstart'], $edit['taxtermend'], $edit['cattermstart'], $edit['cattermend'], $edit['duration'], $edit['cost'], $edit['weight'], $edit['vid']);
596        db_query("DELETE FROM {pay2publish_node_types} WHERE vid = %d", $edit['vid']);
597        foreach ($edit['nodes'] as $type => $selected) {
598          db_query("INSERT INTO {pay2publish_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
599        }
600        module_invoke_all('pay2publish', 'update', 'pay2publish', $edit);
601        $status = SAVED_UPDATED;
602      }
603      else if ($edit['vid']) {
604        $status = pay2publish_delete_action($edit['vid']);
605      }
606      else {
607        $edit['vid'] = db_next_id('{pay2publish}_vid');
608        db_query("INSERT INTO {pay2publish} (vid, name, description, taxtermstart, taxtermend, cattermstart, cattermend, duration, cost, weight) VALUES (%d, '%s', '%s', %d, %d, %d, %d, %d, '%s', %d)", $edit['vid'], $edit['name'], $edit['description'], $edit['taxtermstart'], $edit['taxtermend'], $edit['cattermstart'], $edit['cattermend'], $edit['duration'], $edit['cost'], $edit['weight']);
609        foreach ($edit['nodes'] as $type => $selected) {
610          db_query("INSERT INTO {pay2publish_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
611        }
612        module_invoke_all('pay2publish', 'insert', 'pay2publish', $edit);
613        $status = SAVED_NEW;
614      }
615    
616      cache_clear_all();
617    
618      return $status;
619    }
620    
621    function pay2publish_delete_action($vid) {
622      db_query('DELETE FROM {pay2publish} WHERE vid = %d', $vid);
623      db_query('DELETE FROM {pay2publish_node_types} WHERE vid = %d', $vid);
624      $result = db_query('SELECT tid FROM {pay2publish_node} WHERE vid = %d', $vid);
625      while ($term = db_fetch_object($result)) {
626        pay2publish_del_node($term->tid);
627      }
628    
629      $pay2publish = (array) pay2publish_get_pay2publish($vid);
630      module_invoke_all('pay2publish', 'delete', 'pay2publish', $pay2publish);
631    
632      cache_clear_all();
633    
634      return SAVED_DELETED;
635    }
636    
637    function pay2publish_confirm_delete_action($vid) {
638      $taxonomy_action = pay2publish_get_action($vid);
639    
640      $form['type'] = array('#type' => 'value', '#value' => 'pay2publish');
641      $form['vid'] = array('#type' => 'value', '#value' => $vid);
642      $form['name'] = array('#type' => 'value', '#value' => $taxonomy_action->name);
643      return confirm_form($form, t('Are you sure you want to delete the action %title?', array('%title' => $taxonomy_action->name)),
644                      'admin/pay2publish', t('Deleting an action. This action cannot be undone.'),
645                      t('Delete'),
646                      t('Cancel'));
647    }
648    
649    function pay2publish_confirm_delete_action_submit($form_id, $form_values) {
650      $status = pay2publish_delete_action($form_values['vid']);
651      drupal_set_message(t('Deleted action %name.', array('%name' => $form_values['name'])));
652      return 'admin/pay2publish';
653    }
654    
655    function pay2publish_add_nodes($vid, $nid) {
656      $pay2publish_id = $vid;
657      $pay2publish = pay2publish_get_pay2publish($pay2publish_id);
658    
659      global $form_values;
660      $output = node_filter_form();
661    
662      $filter = node_build_filter_query();
663    
664      $result = pager_query('SELECT n.*, u.name, u.uid FROM {node} n '. $filter['join'] .' INNER JOIN {users} u ON n.uid = u.uid '. $filter['where'] .' ORDER BY n.changed DESC', 50, 0, NULL, $filter['args']);
665    
666      $destination = drupal_get_destination();
667      while ($node = db_fetch_object($result)) {
668        $nodes[$node->nid] = '';
669        if (is_numeric($nid)){
670                    if ($nid == $node->nid) {
671              $nodet = $node;
672                    }
673            }
674        $form['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
675        $form['name'][$node->nid] =  array('#value' => node_get_types('name', $node));
676        $form['username'][$node->nid] = array('#value' => theme('username', $node));
677        $form['status'][$node->nid] =  array('#value' =>  ($node->status ? t('published') : t('not published')));
678        $form['operations'][$node->nid] = array('#value' => l(t('add'), 'admin/pay2publish/'.$pay2publish_id.'/add/node/'.$node->nid , array(), $destination));
679      }
680      $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
681    
682      if (is_numeric($nid) and isset($nodet)){
683        $noden = pay2publish_get_node_n($nid, $pay2publish->vid);
684            if (is_numeric($noden->tid)){
685          drupal_set_message($nodet->title." already exists");
686            }else{
687          $edit['nid'] = $nodet->nid;
688          $edit['nvid'] = $nodet->vid;
689          $edit['name'] = $nodet->title;
690          $edit['uid'] = $nodet->uid;
691          $edit['vid'] = $pay2publish->vid;
692          $edit['duration_m'] = 0;
693          $edit['duration'] = $pay2publish->duration;
694          $edit['cost'] = $pay2publish->cost;
695          if (module_exists('taxonomy') and !module_exists('category')) $rrt1 = taxonomy_get_term($pay2publish->taxtermend);
696          if (module_exists('category')) $rrt2 = category_get_category($pay2publish->cattermend);
697          if (($rrt1->name)or($rrt2->title))
698                $edit['act'] = $rrt1->name.(($rrt1->name)?(($rrt2->title)?', ':'').$rrt2->title:$rrt2->title);
699              else
700                $edit['act'] = '';
701              $edit['payment'] = 0;
702              pay2publish_save_node($edit);
703          drupal_set_message("Added ".$nodet->title);
704        }
705      }
706      return $form;
707    }
708    
709    function pay2publish_form_node($tid) {
710      $edit = (array)pay2publish_get_node($tid);
711      $pay2publish_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
712      $pay2publish = pay2publish_get_pay2publish($pay2publish_id);
713    
714      $form['name'] = array('#type' => 'textfield',
715              '#title' => t('Node Title'),
716              '#default_value' => $edit['name'],
717              '#maxlength' => 64,
718              '#description' => t('The name for this term.  Example: "Linux".'),
719              '#required' => TRUE
720              );
721    
722      $form['tid'] = array('#type' => 'hidden',
723        '#default_value' => $edit['tid'],
724      );
725      $form['nid'] = array('#type' => 'hidden',
726        '#default_value' => $edit['nid'],
727      );
728      $form['vid'] = array('#type' => 'hidden',
729        '#default_value' => $edit['vid'],
730      );
731      $form['uid'] = array('#type' => 'hidden',
732        '#default_value' => $edit['uid'],
733      );
734      $form['taxtermstart'] = array('#type' => 'hidden',
735        '#default_value' => $pay2publish->taxtermstart,
736      );
737      $form['taxtermend'] = array('#type' => 'hidden',
738        '#default_value' => $pay2publish->taxtermend,
739      );
740      $form['cattermstart'] = array('#type' => 'hidden',
741        '#default_value' => $pay2publish->cattermstart,
742      );
743      $form['cattermend'] = array('#type' => 'hidden',
744        '#default_value' => $pay2publish->cattermend,
745      );
746      $form['act'] = array('#type' => 'textfield',
747        '#title' => t('Active'),
748        '#default_value' => $edit['act'],
749        '#maxlength' => 255,
750      );
751      $form['duration_m'] = array('#type' => 'textfield',
752        '#title' => t('Months'),
753        '#default_value' => $edit['duration_m'],
754        '#maxlength' => 255,
755      );
756      $form['duration'] = array('#type' => 'textfield',
757        '#title' => t('Duration'),
758        '#default_value' => $edit['duration'],
759        '#maxlength' => 255,
760      );
761      $form['cost'] = array('#type' => 'textfield',
762        '#title' => t('Cost'),
763        '#default_value' => $edit['cost'],
764        '#maxlength' => 255,
765      );
766      $form['payment'] = array(
767        '#title' => t('Payment'),
768        '#type' => 'select',
769        '#options' => array('0'=>'no', '1'=>'yes'),
770        '#default_value' => $edit['payment'],
771      );
772      $form['weight'] = array('#type' => 'weight',
773        '#title' => t('Weight'),
774        '#default_value' => $edit['weight'],
775        '#description' => t('In listings, the heavier actions will sink and the lighter actions will be positioned nearer the top.'),
776      );
777    
778    
779      $extra = module_invoke_all('pay2publish', 'form', 'term');
780    
781      $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
782    
783      if ($edit['tid']) {
784        $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
785        $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
786      }
787    
788      return $form;
789    }
790    
791    /**
792     * Accept the form submission for a pay2publish node and save the result.
793     */
794    function pay2publish_form_node_submit($form_id, $form_values) {
795      $op = $form_values['op'];
796      $tid = $form_values['tid'];
797      if($op == t('Delete')) {
798        drupal_goto('admin/pay2publish/delete/node/' . $tid);
799      }
800    
801      switch (pay2publish_save_node($form_values)) {
802        case SAVED_NEW:
803          drupal_set_message(t('Created new term %term.', array('%term' => $form_values['name'])));
804          break;
805        case SAVED_UPDATED:
806          drupal_set_message(t('The term %term has been updated.', array('%term' => $form_values['name'])));
807          break;
808      }
809      return 'admin/pay2publish';
810    }
811    
812    function pay2publish_save_node(&$edit) {
813            $r = pay2publish_get_pay2publish($edit['vid']);
814      if ($edit['tid'] && $edit['name']) {
815        if ($edit['payment']==0){
816          if (module_exists('taxonomy') and !module_exists('category')) $rrt1 = taxonomy_get_term($edit['taxtermend']);
817          if (module_exists('category')) $rrt2 = category_get_category($edit['cattermend']);
818          if (($rrt1->name)or($rrt2->title))
819                $edit['act'] = $rrt1->name.(($rrt1->name)?(($rrt2->title)?', ':'').$rrt2->title:$rrt2->title);
820              else
821                $edit['act'] = '';
822              $edit['taxterm'] = $edit['taxtermend'];
823              $edit['catterm'] = $edit['cattermend'];
824            }else{
825          if (module_exists('taxonomy') and !module_exists('category')) $rrt1 = taxonomy_get_term($edit['taxtermstart']);
826          if (module_exists('category')) $rrt2 = category_get_category($edit['cattermstart']);
827          if (($rrt1->name)or($rrt2->title))
828                $edit['act'] = $rrt1->name.(($rrt1->name)?(($rrt2->title)?', ':'').$rrt2->title:$rrt2->title);
829              else
830                $edit['act'] = '';
831              $edit['taxterm'] = $edit['taxtermstart'];
832              $edit['catterm'] = $edit['cattermstart'];
833            }
834        db_query("UPDATE {pay2publish_node} SET nid = %d, vid = %d, uid = %d, name = '%s', act = '%s', duration_m = %d, duration = %d, cost = '%s', payment = %d, weight = %d WHERE tid = %d", $edit['nid'], $edit['vid'], $edit['uid'], $edit['name'], $edit['act'], $edit['duration_m'], $edit['duration'], $edit['cost'], $edit['payment'], $edit['weight'], $edit['tid']);
835    
836        if (module_exists('taxonomy') and !module_exists('category')) {
837                              db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $edit['nid'], $edit['taxtermstart'], $edit['taxtermend']);
838                              db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $edit['nid'], $edit['taxterm']);
839            }
840            if (module_exists('category')) {
841                      db_query('DELETE FROM {category_node} WHERE nid = %d and (cid = %d or cid = %d)',  $edit['nid'], $edit['cattermstart'], $edit['cattermend']);
842              db_query('INSERT INTO {category_node} (nid, cid) VALUES (%d, %d)', $edit['nid'], $edit['catterm']);
843              if (category_get_wrapper_status()){
844                              db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $edit['nid'], $edit['cattermstart'], $edit['cattermend']);
845                              db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $edit['nid'], $edit['catterm']);
846                      }
847        }
848            if (module_exists('product')) {
849               $node->nid = $edit['nid'];
850         $node->vid = $edit['nvid'];
851               $node->ptype = 'pay2publish';
852               $node->price = $edit['cost'];
853               $node->sku = $r->name;
854               $node->is_recurring = 1;
855             $node->price_interval = 0;
856               $node->price_unit = '';
857               $node->price_cycle = 0;
858               $node->auto_charge = 0;
859               $node->hide_cart_link = 1;
860               product_save($node);
861            }
862        module_invoke_all('pay2publish', 'update', 'term', $edit);
863        $status = SAVED_UPDATED;
864      }
865      else if ($edit['tid']){
866              if (module_exists('product')){
867               $node->nid = $edit['nid'];
868           $node->vid = $edit['nvid'];
869               product_delete($node);
870              }
871        return pay2publish_del_node($edit['tid']);
872      }
873      else {
874        $edit['tid'] = db_next_id('{pay2publish_node}_tid');
875        db_query("INSERT INTO {pay2publish_node} (tid, nid, vid, uid, name, act, duration_m, duration, cost, payment, weight) VALUES (%d, %d, %d, %d, '%s', '%s', %d, %d, '%s', %d, %d)", $edit['tid'], $edit['nid'], $edit['vid'], $edit['uid'], $edit['name'], $edit['act'], $edit['duration_m'], $edit['duration'], $edit['cost'], $edit['payment'], $edit['weight']);
876        if (module_exists('taxonomy') and !module_exists('category')) {
877            //      taxonomy_node_save($edit['nid'], array($r->taxtermend));
878                              db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $edit['nid'], $r->taxtermstart, $r->taxtermend);
879                              db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $edit['nid'], $r->taxtermend);
880            }
881            if (module_exists('category')) {
882              $edit[category][] = $r->cattermend;
883                      db_query('DELETE FROM {category_node} WHERE nid = %d and (cid = %d or cid = %d)',  $edit['nid'], $r->cattermstart, $r->cattermend);
884              db_query('INSERT INTO {category_node} (nid, cid) VALUES (%d, %d)', $edit['nid'], $r->cattermend);
885              if (category_get_wrapper_status()){
886                              db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $edit['nid'], $r->cattermstart, $r->cattermend);
887                              db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $edit['nid'], $r->cattermend);
888                      }
889            }
890            if (module_exists('product')){
891               $node->nid = $edit['nid'];
892           $node->vid = $edit['nvid'];
893               $node->ptype = 'pay2publish';
894               $node->price = $edit['cost'];
895               $node->sku = $r->name;
896               $node->is_recurring = 1;
897               $node->price_interval = 0;
898               $node->price_unit = '';
899               $node->price_cycle = 0;
900               $node->auto_charge = 0;
901               $node->hide_cart_link = 1;
902               product_save($node);
903            }
904        module_invoke_all('pay2publish', 'insert', 'term', $edit);
905        $status = SAVED_NEW;
906      }
907    
908      cache_clear_all();
909    
910      return $status;
911    }
912    
913    function pay2publish_confirm_delete_node($tid) {
914      $taxonomy_action = pay2publish_get_node($tid);
915    
916      $form['type'] = array('#type' => 'value', '#value' => 'pay2publish');
917      $form['tid'] = array('#type' => 'value', '#value' => $tid);
918      $form['name'] = array('#type' => 'value', '#value' => $taxonomy_action->name);
919      return confirm_form($form, t('Are you sure you want to delete the node %title?', array('%title' => $taxonomy_action->name)),
920                      'admin/pay2publish', t('Deleting a node. This action cannot be undone.'),
921                      t('Delete'),
922                      t('Cancel'));
923    }
924    
925    function pay2publish_confirm_delete_node_submit($form_id, $form_values) {
926      $status = pay2publish_del_node($form_values['tid']);
927      drupal_set_message(t('Deleted node %name.', array('%name' => $form_values['name'])));
928      return 'admin/pay2publish';
929    }
930    
931    function pay2publish_del_node($tid) {
932      $tids = array($tid);
933      $term = (array) pay2publish_get_node($tid);
934      $r = pay2publish_get_pay2publish($term->vid);
935      db_query('DELETE FROM {pay2publish_node} WHERE tid = %d', $tid);
936      if (module_exists('taxonomy') and !module_exists('category'))
937              db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $term->nid, $r->taxtermstart, $r->taxtermend);
938      if (module_exists('category')) {
939            db_query('DELETE FROM {category_node} WHERE nid = %d and (cid = %d or cid = %d)', $term->nid, $r->cattermend, $r->cattermstart);
940        if (category_get_wrapper_status()){
941                    db_query('DELETE FROM {term_node} WHERE nid = %d and (tid = %d or tid = %d)', $term->nid, $r->cattermend, $r->cattermstart);
942        }
943      }
944      module_invoke_all('pay2publish', 'delete', 'term', $term);
945      cache_clear_all();
946      return SAVED_DELETED;
947    }
948    
949    function _pay2publish_confirm_del_node($tid) {
950      $term = pay2publish_get_node($tid);
951    $pay2publish = pay2publish_get_pay2publish($term->vid);
952      $form['type'] = array('#type' => 'value', '#value' => 'term');
953      $form['name'] = array('#type' => 'value', '#value' => $term->name);
954      $form['level'] = array('#type' => 'value', '#value' => $pay2publish->name);
955      $form['tid'] = array('#type' => 'value', '#value' => $tid);
956      return confirm_form($form, 'pay2publish_node_confirm_delete',
957                      t('are you sure you want to cancel the '.$pay2publish->name.' level for the listing %title?',
958                      array('%title' => theme('placeholder', $term->name))),
959                      'admin/pay2publish',
960                      t(''),
961                      t('OK'),
962                      t('Cancel'));
963    }
964    
965    function pay2publish_node_confirm_delete_submit($form_id, $form_values) {
966      pay2publish_del_node($form_values['tid']);
967      drupal_set_message(t('Level '.$form_values['level'].' for listing %name canceled.', array('%name' => theme('placeholder', $form_values['name']))));
968      return 'admin/pay2publish';
969    }
970    
971    
972    function pay2publish_del_node_u($tid) {
973            global $user;
974    $term = pay2publish_get_node($tid);
975    $pay2publish = pay2publish_get_pay2publish($term->vid);
976      $form['type'] = array('#type' => 'value', '#value' => 'term');
977      $form['name'] = array('#type' => 'value', '#value' => $term->name);
978      $form['level'] = array('#type' => 'value', '#value' => $pay2publish->name);
979      $form['tid'] = array('#type' => 'value', '#value' => $tid);
980      return confirm_form($form, 'pay2publish_node_confirm_delete',
981                      t('are you sure you want to cancel the '.$pay2publish->name.' level for the listing %title?',
982                      array('%title' => theme('placeholder', $term->name))),
983                      $_GET['destination'] ? $_GET['destination'] : 'user/'. $user->uid,
984                      t(''),
985                      t('OK'),
986                      t('Cancel'));
987    }
988    
989    /**
990     * Return an array of all pay2publish objects.
991     *
992     * @param $type
993     *   If set, return only those actions associated with this node type.
994     */
995    function pay2publish_get_actions($type = NULL) {
996      if ($type) {
997        $result = db_query(db_rewrite_sql("SELECT v.vid, v.*, n.type FROM {pay2publish} v LEFT JOIN {pay2publish_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $type);
998      }
999      else {
1000        $result = db_query(db_rewrite_sql('SELECT v.*, n.type FROM {pay2publish} v LEFT JOIN {pay2publish_node_types} n ON v.vid = n.vid ORDER BY v.weight, v.name', 'v', 'vid'));
1001      }
1002    
1003      $actions = array();
1004      $node_types = array();
1005      while ($voc = db_fetch_object($result)) {
1006        $node_types[$voc->vid][] = $voc->type;
1007        unset($voc->type);
1008        $voc->nodes = $node_types[$voc->vid];
1009        $actions[$voc->vid] = $voc;
1010      }
1011    
1012      return $actions;
1013    }
1014    
1015    function pay2publish_get_action($vid = NULL) {
1016      $result = db_query(db_rewrite_sql("SELECT v.vid, v.*, n.type FROM {pay2publish} v LEFT JOIN {pay2publish_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name", 'v', 'vid'), $vid);
1017    
1018      if ($voc = db_fetch_object($result)) {
1019        return $voc;
1020      }
1021    
1022      return NULL;
1023    }
1024    
1025    /**
1026     * Return an array of all duration objects.
1027     *
1028     */
1029    function pay2publish_get_duration($vid = NULL) {
1030      if ($vid){
1031        return db_fetch_object(db_query('SELECT * FROM {pay2publish_duration} WHERE vid = %d', $vid));
1032      }else{
1033        $result = db_query(db_rewrite_sql('SELECT * FROM {pay2publish_duration} ORDER BY weight, name'));
1034        $actions = array();
1035        while ($voc = db_fetch_object($result)) {
1036          $actions[$voc->vid] = $voc;
1037        }
1038        return $actions;
1039      }
1040    }
1041    
1042    function pay2publish_get_duration_vid($vid = NULL) {
1043      if ($vid){
1044        return db_fetch_object(db_query("SELECT * FROM {pay2publish_duration} WHERE duration = '%s'", $vid));
1045      }
1046    }
1047    
1048    /**
1049     * Generate a form for selecting paid advertising to associate with a node.
1050     */
1051    function pay2publish_form_alter($form_id, &$form) {
1052      if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
1053        drupal_add_js(drupal_get_path('module', 'pay2publish') . '/pay2publish.js', 'module', 'header', FALSE);
1054        $node = $form['#node'];
1055    
1056        if (!isset($node->pay2publish)) {
1057          if ($node->nid) {
1058            $terms = pay2publish_node_get_nodes($node->nid);
1059          }
1060          else {
1061            $terms = array();
1062          }
1063        }
1064        else {
1065          $terms = $node->pay2publish;
1066        }
1067    
1068        $c = db_query(db_rewrite_sql("SELECT v.* FROM {pay2publish} v INNER JOIN {pay2publish_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
1069            $mes = '';
1070        while ($pay2publish = db_fetch_object($c)) {
1071          foreach ($terms as $term) {
1072            if ($term->vid == $pay2publish->vid) {
1073              $default_nodes = $term->vid;
1074            }
1075          }
1076                    $deftype[$pay2publish->vid] = $pay2publish->name.' (Cost of month:'.$pay2publish->cost.')';
1077                    if ($pay2publish->cost == 0) $def = $pay2publish->vid;
1078                    $mes .= $pay2publish->name.' (Cost of month:'.$pay2publish->cost.') <br>';
1079        }
1080    
1081            $durations = pay2publish_get_duration();
1082            $defdu[0] = 'Unlimited';
1083        foreach ($durations as $term) $defdu[$term->vid] = $term->name;
1084        if ($mes!=''){
1085            if ($node->nid) {
1086                $default_node = pay2publish_get_node_n_s($node->nid);
1087                    $default_node_vid = $default_node->vid;
1088                $default_durati = pay2publish_get_duration_vid($default_node->duration_m);
1089                    $default_duration = $default_durati->vid;
1090                }
1091          $form['advertising_actions'] = array(
1092            '#type' => 'fieldset',
1093            '#title' => t("Advertising"),
1094                    '#attributes' => array('class' => 'advertising_actions')
1095            );
1096          $form['advertising_actions']['pay2publish'] = array(
1097            '#type' => 'radios',
1098            '#title' => t('Paid advertising'),
1099            '#default_value' => $default_node_vid,
1100            '#options' => $deftype,
1101            '#required' => FALSE,
1102            '#attributes' => $default_node_vid ? array('disabled' => 'disabled'):array(),
1103            '#weight' => 0,
1104          );
1105          $form['advertising_actions']['pay2publish_duration'] = array(
1106            '#type' => 'radios',
1107            '#title' => t('Duration'),
1108            '#default_value' => $default_duration,
1109            '#options' => $defdu,
1110            '#required' => FALSE,
1111            '#attributes' => $default_node_vid ? array('disabled' => 'disabled'):array(),
1112              '#weight' => 0,
1113          );
1114          if (!$default_nodes) {
1115            $form['#attributes']['onclick'] = 'pay2publish_checkstate('.$def.')';
1116          }
1117        }
1118      }
1119    }
1120    
1121    
1122    
1123    /**
1124     * Find all nodes associated to the given node, ordered by pay2publish and node weight.
1125     */
1126    function pay2publish_node_get_nodes($nid, $key = 'tid') {
1127      static $terms;
1128    
1129      if (!isset($terms[$nid])) {
1130        $result = db_query('SELECT * FROM {pay2publish_node} WHERE nid = %d ORDER BY weight, name', $nid);
1131        $terms[$nid] = array();
1132        while ($term = db_fetch_object($result)) {
1133          $terms[$nid][$term->$key] = $term;
1134        }
1135      }
1136      return $terms[$nid];
1137    
1138    }
1139    
1140    /**
1141     * Save node associations for a given node.
1142     */
1143    function pay2publish_node_save($nid, $node) {
1144    
1145      if (is_numeric($nid) and is_numeric($node->pay2publish) and ($node->pay2publish != 0)){
1146        $pay2publish = pay2publish_get_pay2publish($node->pay2publish);
1147        $noden = pay2publish_get_node_n($nid, $node->pay2publish);
1148            if (is_numeric($noden->tid)){
1149          drupal_set_message($nodet->title." already exists");
1150            }else{
1151          $edit['nid'] = $nid;
1152          $edit['nvid'] = $node->vid;
1153          $edit['name'] = $node->title;
1154          $edit['uid'] = $node->uid;
1155          $edit['vid'] = $pay2publish->vid;
1156          $durations = pay2publish_get_duration(is_numeric($node->pay2publish_duration) ? $node->pay2publish_duration : 0);
1157              if ($durations->duration){
1158            $edit['duration'] = $durations->duration*31;
1159            $edit['duration_m'] = $durations->duration;
1160                $edit['cost'] = $pay2publish->cost*$durations->duration;
1161              }else{
1162            $edit['duration'] = $pay2publish->duration;
1163            $edit['duration_m'] = 0;
1164                $edit['cost'] = $pay2publish->cost;
1165              }
1166          if (module_exists('taxonomy') and !module_exists('category')) $rrt1 = taxonomy_get_term($pay2publish->taxtermend);
1167          if (module_exists('category')) $rrt2 = category_get_category($pay2publish->cattermend);
1168          if (($rrt1->name)or($rrt2->title))
1169                $edit['act'] = $rrt1->name.(($rrt1->name)?(($rrt2->title)?', ':'').$rrt2->title:$rrt2->title);
1170              else
1171                $edit['act'] = '';
1172              $edit['payment'] = 0;
1173              pay2publish_save_node($edit);
1174          drupal_set_message("Added ".$node->title);
1175        }
1176      }
1177    }
1178    
1179    /**
1180     * Remove node
1181     */
1182    function pay2publish_node_delete($nid) {
1183      db_query('DELETE FROM {pay2publish_node} WHERE nid = %d', $nid);
1184    }
1185    
1186    /**
1187     * Display form for adding and editing duration.
1188     */
1189    function pay2publish_form_duration($vid=NULL) {
1190      $edit = array();
1191      if($vid) {
1192        $edit = (array)pay2publish_get_duration($vid);
1193      }
1194    
1195      $form = array();
1196    
1197      $form['name'] = array('#type' => 'textfield',
1198        '#title' => t('Duration Name'),
1199        '#default_value' => $edit['name'],
1200        '#maxlength' => 64,
1201        '#description' => t('The name for this duration.  Example: "6 months" or "one year".'),
1202        '#required' => TRUE,
1203      );
1204      $form['description'] = array('#type' => 'textarea',
1205        '#title' => t('Description'),
1206        '#default_value' => $edit['description'],
1207        '#description' => t('Description of the duration (may be used by other modules).'),
1208      );
1209    
1210      $form['duration_months'] = array('#type' => 'textfield',
1211        '#title' => t('Duration in months'),
1212        '#default_value' => $edit['duration'],
1213        '#description' => t('Specify the duration in number of months. Example: "6" for 6 months.'),
1214        '#maxlength' => 255,
1215      );
1216    
1217      $form['weight'] = array('#type' => 'weight',
1218        '#title' => t('Weight'),
1219        '#default_value' => $edit['weight'],
1220        '#description' => t('Use weight to sort your durations.'),
1221      );
1222    
1223    
1224      $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
1225      if ($edit['vid']) {
1226        $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
1227        $form['vid'] = array('#type' => 'value', '#value' => $edit['vid']);
1228        $form['module'] = array('#type' => 'value', '#value' => $edit['module']);
1229      }
1230      return $form;
1231    }
1232    
1233    /**
1234     * Accept the form submission for a pay2publish and save the results.
1235     */
1236    function pay2publish_form_duration_submit($form_id, $form_values) {
1237      $op = $form_values['op'];
1238      $vid = $form_values['vid'];
1239      if($op == t('Delete')) {
1240        drupal_goto('admin/pay2publish/delete/duration/' . $vid);
1241      }
1242      switch (pay2publish_save_duration($form_values)) {
1243      case SAVED_NEW:
1244        drupal_set_message(t('Created new duration %name.', array('%name' => $form_values['name'])));
1245        break;
1246      case SAVED_UPDATED:
1247        drupal_set_message(t('Updated duration %name.', array('%name' => $form_values['name'])));
1248        break;
1249      }
1250      return 'admin/pay2publish';
1251    }
1252    
1253    function pay2publish_save_duration(&$edit) {
1254      $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
1255    
1256      if ($edit['vid'] && $edit['name']) {
1257        db_query("UPDATE {pay2publish_duration} SET name = '%s', description = '%s', duration = %d, weight = %d WHERE vid = %d", $edit['name'], $edit['description'], $edit['duration_months'], $edit['weight'], $edit['vid']);
1258        $status = SAVED_UPDATED;
1259      }
1260      else if ($edit['vid']) {
1261        $status = pay2publish_del_duration($edit['vid']);
1262      }
1263      else {
1264        $edit['vid'] = db_next_id('{pay2publish_duration}_vid');
1265        db_query("INSERT INTO {pay2publish_duration} (vid, name, description, duration, weight) VALUES (%d, '%s', '%s', %d, %d)", $edit['vid'], $edit['name'], $edit['description'], $edit['duration_months'], $edit['weight']);
1266        $status = SAVED_NEW;
1267      }
1268    
1269      cache_clear_all();
1270    
1271      return $status;
1272    }
1273    
1274    function pay2publish_delete_duration($vid) {
1275      db_query('DELETE FROM {pay2publish_duration} WHERE vid = %d', $vid);
1276      cache_clear_all();
1277      return SAVED_DELETED;
1278    }
1279    
1280    function pay2publish_confirm_delete_duration($vid) {
1281      $pay2publish = pay2publish_get_duration($vid);
1282    
1283      $form['type'] = array('#type' => 'value', '#value' => 'pay2publish');
1284      $form['vid'] = array('#type' => 'value', '#value' => $vid);
1285      $form['name'] = array('#type' => 'value', '#value' => $pay2publish->name);
1286      return confirm_form($form, t('Are you sure you want to delete the duration %title?', array('%title' => $pay2publish->name)),
1287                      'admin/pay2publish', t('Deleting a duration. This action cannot be undone.'),
1288                      t('Delete'),
1289                      t('Cancel'));
1290    }
1291    
1292    function pay2publish_confirm_delete_duration_submit($form_id, $form_values) {
1293      $status = pay2publish_delete_duration($form_values['vid']);
1294      drupal_set_message(t('Deleted duration %name.', array('%name' => $form_values['name'])));
1295      return 'admin/pay2publish';
1296    }
1297    
1298    /**
1299     * Return the pay2publish nodes matching a actions ID.
1300     */
1301    function pay2publish_get_pay2publish($vid) {
1302      static $actions = array();
1303    
1304      if (!array_key_exists($vid, $actions)) {
1305        $result = db_query('SELECT v.*, n.type FROM {pay2publish} v LEFT JOIN {pay2publish_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name', $vid);
1306        $node_types = array();
1307        while ($voc = db_fetch_object($result)) {
1308          $node_types[] = $voc->type;
1309          unset($voc->type);
1310          $voc->nodes = $node_types;
1311          $actions[$vid] = $voc;
1312        }
1313      }
1314    
1315      return $actions[$vid];
1316    }
1317    
1318    /**
1319     * Return the node object matching a  tid.
1320     */
1321    function pay2publish_get_node($tid) {
1322      return db_fetch_object(db_query('SELECT * FROM {pay2publish_node} WHERE tid = %d', $tid));
1323    }
1324    
1325    /**
1326     * Return the node object matching a nid and vid.
1327     */
1328    function pay2publish_get_node_n($nid, $vid) {
1329      return db_fetch_object(db_query('SELECT * FROM {pay2publish_node} WHERE nid = %d AND vid = %d', $nid, $vid));
1330    }
1331    
1332    /**
1333     * Return the node object matching a nid and vid.
1334     */
1335    function pay2publish_get_node_n_s($nid) {
1336      return db_fetch_object(db_query('SELECT * FROM {pay2publish_node} WHERE nid = %d ORDER BY weight, name', $nid));
1337    }
1338    
1339    /**
1340     * Return the node query result matching a uid.
1341     */
1342    function pay2publish_get_nodes_u($uid) {
1343      return db_query('SELECT * FROM {pay2publish_node} WHERE uid = %d', $uid);
1344    }
1345    
1346    
1347    /**