b2evolution PHP Cross Reference Blogging Systems

Source: /inc/newsletter/model/_newsletter.funcs.php - 157 lines - 4956 bytes - Summary - Text - Print

Description: This file implements newsletter functions. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements newsletter functions.

   4   *

   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.

   9   * Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.

  10   *

  11   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)

  12   *

  13   * {@internal Open Source relicensing agreement:

  14   * Daniel HAHLER grants Francois PLANQUE the right to license

  15   * Daniel HAHLER's contributions to this file and the b2evolution project

  16   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  17   * }}

  18   *

  19   * @package evocore

  20   *

  21   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  22   * @author fplanque: Francois PLANQUE.

  23   *

  24   * @version $Id: _newsletter.funcs.php 1022 2012-03-12 09:36:01Z yura $

  25   */
  26  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  27  
  28  
  29  /**

  30   * Get number of users for newsletter from UserList filterset

  31   *

  32   * @return array 

  33   *         'all' - Number of accounts in filterset

  34   *         'active' - Number of active accounts in filterset

  35   *         'newsletter' - Number of active accounts which accept newsletter email

  36   */
  37  function get_newsletter_users_numbers()
  38  {
  39      $numbers = array(
  40              'all'        => 0,
  41              'active'     => 0,
  42              'newsletter' => 0,
  43          );
  44  
  45      load_class( 'users/model/_userlist.class.php', 'UserList' );
  46      // Initialize users list from session cache in order to know number of users

  47      $UserList = new UserList( 'admin' );
  48      $UserList->memorize = false;
  49      $UserList->load_from_Request();
  50  
  51      $users_IDs = $UserList->filters['users'];
  52  
  53      if( count( $users_IDs ) )
  54      {    // Found users in the filterset
  55          global $DB;
  56  
  57          $numbers['all'] = count( $users_IDs );
  58  
  59          // Get number of all active users

  60          $SQL = new SQL();
  61          $SQL->SELECT( 'COUNT( * )' );
  62          $SQL->FROM( 'T_users' );
  63          $SQL->WHERE( 'user_ID IN ( '.implode( ', ', $users_IDs ).' )' );
  64          $SQL->WHERE_and( 'user_status IN ( \'activated\', \'autoactivated\' )' );
  65          $numbers['active'] = $DB->get_var( $SQL->get() );
  66  
  67          // Get number of all active users which accept newsletter email

  68          $SQL = get_newsletter_users_sql( $users_IDs );
  69          $SQL->SELECT( 'COUNT( * )' );
  70          $numbers['newsletter'] = $DB->get_var( $SQL->get() );
  71      }
  72  
  73      return $numbers;
  74  }
  75  
  76  
  77  /**

  78   * Get SQL for active users which accept newsletter email

  79   *

  80   * @param array users IDs

  81   * @return object SQL

  82   */
  83  function get_newsletter_users_sql( $users_IDs )
  84  {
  85      global $Settings;
  86  
  87      $SQL = new SQL();
  88      $SQL->SELECT( 'u.user_ID' );
  89      $SQL->FROM( 'T_users u' );
  90      $SQL->FROM_add( 'LEFT OUTER JOIN T_users__usersettings us ON u.user_ID = us.uset_user_ID' );
  91      $SQL->FROM_add( 'AND us.uset_name = \'newsletter_news\'' );
  92      $SQL->WHERE( 'u.user_ID IN ( '.implode( ', ', $users_IDs ).' )' );
  93      $SQL->WHERE_and( 'u.user_status IN ( \'activated\', \'autoactivated\' )' );
  94      if( $Settings->get( 'def_newsletter_news' ) )
  95      {    // If General setting "newsletter_news" = 1 we also should include all users without defined user's setting "newsletter_news"
  96          $SQL->WHERE_and( '( us.uset_value = 1 OR us.uset_value IS NULL )' );
  97      }
  98      else
  99      {    // If General setting "newsletter_news" = 0 we take only users which accept newsletter email
 100          $SQL->WHERE_and( 'us.uset_value = 1' );
 101      }
 102  
 103      return $SQL;
 104  }
 105  
 106  /**

 107   * Send newsletter emails

 108   */
 109  function newsletter_send()
 110  {
 111      global $DB, $Session;
 112  
 113      load_class( 'users/model/_userlist.class.php', 'UserList' );
 114      // Initialize users list from session cache in order to get users IDs for newsletter

 115      $UserList = new UserList( 'admin' );
 116      $UserList->memorize = false;
 117      $UserList->load_from_Request();
 118  
 119      $users_IDs = $UserList->filters['users'];
 120  
 121      // Get all active users which accept newsletter email

 122      $SQL = get_newsletter_users_sql( $users_IDs );
 123      $users = $DB->get_col( $SQL->get() );
 124  
 125      echo sprintf( T_('Newsletter is sending for %s users...'), count( $users ) ).'<br /><br />';
 126      evo_flush();
 127  
 128      $email_newsletter_params = array(
 129              'message' => $Session->get( 'newsletter_message' )
 130          );
 131  
 132      foreach( $users as $user_ID )
 133      {
 134          $UserCache = & get_UserCache();
 135          $User = $UserCache->get_by_ID( $user_ID );
 136  
 137          echo sprintf( T_('Email is sending for %s (%s)...'), $User->get_identity_link(), $User->get( 'email' ) ).' ';
 138  
 139          // Send a newsletter in user's locale

 140          locale_temp_switch( $User->get( 'locale' ) );
 141          $email_result = send_mail_to_User( $user_ID, $Session->get( 'newsletter_title' ), 'newsletter', $email_newsletter_params );
 142          locale_restore_previous();
 143  
 144          if( $email_result )
 145          {    // Success sending
 146              echo T_('OK');
 147          }
 148          else
 149          {    // Failed sending
 150              echo '<span class="red">'.T_('Failed').'</span>';
 151          }
 152          echo '<br />';
 153          evo_flush();
 154      }
 155  }
 156  
 157  ?>

title

Description

title

Description

title

Description

title

title

Body