/**
  * Standard modular run function.
  *
  * @return tempcode	The result of execution.
  */
 function run()
 {
     require_lang('notifications');
     $title = get_page_title('NOTIFICATIONS_LOCKDOWN');
     require_css('notifications');
     require_javascript('javascript_notifications');
     require_code('notifications');
     require_code('notifications2');
     require_all_lang();
     $_notification_types = array(A__CHOICE => '_CHOICE', A__STATISTICAL => '_STATISTICAL');
     $_notification_types = $_notification_types + _get_available_notification_types();
     $lockdown = collapse_2d_complexity('l_notification_code', 'l_setting', $GLOBALS['SITE_DB']->query_select('notification_lockdown', array('*')));
     $current_setting = mixed();
     $notification_sections = array();
     $hooks = find_all_hooks('systems', 'notifications');
     foreach (array_keys($hooks) as $hook) {
         if (substr($hook, 0, 4) == 'ocf_' && get_forum_type() != 'ocf') {
             continue;
         }
         require_code('hooks/systems/notifications/' . $hook);
         $ob = object_factory('Hook_Notification_' . $hook);
         $_notification_codes = $ob->list_handled_codes();
         foreach ($_notification_codes as $notification_code => $notification_details) {
             $allowed_setting = $ob->allowed_settings($notification_code);
             $current_setting = array_key_exists($notification_code, $lockdown) ? $lockdown[$notification_code] : NULL;
             $notification_types = array();
             $save_query = false;
             foreach ($_notification_types as $possible => $ntype) {
                 $save_query = $save_query || post_param_integer('notification_' . $notification_code . '_' . $ntype, 0) == 1;
             }
             foreach ($_notification_types as $possible => $ntype) {
                 $available = $possible == A__CHOICE || $possible == A__STATISTICAL || ($possible & $allowed_setting) != 0;
                 if ($save_query) {
                     $checked = false;
                     // Will strictly read from POST
                 } else {
                     if (is_null($current_setting)) {
                         $checked = $possible == A__CHOICE;
                     } else {
                         if ($possible == A__STATISTICAL) {
                             $checked = $current_setting == A__STATISTICAL;
                         } elseif ($possible == A__CHOICE) {
                             $checked = false;
                         } elseif ($current_setting == -1) {
                             $checked = false;
                         } else {
                             $checked = ($possible & $current_setting) != 0;
                         }
                     }
                 }
                 $_checked = post_param_integer('notification_' . $notification_code . '_' . $ntype, strtoupper(ocp_srv('REQUEST_METHOD')) != 'POST' && $checked ? 1 : 0);
                 $notification_types[] = array('NTYPE' => $ntype, 'LABEL' => do_lang_tempcode('ENABLE_NOTIFICATIONS_' . $ntype), 'CHECKED' => $_checked == 1, 'RAW' => strval($possible), 'AVAILABLE' => $available, 'SCOPE' => $notification_code);
             }
             if (!isset($notification_sections[$notification_details[0]])) {
                 $notification_sections[$notification_details[0]] = array('NOTIFICATION_SECTION' => $notification_details[0], 'NOTIFICATION_CODES' => array());
             }
             $notification_sections[$notification_details[0]]['NOTIFICATION_CODES'][] = array('NOTIFICATION_CODE' => $notification_code, 'NOTIFICATION_LABEL' => $notification_details[1], 'NOTIFICATION_TYPES' => $notification_types, 'SUPPORTS_CATEGORIES' => false, 'PRIVILEGED' => !$ob->member_could_potentially_enable($ntype, $GLOBALS['FORUM_DRIVER']->get_guest_id()));
         }
     }
     // Save
     if (strtoupper(ocp_srv('REQUEST_METHOD')) == 'POST') {
         $GLOBALS['SITE_DB']->query_delete('notification_lockdown');
         foreach ($notification_sections as $notification_section) {
             foreach ($notification_section['NOTIFICATION_CODES'] as $notification_code) {
                 $new_setting = A_NA;
                 foreach ($notification_code['NOTIFICATION_TYPES'] as $notification_type) {
                     $ntype = $notification_type['NTYPE'];
                     if (post_param_integer('notification_' . $notification_code['NOTIFICATION_CODE'] . '_' . $ntype, 0) == 1) {
                         $new_setting = $new_setting | intval($notification_type['RAW']);
                     }
                 }
                 if ($new_setting != A__CHOICE) {
                     $GLOBALS['SITE_DB']->query_insert('notification_lockdown', array('l_notification_code' => substr($notification_code['NOTIFICATION_CODE'], 0, 80), 'l_setting' => $new_setting));
                 }
             }
         }
         attach_message(do_lang_tempcode('SUCCESS'));
     }
     // Sort labels
     global $M_SORT_KEY;
     $M_SORT_KEY = 'NOTIFICATION_LABEL';
     ksort($notification_sections);
     foreach (array_keys($notification_sections) as $i) {
         usort($notification_sections[$i]['NOTIFICATION_CODES'], 'multi_sort');
     }
     $css_path = get_custom_file_base() . '/themes/' . $GLOBALS['FORUM_DRIVER']->get_theme() . '/templates_cached/' . user_lang() . '/global.css';
     $color = 'FF00FF';
     if (file_exists($css_path)) {
         $tmp_file = file_get_contents($css_path);
         $matches = array();
         if (preg_match('#\\nth[\\s,][^\\}]*\\sbackground-color:\\s*\\#([\\dA-Fa-f]*);#sU', $tmp_file, $matches) != 0) {
             $color = $matches[1];
         }
     }
     $notification_types_titles = array();
     foreach ($_notification_types as $possible => $ntype) {
         $notification_types_titles[] = array('NTYPE' => $ntype, 'LABEL' => do_lang_tempcode('ENABLE_NOTIFICATIONS_' . $ntype), 'RAW' => strval($possible));
     }
     $interface = do_template('NOTIFICATIONS_MANAGE', array('SHOW_PRIVILEGES' => true, 'COLOR' => $color, 'NOTIFICATION_TYPES_TITLES' => $notification_types_titles, 'NOTIFICATION_SECTIONS' => $notification_sections));
     return do_template('NOTIFICATIONS_MANAGE_SCREEN', array('TITLE' => $title, 'INTERFACE' => $interface, 'ACTION_URL' => get_self_url()));
 }
Example #2
0
/**
 * Put out a user interface for managing notifications for a notification-category supporting content type. Also toggle notifications if an ID is passed.
 *
 * @param  ID_TEXT		The notification code to work with
 * @param  ?tempcode		Special message to output if we have toggled to enable (NULL: use standard)
 * @param  ?tempcode		Special message to output if we have toggled to disable (NULL: use standard)
 * @return tempcode		UI
 */
function notifications_ui_advanced($notification_code, $enable_message = NULL, $disable_message = NULL)
{
    require_css('notifications');
    require_code('notifications');
    require_lang('notifications');
    require_javascript('javascript_notifications');
    require_javascript('javascript_notifications');
    require_all_lang();
    $test = $GLOBALS['SITE_DB']->query_value_null_ok('notification_lockdown', 'l_setting', array('l_notification_code' => substr($notification_code, 0, 80)));
    if (!is_null($test)) {
        warn_exit(do_lang_tempcode('NOTIFICATION_CODE_LOCKED_DOWN'));
    }
    $ob = _get_notification_ob_for_code($notification_code);
    $info_details = $ob->list_handled_codes();
    $title = get_page_title('NOTIFICATION_MANAGEMENT_FOR', true, array(escape_html($info_details[$notification_code][1])));
    if (is_guest()) {
        access_denied('NOT_AS_GUEST');
    }
    $db = substr($notification_code, 0, 4) == 'ocf_' ? $GLOBALS['FORUM_DB'] : $GLOBALS['SITE_DB'];
    if (is_null($enable_message)) {
        $enable_message = do_lang_tempcode('NOW_ENABLED_NOTIFICATIONS');
    }
    if (is_null($disable_message)) {
        $disable_message = do_lang_tempcode('NOW_DISABLED_NOTIFICATIONS');
    }
    $_notification_types = _get_available_notification_types(get_member());
    $notification_category = get_param('id', NULL);
    if (!is_null($notification_category)) {
        if (notifications_enabled($notification_code, $notification_category)) {
            enable_notifications($notification_code, $notification_category, NULL, A_NA);
            attach_message($disable_message, 'inform');
        } else {
            enable_notifications($notification_code, $notification_category);
            attach_message($enable_message, 'inform');
        }
    } else {
        if (count($_POST) != 0) {
            enable_notifications($notification_code, NULL, NULL, A_NA);
            // Make it clear we've overridden the general value by doing this
            foreach (array_keys($_POST) as $key) {
                $matches = array();
                if (preg_match('#^notification\\_' . preg_quote($notification_code) . '\\_category\\_(.*)#', $key, $matches) != 0) {
                    $notification_category = $matches[1];
                    $new_setting = A_NA;
                    foreach ($_notification_types as $possible => $ntype) {
                        if (post_param_integer('notification_' . $notification_category . '_' . $ntype, 0) == 1) {
                            $new_setting = $new_setting | $possible;
                        }
                    }
                    enable_notifications($notification_code, $notification_category, NULL, $new_setting);
                }
            }
            attach_message(do_lang_tempcode('SUCCESS'), 'inform');
            // Redirect them back
            $redirect = get_param('redirect', NULL);
            if (!is_null($redirect)) {
                return redirect_screen($title, $redirect, do_lang_tempcode('SUCCESS'));
            }
        }
    }
    $tree = _notifications_build_category_tree($_notification_types, $notification_code, $ob, NULL);
    $notification_types_titles = array();
    foreach ($_notification_types as $possible => $ntype) {
        $notification_types_titles[] = array('NTYPE' => $ntype, 'LABEL' => do_lang_tempcode('ENABLE_NOTIFICATIONS_' . $ntype), 'RAW' => strval($possible));
    }
    $css_path = get_custom_file_base() . '/themes/' . $GLOBALS['FORUM_DRIVER']->get_theme() . '/templates_cached/' . user_lang() . '/global.css';
    $color = 'FF00FF';
    if (file_exists($css_path)) {
        $tmp_file = file_get_contents($css_path);
        $matches = array();
        if (preg_match('#\\nth[\\s,][^\\}]*\\sbackground-color:\\s*\\#([\\dA-Fa-f]*);#sU', $tmp_file, $matches) != 0) {
            $color = $matches[1];
        }
    }
    return do_template('NOTIFICATIONS_MANAGE_ADVANCED_SCREEN', array('TITLE' => $title, 'COLOR' => $color, 'ACTION_URL' => get_self_url(false, false, array('id' => NULL)), 'NOTIFICATION_TYPES_TITLES' => $notification_types_titles, 'TREE' => $tree, 'NOTIFICATION_CODE' => $notification_code));
}