Commit 4a3199cb authored by Amiya Sahu's avatar Amiya Sahu

Code Cleanup and languages added

parent cc656d05
...@@ -6,16 +6,16 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly ...@@ -6,16 +6,16 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
} }
function reset_all_notification_options() { function reset_all_notification_options() {
qa_opt(ALLOW_CAT_FOLLOWER_EMAILS_OPT, false); qa_opt('ami_email_notf_allow_cat_follower', false);
qa_opt(ALLOW_TAG_FOLLOWER_EMAILS_OPT, false); qa_opt('ami_email_notf_allow_tag_follower', false);
qa_opt(ALLOW_USER_FOLLOWER_EMAILS_OPT, false); qa_opt('ami_email_notf_allow_user_follower', false);
qa_opt(MINIMUM_USER_POINT_OPT, false); qa_opt('ami_email_notf_min_point', false);
qa_opt(MINIMUM_USER_POINT_VAL_OPT, false); qa_opt('ami_email_notf_min_point_val', false);
} }
function reset_all_notification_points_options() { function reset_all_notification_points_options() {
qa_opt(MINIMUM_USER_POINT_OPT, false); qa_opt('ami_email_notf_min_point', false);
qa_opt(MINIMUM_USER_POINT_VAL_OPT, false); qa_opt('ami_email_notf_min_point_val', false);
} }
function set_all_notification_options() { function set_all_notification_options() {
...@@ -23,38 +23,38 @@ function set_all_notification_options() { ...@@ -23,38 +23,38 @@ function set_all_notification_options() {
$error = array(); $error = array();
//if plugin is enabled then atlest one option has to be enabled //if plugin is enabled then atlest one option has to be enabled
if (options_selected()) { if (options_selected()) {
qa_opt(ALLOW_CAT_FOLLOWER_EMAILS_OPT, !!qa_post_text(ALLOW_CAT_FOLLOWER_EMAILS_FIELD)); qa_opt('ami_email_notf_allow_cat_follower', !!qa_post_text('ami_email_notf_allow_cat_follower'));
qa_opt(ALLOW_TAG_FOLLOWER_EMAILS_OPT, !!qa_post_text(ALLOW_TAG_FOLLOWER_EMAILS_FIELD)); qa_opt('ami_email_notf_allow_tag_follower', !!qa_post_text('ami_email_notf_allow_tag_follower'));
qa_opt(ALLOW_USER_FOLLOWER_EMAILS_OPT, !!qa_post_text(ALLOW_USER_FOLLOWER_EMAILS_FIELD)); qa_opt('ami_email_notf_allow_user_follower', !!qa_post_text('ami_email_notf_allow_user_follower'));
$minimum_user_point_option = !!qa_post_text(MINIMUM_USER_POINT_FIELD); $minimum_user_point_option = !!qa_post_text('ami_email_notf_min_point');
if ($minimum_user_point_option) { //if minimum point option is checked if ($minimum_user_point_option) { //if minimum point option is checked
$minimum_user_point_value = qa_post_text(MINIMUM_USER_POINT_VAL_FIELD); $minimum_user_point_value = qa_post_text('ami_email_notf_min_point_val');
if (!!$minimum_user_point_value && is_numeric($minimum_user_point_value) && $minimum_user_point_value > 0) { //if the minimum point value is provided then only set else reset if (!!$minimum_user_point_value && is_numeric($minimum_user_point_value) && $minimum_user_point_value > 0) { //if the minimum point value is provided then only set else reset
qa_opt(MINIMUM_USER_POINT_OPT, $minimum_user_point_option); qa_opt('ami_email_notf_min_point', $minimum_user_point_option);
qa_opt(MINIMUM_USER_POINT_VAL_OPT, (int) $minimum_user_point_value); qa_opt('ami_email_notf_min_point_val', (int) $minimum_user_point_value);
} else if (!is_numeric($minimum_user_point_value) || $minimum_user_point_value <= 0) { } else if (!is_numeric($minimum_user_point_value) || $minimum_user_point_value <= 0) {
reset_all_notification_points_options(); reset_all_notification_points_options();
//send a error message to UI //send a error message to UI
$error['enter_point_value'] = "The points value should be a numeric and non-zero positive integer "; $error['enter_point_value'] = qa_lang('point_value_should_numeric');
} else { } else {
reset_all_notification_points_options(); reset_all_notification_points_options();
//send a error message to UI //send a error message to UI
$error['enter_point_value'] = "The points value is required to enable the option "; $error['enter_point_value'] = qa_lang('point_value_required'); ;
} }
} else { } else {
reset_all_notification_points_options(); reset_all_notification_points_options();
} }
} else { } else {
//if none of the elements are selected disable the plugin and send a error message UI //if none of the elements are selected disable the plugin and send a error message UI
qa_opt(ENABLE_PLUGIN, false); qa_opt('ami_email_notf_enable_plugin', false);
reset_all_notification_options(); reset_all_notification_options();
$error['no_options_selected'] = "Please choose atleast follower option to enable this plugin "; $error['no_options_selected'] = qa_lang('choose_atleast_one_opt');
} }
return $error; return $error;
} }
function options_selected() { function options_selected() {
return ((!!qa_post_text(ALLOW_CAT_FOLLOWER_EMAILS_FIELD)) || return ((!!qa_post_text('ami_email_notf_allow_cat_follower')) ||
(!!qa_post_text(ALLOW_TAG_FOLLOWER_EMAILS_FIELD)) || (!!qa_post_text('ami_email_notf_allow_tag_follower')) ||
(!!qa_post_text(ALLOW_USER_FOLLOWER_EMAILS_FIELD)) ); (!!qa_post_text('ami_email_notf_allow_user_follower')) );
} }
...@@ -34,6 +34,10 @@ ...@@ -34,6 +34,10 @@
'debug-mode-enable' => 'Enable debug mode ( requires event logger plugin to be enabled with log file option , to get the search results to the log file . Not recomended if you are not a developer ) . ', 'debug-mode-enable' => 'Enable debug mode ( requires event logger plugin to be enabled with log file option , to get the search results to the log file . Not recomended if you are not a developer ) . ',
'save-button' => 'Save Changes', 'save-button' => 'Save Changes',
'q_posted_body' => "A new question has been asked by ^q_handle:\n\nThe Question is : ^open^q_title\n\nDescription : ^open^q_content^close\n\nClick below to see the question:\n\n^url\n\nThank you,\n\n^site_title ^open^site_url", 'q_posted_body' => "A new question has been asked by ^q_handle:\n\nThe Question is : ^open^q_title\n\nDescription : ^open^q_content^close\n\nClick below to see the question:\n\n^url\n\nThank you,\n\n^site_title ^open^site_url",
'choose_atleast_one_opt' => 'Please choose atleast follower option to enable this plugin ' ,
'point_value_required' => 'The points value is required to enable the option ' ,
'point_value_should_numeric' => 'The points value should be a numeric and non-zero positive integer',
); );
......
<?php
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
//define the option prefix
define('PLUGIN_NAME', 'qa_email_notf_');
//define enable and disable options
define('ENABLE_PLUGIN', PLUGIN_NAME . 'enable_plugin');
define('ENABLE_PLUGIN_FIELD', PLUGIN_NAME . 'enable_plugin_field');
//define the options
define('EAMIL_NOTF_DEBUG_MODE_OPT', PLUGIN_NAME . 'debug_mode');
define('ALLOW_CAT_FOLLOWER_EMAILS_OPT', PLUGIN_NAME . 'allow_cat_follower_emails');
define('ALLOW_TAG_FOLLOWER_EMAILS_OPT', PLUGIN_NAME . 'allow_tag_follower_emails');
define('ALLOW_USER_FOLLOWER_EMAILS_OPT', PLUGIN_NAME . 'allow_user_follower_emails');
define('MINIMUM_USER_POINT_OPT', PLUGIN_NAME . 'min_point');
define('MINIMUM_USER_POINT_VAL_OPT', PLUGIN_NAME . 'min_point_val');
//define the admin form fields and buttons
define('EAMIL_NOTF_DEBUG_MODE_FIELD', PLUGIN_NAME . 'debug_mode_field');
define('ALLOW_CAT_FOLLOWER_EMAILS_FIELD', PLUGIN_NAME . 'allow_cat_follower_emails_field');
define('ALLOW_TAG_FOLLOWER_EMAILS_FIELD', PLUGIN_NAME . 'allow_tag_follower_emails_field');
define('ALLOW_USER_FOLLOWER_EMAILS_FIELD', PLUGIN_NAME . 'allow_user_follower_emails_field');
define('MINIMUM_USER_POINT_FIELD', PLUGIN_NAME . 'min_point_field');
define('MINIMUM_USER_POINT_VAL_FIELD', PLUGIN_NAME . 'min_point_val_field');
define('SAVE_BUTTON', PLUGIN_NAME . 'save_button');
<?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__);
include_once EMAIL_NOTF_PLUGIN_DIR . '/plugin-constants.php';
class qa_category_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(ALLOW_USER_FOLLOWER_EMAILS_OPT)) {
$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(ALLOW_TAG_FOLLOWER_EMAILS_OPT) && !!$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(ALLOW_CAT_FOLLOWER_EMAILS_OPT) && !!$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(MINIMUM_USER_POINT_OPT)) {
//generate where clause
$min_user_points = qa_opt(MINIMUM_USER_POINT_VAL_OPT);
$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(SAVE_BUTTON)) {
$enable_plugin = !!qa_post_text(ENABLE_PLUGIN_FIELD);
qa_opt(ENABLE_PLUGIN, $enable_plugin);
if (!$enable_plugin) {
//if the plugin is disabled then turn off all features
reset_all_notification_options();
} else {
$response = set_all_notification_options();
//$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(
EAMIL_NOTF_DEBUG_MODE_OPT => ENABLE_PLUGIN_FIELD,
ALLOW_CAT_FOLLOWER_EMAILS_OPT => ENABLE_PLUGIN_FIELD,
ALLOW_TAG_FOLLOWER_EMAILS_OPT => ENABLE_PLUGIN_FIELD,
ALLOW_USER_FOLLOWER_EMAILS_OPT => ENABLE_PLUGIN_FIELD,
MINIMUM_USER_POINT_OPT => ENABLE_PLUGIN_FIELD,
MINIMUM_USER_POINT_VAL_OPT => ENABLE_PLUGIN_FIELD,
));
return array(
'ok' => ($saved && !$error ) ? 'Email Notification Settings Saved ' : null,
'fields' => array(
array(
'label' => qa_lang('notify/plugin-enable'),
'tags' => 'name="' . ENABLE_PLUGIN_FIELD . '" id="' . ENABLE_PLUGIN_FIELD . '"',
'value' => qa_opt(ENABLE_PLUGIN),
'type' => 'checkbox',
'error' => qa_html(@$err_no_options_selected),
),
array(
'id' => ALLOW_USER_FOLLOWER_EMAILS_OPT,
'label' => qa_lang('notify/user-follower-enable'),
'tags' => 'name="' . ALLOW_USER_FOLLOWER_EMAILS_FIELD . '" id="' . ALLOW_USER_FOLLOWER_EMAILS_FIELD . '"',
'value' => qa_opt(ALLOW_USER_FOLLOWER_EMAILS_OPT),
'type' => 'checkbox',
),
array(
'id' => ALLOW_TAG_FOLLOWER_EMAILS_OPT,
'label' => qa_lang('notify/tag-follower-enable'),
'tags' => 'name="' . ALLOW_TAG_FOLLOWER_EMAILS_FIELD . '" id="' . ALLOW_TAG_FOLLOWER_EMAILS_FIELD . '"',
'value' => qa_opt(ALLOW_TAG_FOLLOWER_EMAILS_OPT),
'type' => 'checkbox',
),
array(
'id' => ALLOW_CAT_FOLLOWER_EMAILS_OPT,
'label' => qa_lang('notify/cat-follower-enable'),
'tags' => 'name="' . ALLOW_CAT_FOLLOWER_EMAILS_FIELD . '" id="' . ALLOW_CAT_FOLLOWER_EMAILS_FIELD . '"',
'value' => qa_opt(ALLOW_CAT_FOLLOWER_EMAILS_OPT),
'type' => 'checkbox',
),
array(
'id' => MINIMUM_USER_POINT_OPT,
'label' => qa_lang('notify/minimum-point-enable'),
'tags' => 'name="' . MINIMUM_USER_POINT_FIELD . '" id="' . MINIMUM_USER_POINT_FIELD . '"',
'value' => qa_opt(MINIMUM_USER_POINT_OPT),
'type' => 'checkbox',
),
array(
'id' => MINIMUM_USER_POINT_VAL_OPT,
'label' => qa_lang('notify/minimum-point-input-lable'),
'value' => qa_html(qa_opt(MINIMUM_USER_POINT_VAL_OPT)),
'tags' => 'name="' . MINIMUM_USER_POINT_VAL_FIELD . '" id="' . MINIMUM_USER_POINT_VAL_FIELD . '" ',
'error' => qa_html(@$err_enter_point_value),
),
),
'buttons' => array(
array(
'label' => qa_lang('notify/save-button'),
'tags' => 'name="' . SAVE_BUTTON . '"',
),
),
);
}
public function plugin_enabled_from_admin_panel() {
return ( (!!qa_opt(ENABLE_PLUGIN)) &&
(
(!!qa_opt(ALLOW_CAT_FOLLOWER_EMAILS_OPT)) ||
(!!qa_opt(ALLOW_TAG_FOLLOWER_EMAILS_OPT)) ||
(!!qa_opt(ALLOW_USER_FOLLOWER_EMAILS_OPT))
)
);
}
}
;
/*
Omit PHP closing tag to help avoid accidental output
*/
<?php <?php
/* /*
Plugin Name: Email Notifications Plugin Name: Category/Tag Email Notification
Plugin URI: http://amiyasahu.com Plugin URI: http://amiyasahu.com
Plugin Description: Sends email for new questions, to users who favoritised the category where it was posted Plugin Description: Sends email for new questions, to users who is following a catogory or
Plugin Version: 0.1 Plugin Version: 0.1
Plugin Date: 2014-04-20 Plugin Date: 2014-04-20
Plugin Author: Amiya Sahu Plugin Author: Amiya Sahu
...@@ -18,8 +18,8 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly ...@@ -18,8 +18,8 @@ if (!defined('QA_VERSION')) { // don't allow this page to be requested directly
exit; exit;
} }
qa_register_plugin_module('event', 'qa-category-email-notifications-event.php', 'qa_category_email_notifications_event', 'Category Email Notifications'); qa_register_plugin_module('event', 'qa-email-notifications-event.php', 'qa_email_notifications_event', 'Category/Tag Email Notifications');
qa_register_plugin_phrases('language/qa-email-notification-lang-*.php', 'notify'); qa_register_plugin_phrases('language/qa-email-notification-lang-*.php', 'notify');
/* /*
Omit PHP closing tag to help avoid accidental output Omit PHP closing tag to help avoid accidental output
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment