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

Diff of /contributions/modules/mlm/mlm.module

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

revision 1.51, Mon Aug 10 01:57:46 2009 UTC revision 1.52, Sun Aug 23 23:06:27 2009 UTC
# Line 1  Line 1 
1  <?php // $Id: mlm.module,v 1.50 2009/08/10 01:09:32 vauxia Exp $  <?php // $Id: mlm.module,v 1.51 2009/08/10 01:57:46 vauxia Exp $
2    
3  /**  /**
4   * @file   * @file
# Line 34  function mlm_access($mlm = NULL, $op = ' Line 34  function mlm_access($mlm = NULL, $op = '
34  }  }
35    
36  /**  /**
37   * API Function: Include a handler file for a payment object.   * API Function: Include a handler file for a mailing list object.
38   */   */
39  function mlm_backend_load($name) {  function mlm_backend_load($name) {
40    static $cache;    static $cache;
# Line 103  function mlm_load($values = array()) { Line 103  function mlm_load($values = array()) {
103   * Implementation of hook_elements().   * Implementation of hook_elements().
104   */   */
105  function mlm_elements() {  function mlm_elements() {
106    $elements = array();    // An element representing a subscription form for one or more lists.
107    if (module_exists('send')) {    return array(
108      // Add send element to select from available mailing list(s)      'mlm_subscription' => array(
     $elements['mlm_send'] = array(  
109        '#input' => TRUE,        '#input' => TRUE,
110        '#process' => array('send_process'),        '#process' => array('mlm_element_process'),
111        '#after_build' => array('send_set_value'),        '#mlm_lists' => array(),
112      );        '#mlm_options' => array(),
113    }      ),
   return $elements;  
 }  
   
 /**  
  * A #process handler for the news_mlm form element.  
  */  
 function mlm_send_element_process($element, $edit, &$form_state, $form) {  
   foreach (mlm_lists('Announcement') as $list => $mlm) {  
     $options[$list] = $mlm->title;  
   }  
   if (isset($form_state['send']->lists)) {  
     $selected = $form_state['send']->lists;  
   }  
   elseif (isset($element['#value']['lists'])) {  
     $selected = $element['#value']['lists'];  
   }  
   else {  
     $selected = array();  
   }  
   $element['lists'] = array(  
     '#type'          => 'checkboxes',  
     '#title'         => t('Mailing lists'),  
     '#options'       => $options,  
     '#default_value' => $selected,  
114    );    );
   return $element;  
115  }  }
116    
117  /**  /**
118   * An #after_build handler for a the news_mlm element.   * A #process handler for the mlm form element.
  * Set the element value in a format that will be recognized by send_contact.  
119   */   */
120  function mlm_send_element_set_value($element, &$form_state) {  function mlm_element_process($element, $edit, &$form_state, $form) {
121    $lists = array();    module_load_include('subscription.inc', 'mlm', 'include/mlm');
122    if ($element['#value']) {    return mlm_subscription_element_process($element, $edit, $form_state, $form);
     foreach ($element['#value']['lists'] as $lid => $status) {  
       if ($status) $lists[] = array('lid' => $lid);  
     }  
   }  
   $element['#value'] = $lists;  
   return $element;  
123  }  }
124    
125  /**  /**
126   * Implementation of hook_user().   * Implementation of hook_user().
127   */   */
128  function mlm_user($op, &$edit, &$user, $category = NULL) {  function mlm_user($op, &$edit, &$account, $category = NULL) {
   if (!user_access('subscribe to mailing lists')) return;  
   
129    switch ($op) {    switch ($op) {
130    
     case 'form':  
       if ($category == 'mlm') {  
         //return mlm_form($user);  
       }  
       return;  
   
131      case 'update':      case 'update':
132        // todo - if new address, make appropriate changes        // todo - if new address, make appropriate changes
133        return;        return;
# Line 178  function mlm_user($op, &$edit, &$user, $ Line 137  function mlm_user($op, &$edit, &$user, $
137        return;        return;
138    
139      case 'categories':      case 'categories':
140        //if (mlm_lists(NULL, NULL, $user)) {        return array(
141          //return array(array('name' => 'mlm', 'title' => t('Mailing Lists'), 'weight' => 10));          array(
142        //}            'name' => 'mlm',
143              'title' => t('Mailing lists'),
144              'weight' => 10,
145              'access callback' => 'user_access',
146              'access arguments' => array('subscribe to mailing lists'),
147            ),
148          );
149    
150        case 'form':
151          if ($category == 'mlm') {
152            module_load_include('subscription.inc', 'mlm', 'include/mlm');
153            return mlm_subscription_page(array(), $account);
154          }
155          return;
156    }    }
157  }  }
158    
# Line 239  function mlm_block($op = 'list', $delta Line 211  function mlm_block($op = 'list', $delta
211      case 'view':      case 'view':
212        if ($delta == 0) {        if ($delta == 0) {
213          if (!user_access('subscribe to mailing lists')) return;          if (!user_access('subscribe to mailing lists')) return;
214          $lists = array();  
215          foreach (variable_get('mlm_block_lists', array()) as $lid => $status) {          $lists = array_filter(variable_get('mlm_block_lists', array()));
216            if ($status) $lists[$lid] = mlm_load($lid);          if (variable_get('mlm_block_hide', '')) {
217              foreach($lists as $key => $mlm) {
218                $mlm = mlm_load($mlm);
219                if ($mlm->_is_subscribed()) unset($lists[$key]);
220              }
221          }          }
222          if ($lists) {          if (!$lists) return;
223            global $user;  
224            $help = variable_get('mlm_block_helptext', '');          return array(
225            $all = !variable_get('mlm_block_hide', '');            'subject' => t('Subscribe to list'),
226            return array(            'content' => drupal_get_form('mlm_block_form', $lists),
227              'subject' => t('Subscribe to list'),          );
             'content' => drupal_get_form('mlm_subscription_form', $user, $lists, $help, TRUE),  
           );  
         };  
228        }        }
229        return;        return;
230    }    }
231  }  }
232    
233  /**  /**
234     * Form handler for the MLM block.
235     */
236    function mlm_block_form($form_state, $lists) {
237      $form = array();
238    
239      $form['mlm'] = array(
240        '#type' => 'mlm_subscription',
241        '#mlm_lists' => $lists,
242        '#mlm_settings' => array(
243          'helptext' => variable_get('mlm_block_helptext', ''),
244          'only_unsubscribed' => variable_get('mlm_block_hide', ''),
245          'show_mail' => TRUE,
246        ),
247      );
248      $form['mlm']['submit'] = array(
249        '#type' => 'submit',
250        '#value' => variable_get('mlm_block_button', t('Subscribe')),
251        '#weight' => 10,
252      );
253      return $form;
254    }
255    
256    /**
257   * Implementation of hook_theme().   * Implementation of hook_theme().
258   */   */
259  function mlm_theme() {  function mlm_theme() {
# Line 290  function mlm_mimemail_incoming($mail) { Line 286  function mlm_mimemail_incoming($mail) {
286  }  }
287    
288  /**  /**
289   * The subscription block form for one or more MLM lists.   * API Function: return information about mlm backends.
290   */   */
291  function mlm_subscription_form($form_state, $account = NULL, $lists = array(), $help = '', $all = TRUE, $short = FALSE) {  function mlm_backends($name = NULL) {
292    $form = array();    static $backends;
293      if (!isset($backends)) {
294    // We're only showing lists we haven't subscribed to.      module_load_include('backends.inc', 'mlm', 'backend/mlm');
295    if (!$all) {      $backends = mlm_backend_info();
     foreach ($lists as $key => $mlm) {  
       if ($mlm->_is_subscribed()) unset($lists[$key]);  
     }  
   }  
   foreach ($lists as $mlm) {  
     $form = array_merge($form, $mlm->subscribe_form());  
   }  
   if (!$form) return;  
   
   if ($help) {  
     $form['helptext'] = array(  
       '#type' => 'markup',  
       '#value' => $help,  
       '#weight' => -5,  
     );  
   }  
   if (count($lists) == 1) {  
     $mlm = current($lists);  
     $form['mlm_lists'] = array(  
       '#type' => 'value',  
       '#value' => $lists,  
     );  
   
     if ($form) {  
       $form['mlm_lists'] = array(  
         '#type' => 'value',  
         '#value' => array($mlm->lid => !$mlm->_is_subscribed($account))  
       );  
   
       $form['submit'] = array(  
         '#type' => 'submit',  
         '#value' => $mlm->_is_subscribed($account) ? t('Unsubscribe from this list') : t('Subscribe to this list'),  
       );  
     }  
   }  
   else {  
     $form['mlm_lists'] = array(  
       '#type' => 'checkboxes',  
     );  
     foreach ($lists as $mlm) {  
       $form['mlm_lists'][$mlm->lid] = array(  
         '#type' => 'checkbox',  
         '#title' => l($mlm->title, $mlm->path()),  
         '#description' => $short ? NULL : $mlm->description(),  
         '#default_value' => (int) $mlm->_is_subscribed($account),  
         '#attributes' => array('class' => 'mlm-list'),  
       );  
     }  
   
     $form['submit'] = array('#type' => 'submit', '#value' => t('Subscribe'));  
   }  
   if ($form) {  
     $form['#submit'][] = 'mlm_subscription_form_submit';  
     $form['#validate'][] = 'mlm_subscription_form_validate';  
296    }    }
297    return $form;    return $backends;
298  }  }
299    
300  /**  /**
301   * Validate hook for a subscription form.   * API Function: return a name/title array of mlm backends.
302   */   */
303  function mlm_subscription_form_validate($form, &$form_state) {  function mlm_backend_names($all = FALSE) {
304    if (!valid_email_address($form_state['values']['mlm_mail'])) {    $names = array();
305      form_set_error('mlm_mail', t('%mail is not a valid email address', array('%mail' => $form_state['values']['mlm_mail'])));    $enabled = array_filter(variable_get('mlm_backends', array()));
306      foreach(mlm_backends() as $name => $info) {
307        if ($all || isset($enabled[$name])) $names[$name] = $info['title'];
308    }    }
309      return $names;
310  }  }
311    
312  /**  /**
313   * All-purpose callback to save a list of subscriptions preferences for   * API Function: return information about mlm list types.
  * one or more lists.  Called from the list node form, block, and mlm page.  
314   */   */
315  function mlm_subscription_form_submit($form, &$form_state) {  function mlm_list_types($name = NULL) {
316    $account = NULL;    static $types;
317    $notify = TRUE;    if (!isset($types)) {
318        module_load_include('backends.inc', 'mlm', 'backend/mlm');
319    if (isset($form_state['values']['mlm_mail'])) {      $types = mlm_list_type_info();
     $account = $form_state['values']['mlm_mail'];  
   }  
   elseif (isset($form_state['values']['mail'])) {  
     $account = $form_state['values']['mail'];  
   }  
   
   foreach ($form_state['values']['mlm_lists'] as $lid => $val) {  
     $list = mlm_load($lid);  
     if (!$val) {  
       $list->_unsubscribe($account, $type, $notify);  
     }  
     elseif ($val) {  
       $list->_subscribe($account, $type, $notify);  
     }  
320    }    }
321      return $types;
322  }  }
323    
324  function mlm_backends($name = NULL) {  /**
325    static $backends;   * API Function: return a name/title array of mlm list types.
326    if (!isset($backends)) {   */
327      module_load_include('backends.inc', 'mlm', 'backend/mlm');  function mlm_list_type_names() {
328      $backends = mlm_backend_info();    $names = array();
329    }    foreach (mlm_list_types as $name => $info) $names[$name] = $info['title'];
330    return $backends;    return $names;
331  }  }
332    
333  /**  /**
# Line 445  function mlm_lists($list_type = NULL, $b Line 376  function mlm_lists($list_type = NULL, $b
376    }    }
377    return $lists;    return $lists;
378  }  }
   
 /**  
  * Implementation of Send module's hook_send_mode_info().  
  * Provide a "mailing list" delivery mode.  
  */  
 function mlm_send_mode_info() {  
   return array(  
     'mlm' => array(  
       'callback' => 'mlm_send_deliver',  
       'database columns' => array(  
         'lid' => array('type' => 'int', 'unsigned' => TRUE),  
       ),  
       'unique keys' => array('lid' => array('lid')),  
       'user only' => FALSE,  
     ),  
   );  
 }  
   
 /**  
  * Delivery callback for MLM module's send handler.  
  */  
 function mlm_send_deliver($profile, $contact, $nids, &$message, $mode) {  
   if ($mlm = mlm_load($contact->lid)) {  
     $mlm->post($message);  
   }  
 }  

Legend:
Removed from v.1.51  
changed lines
  Added in v.1.52

  ViewVC Help
Powered by ViewVC 1.1.2