<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    catalogues
 */

/**
 * Hook class.
 */
class Hook_notification_catalogue_entry extends Hook_Notification
{
    /**
     * Find whether a handled notification code supports categories.
     * (Content types, for example, will define notifications on specific categories, not just in general. The categories are interpreted by the hook and may be complex. E.g. it might be like a regexp match, or like FORUM:3 or TOPIC:100)
     *
     * @param  ID_TEXT $notification_code Notification code
     * @return boolean Whether it does
     */
    public function supports_categories($notification_code)
    {
        return true;
    }

    /**
     * Standard function to create the standardised category tree
     *
     * @param  ID_TEXT $notification_code Notification code
     * @param  ?ID_TEXT $id The ID of where we're looking under (null: N/A)
     * @return array Tree structure
     */
    public function create_category_tree($notification_code, $id)
    {
        require_code('catalogues');

        $name = substr($notification_code, strlen('catalogue_entry__'));

        $total = $GLOBALS['SITE_DB']->query_select_value_if_there('catalogue_categories', 'COUNT(*)', array('c_name' => $name));
        if ($total > intval(get_option('general_safety_listing_limit'))/*reasonable limit*/) {
            return parent::create_category_tree($notification_code, $id); // Too many, so just allow removing UI
        }

        $page_links = get_catalogue_category_tree($name, is_null($id) ? null : intval($id), '', null, ($id === null) ? 0 : 1);
        $filtered = array();
        foreach ($page_links as $p) {
            if (strval($p['id']) !== $id) {
                $filtered[] = $p;
            }
        }
        return $filtered;
    }

    /**
     * Find the initial setting that members have for a notification code (only applies to the member_could_potentially_enable members).
     *
     * @param  ID_TEXT $notification_code Notification code
     * @param  ?SHORT_TEXT $category The category within the notification code (null: none)
     * @return integer Initial setting
     */
    public function get_initial_setting($notification_code, $category = null)
    {
        return A_NA;
    }

    /**
     * Get a list of all the notification codes this hook can handle.
     * (Addons can define hooks that handle whole sets of codes, so hooks are written so they can take wide authority)
     *
     * @return array List of codes (mapping between code names, and a pair: section and labelling for those codes)
     */
    public function list_handled_codes()
    {
        $list = array();
        $catalogues = $GLOBALS['SITE_DB']->query_select('catalogues', array('c_name', 'c_title'), null, 'WHERE c_name NOT LIKE \'\_%\'');
        foreach ($catalogues as $catalogue) {
            $catalogue_name = $catalogue['c_name'];
            $nl = do_lang('catalogues:NOTIFICATION_TYPE_catalogue_entry__' . $catalogue_name, null, null, null, null, false);
            if (is_null($nl)) {
                $nl = do_lang('catalogues:NOTIFICATION_TYPE_catalogue_entry', get_translated_text($catalogue['c_title']));
            }
            $list['catalogue_entry__' . $catalogue_name] = array(do_lang('CONTENT'), $nl);
        }
        return $list;
    }

    /**
     * Get a list of members who have enabled this notification (i.e. have permission to AND have chosen to or are defaulted to).
     *
     * @param  ID_TEXT $notification_code Notification code
     * @param  ?SHORT_TEXT $category The category within the notification code (null: none)
     * @param  ?array $to_member_ids List of member IDs we are restricting to (null: no restriction). This effectively works as a intersection set operator against those who have enabled.
     * @param  integer $start Start position (for pagination)
     * @param  integer $max Maximum (for pagination)
     * @return array A pair: Map of members to their notification setting, and whether there may be more
     */
    public function list_members_who_have_enabled($notification_code, $category = null, $to_member_ids = null, $start = 0, $max = 300)
    {
        $members = $this->_all_members_who_have_enabled($notification_code, $category, $to_member_ids, $start, $max);
        $members = $this->_all_members_who_have_enabled_with_page_access($members, 'catalogues', $notification_code, $category, $to_member_ids, $start, $max);
        $catalogue_category_id = intval($category);
        $catalogue_name = $GLOBALS['SITE_DB']->query_select_value('catalogue_categories', 'c_name', array('id' => $catalogue_category_id));
        $members = $this->_all_members_who_have_enabled_with_category_access($members, 'catalogues_catalogue', $notification_code, $catalogue_name, $to_member_ids, $start, $max);
        if (get_value('disable_cat_cat_perms') !== '1') {
            $members = $this->_all_members_who_have_enabled_with_category_access($members, 'catalogues_category', $notification_code, strval($catalogue_category_id), $to_member_ids, $start, $max);
        }
        return $members;
    }
}
