qa-email-notifications-event.php 14.4 KB
Newer Older
Amiya Sahu's avatar
Amiya Sahu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
<?php

/*
  Amiya Sahu

  File: qa-plugin/qa-category-email-notifications/qa-category-email-notifications-event.php
  Version: 0.9
  Date: 2013-02-21
  Description: Event module class for category email notifications plugin
 */
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
      header('Location: ../../');
      exit;
}
define('EMAIL_NOTF_PLUGIN_DIR', __DIR__);

class qa_email_notifications_event {

      public $log_file_name, $log_file_exists, $log_file;

      function process_event($event, $userid, $handle, $cookieid, $params) {
            if ($this->plugin_enabled_from_admin_panel()) {  //proceed only if the plugin is enabled 
                  require_once QA_INCLUDE_DIR . 'qa-app-emails.php';
                  require_once QA_INCLUDE_DIR . 'qa-app-format.php';
                  require_once QA_INCLUDE_DIR . 'qa-util-string.php';
                  switch ($event) {
                        case 'q_post':

                              $categoryid = $this->qa_get($params, 'categoryid');
                              $tags       = $this->qa_get($params, 'tags');
                              $emails     = qa_db_select_with_pending($this->qa_db_notificaton_emails_selectspec(qa_get_logged_in_userid(), $tags, $categoryid));
                              $emails     = $this->combine_emails($emails);

                              for ($i = 0; $i < count($emails); $i++) {
                                    $bcclist = array();
                                    for ($j = 0; $j < 75 && $i < count($emails); $j++, $i++) {
                                          $bcclist[] = $emails[$i]['email'];
                                    }

                                    $this->category_email_notification_send_notification($bcclist, null, null, qa_lang('emails/q_posted_subject'), qa_lang('notify/q_posted_body'), 
                                        array(
                                        '^q_handle'  => isset($handle) ? $handle : qa_lang('main/anonymous'),
                                        '^q_title'   => $params['title'], // don't censor title or content here since we want the admin to see bad words
                                        '^q_content' => $params['text'],
                                        '^url'       => qa_q_path($params['postid'], $params['title'], true),
                                        '^site_url'  => qa_opt("site_url"),
                                            )
                                    );
                              }
                              break;
                  } //switch 
            }//if 
      }

      function qa_db_notificaton_emails_selectspec($userid, $tags, $categoryid) {
            if ($this->plugin_enabled_from_admin_panel()) {  //proceed only if the plugin is enabled
                  require_once QA_INCLUDE_DIR . 'qa-app-updates.php';

                  $source = '';
                  $arguments = array();
                  if (!!qa_opt('ami_email_notf_allow_user_follower')) {
                        $source    .= (!!$source) ? ' UNION ' : '';
                        $source    .= "( SELECT ^users.email , 'U' as favorited , ^userpoints.points from ^users JOIN ^userpoints ON ^users.userid=^userpoints.userid JOIN ^userfavorites ON ^users.userid=^userfavorites.userid WHERE ^userfavorites.entityid=$ AND ^userfavorites.entitytype=$  AND ^users.email !=$ )";
                        $args      = array($userid, QA_ENTITY_USER, qa_get_logged_in_user_field('email'));
                        $arguments = array_merge($arguments, $args);
                  }
                  if (!!qa_opt('ami_email_notf_allow_tag_follower') && !!$tags) {
                        $source .= (!!$source) ? ' UNION ' : '';
                        $source .= "( SELECT ^users.email , 'T' as favorited , ^userpoints.points from ^users JOIN ^userpoints ON ^users.userid=^userpoints.userid JOIN ^userfavorites ON ^userfavorites.userid=^users.userid WHERE ^userfavorites.entityid IN 
                            ( SELECT wordid from ^words where ^words.word IN ($) ) AND ^userfavorites.entitytype=$ AND ^users.email !=$ )";
                        $args = array(qa_tagstring_to_tags($tags), QA_ENTITY_TAG, qa_get_logged_in_user_field('email'));
                        $arguments = array_merge($arguments, $args);
                  }
                  if (!!qa_opt('ami_email_notf_allow_cat_follower') && !!$categoryid) {
                        $source .= (!!$source) ? ' UNION ' : '';
                        $source .= "( SELECT ^users.email , 'C' as favorited , ^userpoints.points from ^users JOIN ^userpoints ON ^users.userid=^userpoints.userid JOIN ^userfavorites ON ^userfavorites.userid=^users.userid "
                                . "WHERE ^userfavorites.entityid=$ AND ^userfavorites.entitytype=$ AND ^users.email !=$ )";
                        $args = array($categoryid, QA_ENTITY_CATEGORY, qa_get_logged_in_user_field('email'));
                        $arguments = array_merge($arguments, $args);
                  }
                  $where_clause = '';
                  if (!!qa_opt('ami_email_notf_min_point')) {
                        //generate where clause 
                        $min_user_points = qa_opt('ami_email_notf_min_point_val');
                        $where_clause    = ((!!$min_user_points && ( $min_user_points > 0) )) ? 'where result.points > ' . $min_user_points : '';
                  }
                  return array(
                      'columns'   => array(' * '),
                      'source'    => ' ( ' . $source . ' ) as result ' . $where_clause,
                      'arguments' => $arguments,
                      'sortasc'   => 'title',
                  );
            }  //if plugin is enabled 
      }

//qa_db_notificaton_emails_selectspec

      function combine_emails($emails_id_list) {

            $unique_email_ids   = array();
            $return_email_datas = array();

            foreach ($emails_id_list as $email_data) {
                  $email = $email_data['email'];
                  if (!in_array($email, $unique_email_ids)) {
                        $return_email_datas[] = $email_data;
                        $unique_email_ids[]   = $email;
                  }
            }
            return $return_email_datas;
      }

      function category_email_notification_send_notification($bcclist, $email, $handle, $subject, $body, $subs){
            if (qa_to_override(__FUNCTION__)) {
                  $args = func_get_args();
                  return qa_call_override(__FUNCTION__, $args);
            }

            global $qa_notifications_suspended;

            if ($qa_notifications_suspended > 0) return false;

            require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
            require_once QA_INCLUDE_DIR . 'qa-util-string.php';

            $subs['^site_title'] = qa_opt('site_title');
            $subs['^handle']     = $handle;
            $subs['^email']      = $email;
            $subs['^open']       = "\n";
            $subs['^close']      = "\n";

            return $this->category_email_send_email(array(
                        'fromemail' => qa_opt('from_email'),
                        'fromname'  => qa_opt('site_title'),
                        'toemail'   => $email,
                        'toname'    => $handle,
                        'bcclist'   => $bcclist,
                        'subject'   => strtr($subject, $subs),
                        'body'      => (empty($handle) ? '' : qa_lang_sub('emails/to_handle_prefix', $handle)) . strtr($body, $subs),
                        'html'      => false,
            ));
      }

      function category_email_send_email($params) {
            if (qa_to_override(__FUNCTION__)) {
                  $args = func_get_args();
                  return qa_call_override(__FUNCTION__, $args);
            }

            require_once QA_INCLUDE_DIR . 'qa-class.phpmailer.php';

            $mailer = new PHPMailer();
            $mailer->CharSet = 'utf-8';

            $mailer->From     = $params['fromemail'];
            $mailer->Sender   = $params['fromemail'];
            $mailer->FromName = $params['fromname'];
            if (isset($params['toemail'])) {
                  $mailer->AddAddress($params['toemail'], $params['toname']);
            }
            $mailer->Subject = $params['subject'];
            $mailer->Body = $params['body'];
            if (isset($params['bcclist'])) {
                  foreach ($params['bcclist'] as $email) {
                        $mailer->AddBCC($email);
                  }
            }

            if ($params['html']) $mailer->IsHTML(true);

            if (qa_opt('smtp_active')) {
                  $mailer->IsSMTP();
                  $mailer->Host = qa_opt('smtp_address');
                  $mailer->Port = qa_opt('smtp_port');

                  if (qa_opt('smtp_secure')) $mailer->SMTPSecure = qa_opt('smtp_secure');

                  if (qa_opt('smtp_authenticate')) {
                        $mailer->SMTPAuth = true;
                        $mailer->Username = qa_opt('smtp_username');
                        $mailer->Password = qa_opt('smtp_password');
                  }
            }
            return $mailer->Send();
      }

      public function qa_get($param, $name = '') {
            return isset($param[$name]) ? $param[$name] : '';
      }


      function admin_form(&$qa_content) {

            //add the functions 
            require_once EMAIL_NOTF_PLUGIN_DIR . '/functions.php';
            //	Process form input

            $saved = false;

            if (qa_clicked('ami_email_notf_save_button')) {
                  $enable_plugin = !!qa_post_text('ami_email_notf_enable_plugin');
                  qa_opt('ami_email_notf_enable_plugin', $enable_plugin);
                  if (!$enable_plugin) {
                        //if the plugin is disabled then turn off all features 
Amiya Sahu's avatar
Amiya Sahu committed
205
                        ami_reset_all_notification_options();
Amiya Sahu's avatar
Amiya Sahu committed
206
                  } else {
Amiya Sahu's avatar
Amiya Sahu committed
207
                        $response = ami_set_all_notification_options();
Amiya Sahu's avatar
Amiya Sahu committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
                        //$error will be false if the 
                        $error = (isset($response) && is_array($response) && !empty($response)) ? true : false;
                  }

                  if (isset($response) && isset($error) && !!$error) {
                        $err_enter_point_value   = $this->qa_get($response, 'enter_point_value');
                        $err_no_options_selected = $this->qa_get($response, 'no_options_selected');
                  }

                  $saved = true;
            }


            //	Create the form for display

            qa_set_display_rules($qa_content, array(
                'ami_email_notf_allow_cat_follower'  => 'ami_email_notf_enable_plugin',
                'ami_email_notf_allow_tag_follower'  => 'ami_email_notf_enable_plugin',
                'ami_email_notf_allow_user_follower' => 'ami_email_notf_enable_plugin',
                'ami_email_notf_min_point'           => 'ami_email_notf_enable_plugin',
                'ami_email_notf_min_point_val'       => 'ami_email_notf_enable_plugin',
            ));

            return array(
                'ok' => ($saved && !$error ) ? 'Email Notification Settings Saved ' : null,
                'fields' => array(
                    array(
                        'label' => qa_lang('notify/plugin-enable'),
                        'tags'  => 'name="ami_email_notf_enable_plugin" id="ami_email_notf_enable_plugin"',
                        'value' => qa_opt('ami_email_notf_enable_plugin'),
                        'type'  => 'checkbox',
                        'error' => qa_html(@$err_no_options_selected),
                    ),
                    array(
                        'id'    => 'ami_email_notf_allow_user_follower',
                        'label' => qa_lang('notify/user-follower-enable'),
                        'tags'  => 'name="ami_email_notf_allow_user_follower" id="ami_email_notf_allow_user_follower"',
                        'value' => qa_opt('ami_email_notf_allow_user_follower'),
                        'type'  => 'checkbox',
                    ),
                    array(
                        'id'    => 'ami_email_notf_allow_tag_follower',
                        'label' => qa_lang('notify/tag-follower-enable'),
                        'tags'  => 'name="ami_email_notf_allow_tag_follower" id="ami_email_notf_allow_tag_follower"',
                        'value' => qa_opt('ami_email_notf_allow_tag_follower'),
                        'type'  => 'checkbox',
                    ),
                    array(
                        'id'    => 'ami_email_notf_allow_cat_follower',
                        'label' => qa_lang('notify/cat-follower-enable'),
                        'tags'  => 'name="ami_email_notf_allow_cat_follower" id="ami_email_notf_allow_cat_follower"',
                        'value' => qa_opt('ami_email_notf_allow_cat_follower'),
                        'type'  => 'checkbox',
                    ),
                    array(
                        'id'    => 'ami_email_notf_min_point',
                        'label' => qa_lang('notify/minimum-point-enable'),
                        'tags'  => 'name="ami_email_notf_min_point" id="ami_email_notf_min_point"',
                        'value' => qa_opt('ami_email_notf_min_point'),
                        'type'  => 'checkbox',
                    ),
                    array(
                        'id'    => 'ami_email_notf_min_point_val',
                        'label' => qa_lang('notify/minimum-point-input-lable'),
                        'value' => qa_html(qa_opt('ami_email_notf_min_point_val')),
                        'tags'  => 'name="ami_email_notf_min_point_val" id="ami_email_notf_min_point_val" ',
                        'error' => qa_html(@$err_enter_point_value),
                    ),
                    
                ),
                'buttons' => array(
                    array(
                        'label' => qa_lang('notify/save-button'),
                        'tags'  => 'name="ami_email_notf_save_button"',
                    ),
                ),
            );
      }

      public function plugin_enabled_from_admin_panel() {
            return ( (!!qa_opt('ami_email_notf_enable_plugin')) &&
                    (
                    (!!qa_opt('ami_email_notf_allow_cat_follower')) ||
                    (!!qa_opt('ami_email_notf_allow_tag_follower')) ||
                    (!!qa_opt('ami_email_notf_allow_user_follower'))
                    )
                    );
      }

}

;


/*
	Omit PHP closing tag to help avoid accidental output
*/