/[drupal]/contributions/modules/mlm/include/mlm.inc
ViewVC logotype

Diff of /contributions/modules/mlm/include/mlm.inc

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

revision 1.10, Mon Aug 10 01:57:46 2009 UTC revision 1.11, Sun Aug 23 23:06:28 2009 UTC
# Line 1  Line 1 
1  <?php // $Id: mlm.inc,v 1.9 2009/08/10 01:09:32 vauxia Exp $  <?php // $Id: mlm.inc,v 1.10 2009/08/10 01:57:46 vauxia Exp $
2    
3  class mlm {  class mlm {
4    // The supported list types ( announcement, discussion, etc. ).    // The supported list types ( announcement, discussion, etc. ).
# Line 25  class mlm { Line 25  class mlm {
25    // An array of settings/options.    // An array of settings/options.
26    // var $settings = array();    // var $settings = array();
27    
28      // An array of settings/options.
29      // var $text = array();
30    
31    function __construct($values = NULL) {    function __construct($values = NULL) {
32      $current = (array) $this;      $current = (array) $this;
33      if ($values) {      if ($values) {
34        foreach ($values as $name => $val) {        foreach ($values as $name => $val) {
35          if ($name == 'settings' && !empty($val)) {          if ($name == 'settings' && !empty($val) && is_scalar($val)) {
36            $this->__construct(unserialize($val));            $this->__construct(unserialize($val));
37          }          }
38          elseif (array_key_exists($name, $current) && !is_null($val)) {          elseif (array_key_exists($name, $current) && !is_null($val)) {
# Line 63  class mlm { Line 66  class mlm {
66      }      }
67    }    }
68    
69      function text_options() {
70        return array(
71          'notify_subscribe' => array(
72            'title' => t('Send a "Welcome" message'),
73            'subject' => TRUE,
74            'body' => TRUE,
75            'format' => TRUE,
76          ),
77          /*
78          'confirm_subscribe' => array(
79            'subject' => TRUE,
80            'body' => TRUE,
81            'format' => TRUE,
82          ),
83          'confirm_unsubscribe' => array(
84            'subject' => TRUE,
85            'body' => TRUE,
86            'format' => TRUE,
87          ),*/
88          'notify_unsubscribe' => array(
89            'title' => t('Send a "Goodbye" message'),
90            'subject' => TRUE,
91            'body' => TRUE,
92            'format' => TRUE,
93          ),
94          'header' => array(
95            'title' => t('Add a header to outgoing messages'),
96            'subject' => FALSE,
97            'body' => TRUE,
98            'format' => TRUE,
99          ),
100          'footer' => array(
101            'title' => t('Add a footer to outgoing messages'),
102            'subject' => FALSE,
103            'body' => TRUE,
104            'format' => TRUE,
105          ),
106        );
107      }
108    
109    function set_backend() {    function set_backend() {
110      if (!isset($this->backend)) $this->backend = get_class($this);      if (!isset($this->backend)) $this->backend = get_class($this);
111    }    }
# Line 80  class mlm { Line 123  class mlm {
123      return check_plain($this->title);      return check_plain($this->title);
124    }    }
125    
126      function set_supported_types($types = array('announcement', 'discussion')) {
127        if (!$this->supported_types) {
128          $info = mlm_list_types();
129          $this->supported_types = array();
130          foreach ($types as $name) {
131            $this->supported_types[$name] = $info[$name]['title'];
132          }
133        }
134      }
135    
136      function supported_types($name = NULL) {
137        $this->set_supported_types();
138        if ($name) return $this->supported_types[$name];
139        return $this->supported_types;
140      }
141    
142    function set_list_type($val = NULL) {    function set_list_type($val = NULL) {
143      if (in_array($val, $this->supported_types())) {      if ($this->supported_types($val)) {
144        $this->list_type = $val;        $this->list_type = $val;
145      }      }
146    }    }
# Line 100  class mlm { Line 159  class mlm {
159      return filter_xss($this->incoming_address);      return filter_xss($this->incoming_address);
160    }    }
161    
   function supported_types() {  
     $types = array();  
     foreach ($this->supported_types as $type) {  
       $types[$type] = t(ucwords($type));  
     }  
     return $types;  
   }  
   
162    function set_description($text = NULL) {    function set_description($text = NULL) {
163      $this->description = filter_xss($text);      $this->description = filter_xss($text);
164    }    }
# Line 117  class mlm { Line 168  class mlm {
168        $node = node_load($this->nid);        $node = node_load($this->nid);
169        return $node->teaser;        return $node->teaser;
170      }      }
171        if ($description = $this->settings('description')) {
172          return check_markup($description, $this->settings('format'));
173        }
174    }    }
175    
176    function set_settings($settings = array()) {    function set_settings($settings = array()) {
# Line 135  class mlm { Line 189  class mlm {
189      }      }
190    }    }
191    
192      function settings($name = NULL) {
193        $this->set_settings();
194        if ($name) return $this->settings[$name];
195        return $this->settings;
196      }
197    
198      function text($name = NULL) {
199        // Load the text value.
200        if (!isset($this->text[$name])) {
201          $res = db_query("SELECT mtid, lid, list_type, subject, body, format
202            FROM {mlm_text}
203            WHERE name = '%s'
204            AND (lid = %d)
205            OR (lid IS NULL AND (list_type = '%s' OR list_type IS NULL))
206            ORDER BY (lid = %d) DESC, (list_type = '%s') DESC LIMIT 1",
207            $name, $this->lid, $this->list_type, $this->lid, $this->list_type);
208          $this->text[$name] = db_fetch_object($res);
209        }
210        return $this->text[$name];
211      }
212    
213      function token_list($type = 'all') {
214        if ($type == 'mlm' || $type = 'all') {
215          $tokens['mlm']['title'] = t('List name');
216          $tokens['mlm']['list_type'] = t('List type');
217          $tokens['mlm']['description'] = t('List description');
218          $tokens['mlm']['path'] = t('Subscription URL');
219        }
220      }
221    
222      function token_values($type, $object = NULL) {
223        if ($type == 'mlm') {
224          $tokens['title'] = $this->title();
225          $tokens['list_type'] = $this->list_type();
226          $tokens['description'] = $this->description();
227          $tokens['path'] = $this->path();
228    
229          return $tokens;
230        }
231      }
232    
233    function path() {    function path() {
234      if ($this->nid) {      if ($this->nid) {
235        return 'node/'. $this->nid;        return 'node/'. $this->nid;
# Line 189  class mlm { Line 284  class mlm {
284        '#rows' => 20,        '#rows' => 20,
285        '#title' => t('Add subscribers manually'),        '#title' => t('Add subscribers manually'),
286      );      );
287      if ($this->mlm_text->welcome) {      if ($this->settings('notify_subscribe')) {
288        $form['notify'] = array(        $form['notify'] = array(
289          '#type' => 'checkbox',          '#type' => 'checkbox',
290          '#title' => t('Send Welcome E-mail'),          '#title' => t('Send Welcome E-mail'),
# Line 205  class mlm { Line 300  class mlm {
300        $text = preg_replace(array('/\s/ms', '/,+/'), ',', $text);        $text = preg_replace(array('/\s/ms', '/,+/'), ',', $text);
301        foreach (explode(',', $text) as $mail) {        foreach (explode(',', $text) as $mail) {
302          if (valid_email_address($mail)) {          if (valid_email_address($mail)) {
303            $this->_subscribe($mail, 'subscriber', $values['notify']);            $this->_subscribe($mail, array(), $values['notify']);
304          }          }
305          else {          else {
306            drupal_set_message(t('%mail is not a valid email address', array('%mail' => $mail)), 'error');            drupal_set_message(t('%mail is not a valid email address', array('%mail' => $mail)), 'error');
# Line 269  class mlm { Line 364  class mlm {
364        $form['nid'] = array('#type' => 'value', '#value' => $node->nid);        $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
365      }      }
366    
367      // TODO      $form['title'] = array(
368          '#type' => 'textfield',
369          '#title' => t('List name'),
370          '#default_value' => $this->title,
371          '#weight' => -10,
372        );
373    
374      $types = $this->supported_types();      $types = $this->supported_types();
375      $form['list_type'] = array(      $form['list_type'] = array(
376        '#type' => 'select',        '#type' => 'select',
# Line 278  class mlm { Line 379  class mlm {
379        '#default_value' => $this->list_type ? $this->list_type : current($types) ,        '#default_value' => $this->list_type ? $this->list_type : current($types) ,
380        '#access' => count($types) > 1,        '#access' => count($types) > 1,
381      );      );
382    
383        $form['description']['description'] = array(
384          '#type' => 'textarea',
385          '#title' => t('Description'),
386          '#default_value' => $this->settings('description'),
387        );
388        $form['description']['description_format'] = filter_form($this->settings('description_format'), NULL, array('description_format'));
389    
390      $form['incoming_address'] = array('#type' => 'value', '#value' => '');      $form['incoming_address'] = array('#type' => 'value', '#value' => '');
391    
392        $form['text'] = array();
393        foreach($this->text_options() as $name => $info) {
394          $text = $this->text($name);
395          $default = !$text->lid;
396          $form['text'][$name] = array(
397            '#type' => 'checkbox',
398            '#title' => $info['title'],
399            '#description' => $info['description'],
400            '#default_value' => $this->settings($name),
401          );
402          $form['text'][$name. '_text'] = array( '#tree' => TRUE );
403          if ($info['subject']) {
404           $form['text'][$name .'_text']['subject'] = array(
405              '#type' => 'textfield',
406              '#title' => t('Subject'),
407              '#default_value' => $default ? NULL : $text->subject,
408            );
409          }
410          if ($info['body']) {
411           $form['text'][$name .'_text']['body'] = array(
412              '#type' => 'textarea',
413              '#title' => t('body'),
414              '#default_value' => $default ? NULL : $text->body,
415            );
416           $format = $default ? FILTER_FORMAT_DEFAULT : $text->format;
417           if (is_null($format)) $format = FILTER_FORMAT_DEFAULT;
418           $form['text'][$name .'_text'][$name .'_format'] = filter_form($format, NULL, array($name .'_text', $name .'_format'));
419          }
420        }
421        if ($form['text']){
422          $form['text']['#type'] = 'fieldset';
423          $form['text']['#title'] = t('Instructions and help text');
424          $form['text']['#collapsible'] = TRUE;
425        }
426    
427      // Call the API's edit_form function to add additional fields.      // Call the API's edit_form function to add additional fields.
428      if (method_exists($this, 'edit_form')) {      if (method_exists($this, 'edit_form')) {
429        $form = array_merge($edit_form, $this->edit_form($form));        $form = array_merge($form, $this->edit_form($form));
430      }      }
431    
432      return $form;      return $form;
# Line 308  class mlm { Line 452  class mlm {
452    //}    //}
453    
454    function _edit_form_submit(&$form, &$form_state, &$values) {    function _edit_form_submit(&$form, &$form_state, &$values) {
455        // Convert text settings to storable values.
456        foreach($this->text_options() as $name => $info) {
457          if ($values[$name]) {
458            $text = $values[$name .'_text'];
459            $this->text[$name] = array(
460              'subject' => filter_xss($text['subject']),
461              'body' => filter_xss($text['body']),
462              'format' => filter_xss($text[$name .'_format']),
463            );
464          }
465          // Remove it from the rest of the processing.
466          unset ($values[$name .'_text']);
467        }
468    
469      // Re-populate this class with new values, if any.      // Re-populate this class with new values, if any.
470      $this->__construct($values);      $this->__construct($values);
471    
# Line 361  class mlm { Line 519  class mlm {
519        $this->lid = db_last_insert_id('mlm', 'lid');        $this->lid = db_last_insert_id('mlm', 'lid');
520      }      }
521    
522        // Store text values for this list.
523        foreach ($this->text_options() as $name => $info) {
524          if ($this->settings[$name]) {
525            $value = (object) $this->text[$name];
526            $value->name = $name;
527            $value->lid = $this->lid;
528            $value->list_type = $this->list_type;
529    
530            $update = NULL;
531            if ($mtid = db_result(db_query("SELECT mtid FROM {mlm_text}
532              WHERE lid = %d AND name = '%s'", $this->lid, $name))) {
533              $value->mtid = $mtid;
534              $update = 'mtid';
535            }
536            drupal_write_record('mlm_text', $value, $update);
537          }
538          else {
539            db_query("DELETE FROM {mlm_text}
540              WHERE name = '%s' AND lid = %d", $name, $this->lid);
541          }
542        }
543    
544      // Store settings for this list.      // Store settings for this list.
545      $filter = array('form_id', 'form_build_id', 'form_token', 'op', 'submit');      $filter = array('form_id', 'form_build_id', 'form_token', 'op', 'submit');
546      foreach ($this->settings as $name => $value) {      foreach ($this->settings as $name => $value) {
# Line 385  class mlm { Line 565  class mlm {
565      db_query("DELETE FROM {mlm} WHERE lid = %d", $this->lid);      db_query("DELETE FROM {mlm} WHERE lid = %d", $this->lid);
566      db_query("DELETE FROM {mlm_setting} WHERE lid = %d", $this->lid);      db_query("DELETE FROM {mlm_setting} WHERE lid = %d", $this->lid);
567      db_query("DELETE FROM {mlm_slog} WHERE lid = %d", $this->lid);      db_query("DELETE FROM {mlm_slog} WHERE lid = %d", $this->lid);
568        db_query("DELETE FROM {mlm_text} WHERE lid = %d", $this->lid);
569    
570      // Make sure this list isn't appearing in the subscription block.      // Make sure this list isn't appearing in the subscription block.
571      $block = variable_get('mlm_block_lists', array());      $block = variable_get('mlm_block_lists', array());
# Line 395  class mlm { Line 576  class mlm {
576    function delete() {    function delete() {
577    }    }
578    
579    function subscribe_form() {    function subscribe_form(&$form_state, $settings, $account = NULL, $lists = array()) {
580      $form['mlm_mail'] = array(      $account = $this->account($account);
581        '#type' => 'textfield',      if (!$settings['show_mail'] && $account->uid) {
582        '#default_value' => $account->mail ? $account->mail : t('email address'),        $form['mlm_mail'] = array(
583        '#size' => 20,          '#type' => 'value',
584        '#attributes' => array('onclick' => "this.value='';"),          '#value' => $account->mail,
585        '#maxlength' => 80        );
586        }
587        else {
588          $form['mlm_mail'] = array(
589            '#type' => 'textfield',
590            '#default_value' => $account->mail ? $account->mail : t('email address'),
591            '#size' => 20,
592            '#attributes' => array('onclick' => "this.value='';"),
593            '#maxlength' => 80
594          );
595        }
596        $form['subscribe_'. $this->lid] = array(
597          '#type' => 'checkbox',
598          '#title' => $this->title(),
599          '#default_value' => $this->is_subscribed(),
600          '#description' => $this->description(),
601      );      );
602      return $form;      return $form;
603    }    }
604    
605    function subscribe_validate() {    function subscribe_validate($element, $form_state) {
606        if (isset($element['#value']['mlm_mail'])) {
607          if (!valid_email_address($element['#value']['mlm_mail'])) {
608            form_set_error('mlm_mail', t('%mail is not a valid email address', array('%mail' => $form_state['values']['mlm_mail'])));
609          }
610        }
611    }    }
612    
613    /**    /**
   
   /**  
614     * API call to subscribe a user to a list.     * API call to subscribe a user to a list.
615     *     *
616     * @param $accounts     * @param $accounts
617     *   The email addresses or user objects to subscribe.     *   The email addresses or user objects to subscribe.
618     * @param $type     * @param $values
619     *   The subscription type ( e.g. "subscriber", "moderator" )     *   Subscription settings, such as type or other values.
620     * @param $notify     * @param $notify
621     *   Whether or not to send a welcome message.     *   Whether or not to send a welcome message.
622     * @return     * @return
623     *   Boolean result of the subscribe request.     *   Boolean result of the subscribe request.
624     */     */
625    function _subscribe($accounts = array(), $type = NULL, $notify = FALSE) {    function _subscribe($accounts = array(), $values = array(), $notify = TRUE) {
626      if (!is_array($accounts)) $accounts = array($accounts);      if (!is_array($accounts)) $accounts = array($accounts);
627      foreach ($accounts as $account) {      foreach ($accounts as $account) {
628        $account = $this->account($account);        $account = $this->account($account);
# Line 435  class mlm { Line 634  class mlm {
634    
635        // Handle the actual subscription request        // Handle the actual subscription request
636        if (method_exists($this, 'subscribe')) {        if (method_exists($this, 'subscribe')) {
637          $this->subscribe();          $this->subscribe($values);
638        }        }
639    
640        // Send the user a notification, if desired and available.        // Send the user a notification, if desired and available.
641        if ($notify && method_exists($this, 'notify')) {        if ($notify) {
642          $this->notify('subscribe', $account);          $this->notify('subscribe', $account);
643        }        }
644    
# Line 460  class mlm { Line 659  class mlm {
659     * @return     * @return
660     *   Boolean result of the unsubscribe request.     *   Boolean result of the unsubscribe request.
661     */     */
662    function _unsubscribe($accounts = array(), $notify = FALSE) {    function _unsubscribe($accounts = array(), $values = array(), $notify = TRUE) {
663      if (!is_array($accounts)) $accounts = array($accounts);      if (!is_array($accounts)) $accounts = array($accounts);
664      foreach ( $accounts as $account) {      foreach ( $accounts as $account) {
665        $account = $this->account($account);        $account = $this->account($account);
666    
667        // Handle the actual subscription request        // Handle the actual subscription request
668        if (method_exists($this, 'unsubscribe')) {        if (method_exists($this, 'unsubscribe')) {
669          $this->unsubscribe();          $this->unsubscribe($values);
670        }        }
671    
672        // Send the user a notification, if desired and available.        // Send the user a notification, if desired and available.
673        if ($notify && method_exists($this, 'notify')) {        if ($notify) {
674          $this->notify('unsubscribe', $account);          $this->notify('unsubscribe', $account);
675        }        }
676    
# Line 482  class mlm { Line 681  class mlm {
681      }      }
682    }    }
683    
684    function notify($op = 'subscribe', $account) {    function notify($op, $account) {
685      if ($body = $this->mlm_text->goodbye) {      if ($text = $this->text('notify_'. $op)) {
686        mimemail(NULL, $account, t('Unubscribed from "@list"', array('@list' => $this->title)), $body);        mimemail(NULL, $account, $text->subject, $text->body);
687      }      }
688    }    }
689    
# Line 507  class mlm { Line 706  class mlm {
706    /**    /**
707     * API call to return a list of subscribers to a list.     * API call to return a list of subscribers to a list.
708     *     *
    * @param $type  
    *   The subscription type ( e.g. "subscriber", "moderator" )  
709     * @param $filter     * @param $filter
710     *   An array of filters to the subscription list.     *   An array of filters to the subscription list.
711     * @return     * @return
# Line 566  class mlm { Line 763  class mlm {
763     *  The status of the archive command.     *  The status of the archive command.
764     */     */
765    function archive($message = array()) {    function archive($message = array()) {
766      // Log traffic via send if it's available.      // Log traffic via send if it's available. TODO still in use?
767      if ( function_exists('send_status') ) {      if ( function_exists('send_status') ) {
768        send_status('mlm');        send_status('mlm');
769      }      }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11

  ViewVC Help
Powered by ViewVC 1.1.2