<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing 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  Christopher Graham
 * @package    core_comcode_pages
 */

/**
 * Hook class.
 */
class Hook_content_meta_aware_comcode_page extends Hook_CMA
{
    /**
     * Get content type details.
     *
     * @param  ?ID_TEXT $zone The zone to link through to (null: autodetect)
     * @param  boolean $get_extended_data Populate additional data that is somewhat costly to compute (add_url, archive_url)
     * @return ?array Map of content-type info (null: disabled)
     */
    public function info(?string $zone = null, bool $get_extended_data = false) : ?array
    {
        if (($zone === null) && ($get_extended_data)) {
            $zone = @strval(get_page_zone('sitemap', false));
        }

        return [
            'support_custom_fields' => true,

            'content_type_label' => 'zones:COMCODE_PAGE',
            'content_type_universal_label' => 'Comcode page',

            'db' => $GLOBALS['SITE_DB'],
            'table' => 'comcode_pages',
            'id_field' => ['the_page', 'the_zone'],
            'id_field_numeric' => false,
            'parent_category_field' => 'the_zone',
            'parent_category_meta_aware_type' => null,
            'is_category' => false,
            'is_entry' => true,
            'category_field' => ['the_zone', 'the_page'], // For category permissions (actually used for page permissions in this case)
            'permission_module' => null, // For category permissions
            'parent_spec__table_name' => null,
            'parent_spec__parent_name' => null,
            'parent_spec__field_name' => null,
            'category_is_string' => true,

            'title_field' => 'the_page',
            'title_field_dereference' => false,
            'description_field' => null,
            'description_field_dereference' => null,
            'description_field_supports_comcode' => null,
            'image_field' => null,
            'image_field_is_theme_image' => false,
            'alternate_icon_theme_image' => 'icons/content_types/page',

            'view_page_link_pattern' => '_WILD',
            'edit_page_link_pattern' => '_SEARCH:cms_comcode_pages:_edit:page_link=_WILD',
            'view_category_page_link_pattern' => '_WILD:',
            'add_url' => ($get_extended_data && has_submit_permission('high', get_member(), get_ip_address(), 'cms_comcode_pages')) ? (get_module_zone('cms_comcode_pages') . ':cms_comcode_pages:edit') : null,
            'archive_url' => $get_extended_data ? ($zone . ':sitemap') : null,

            'support_url_monikers' => true,

            'views_field' => null,
            'order_field' => 'p_order',
            'submitter_field' => 'p_submitter',
            'author_field' => null,
            'add_time_field' => 'p_add_date',
            'edit_time_field' => 'p_edit_date',
            'date_field' => 'p_add_date',
            'validated_field' => 'p_validated',
            'additional_sort_fields' => [
                'page_title' => [ // No SQL; cms_comcode_pages manages sorting because of field translation
                    'ASC' => '',
                    'DESC' => '',
                ],
                'page_link' => [
                    'ASC' => 'r.the_zone ASC,r.the_page ASC',
                    'DESC' => 'r.the_zone DESC,r.the_page DESC',
                ],
            ],

            'seo_type_code' => 'comcode_page',

            'feedback_type_code' => null,

            'search_hook' => 'comcode_pages',
            'rss_hook' => 'comcode_pages',
            'attachment_hook' => 'comcode_page',
            'notification_hook' => null,
            'sitemap_hook' => 'comcode_page',

            'addon_name' => 'core_comcode_pages',

            'module' => null,
            'cms_page' => 'cms_comcode_pages',

            'commandr_filesystem_hook' => 'comcode_pages',
            'commandr_filesystem__is_folder' => false,

            'support_revisions' => false, // Supports the file-based engine, which is a somewhat separate implementation

            'support_privacy' => false,

            'support_content_reviews' => true,

            'support_spam_heuristics' => null,

            'actionlog_regexp' => '\w+_COMCODE_PAGE',

            'extra_where_sql' => 'r.p_include_on_sitemap=1',

            'default_prominence_weight' => PROMINENCE_WEIGHT_MEDIUM,
            'default_prominence_flags' => 0,
        ];
    }

    /**
     * Render a content box for a content row.
     *
     * @param  array $row The database row for the content
     * @param  ID_TEXT $zone The zone to display in
     * @param  boolean $give_context Whether to include context (i.e. say WHAT this is, not just show the actual content)
     * @param  boolean $include_breadcrumbs Whether to include breadcrumbs (if there are any)
     * @param  ?ID_TEXT $root Virtual root to use (null: none)
     * @param  boolean $attach_to_url_filter Whether to copy through any filter parameters in the URL, under the basis that they are associated with what this box is browsing
     * @param  ID_TEXT $guid Overridden GUID to send to templates (blank: none)
     * @return Tempcode Results
     */
    public function render_box(array $row, string $zone, bool $give_context = true, bool $include_breadcrumbs = true, ?string $root = null, bool $attach_to_url_filter = false, string $guid = '') : object
    {
        unset($zone); // Meaningless here

        require_code('zones2');

        return render_comcode_page_box($row, $give_context, $include_breadcrumbs, $root, $guid);
    }

    /**
     * Get a view page-link for content.
     *
     * @param  ?array $row The database row for the content (null: no ID, assume passed in some other way)
     * @param  string $append What to append to the view page-link
     * @return string The page-link (blank: none)
     */
    public function get_view_page_link(?array $row, string $append = '') : string
    {
        $page_link = parent::get_view_page_link($row, $append);

        // Assume the page is on the welcome zone if no colon exists; stick a colon at the beginning to indicate such.
        if (strpos($page_link, ':') === false) {
            $page_link = ':' . $page_link;
        }

        return $page_link;
    }
}
