예제 #1
0
/**
 * Updates page data by id or create new
 *
 * @param array $page_data Page data
 * @param int $page_id Page idetifier, if equals zero new page will be created
 * @param string $lang_code 2 letters language code
 * @return int Page identifier on success, false otherwise
 */
function fn_update_page($page_data, $page_id = 0, $lang_code = CART_LANGUAGE)
{
    /**
     * Actions before all checks and initializations
     *
     * @param array  $page_data Page data
     * @param int    $page_id   Page idetifier, if equals zero new page will be created
     * @param string $lang_code 2 letters language code
     */
    fn_set_hook('update_page_pre', $page_data, $page_id, $lang_code);
    if (!empty($page_id) && !fn_check_company_id('pages', 'page_id', $page_id)) {
        fn_company_access_denied_notification();
        return false;
    }
    if (!empty($page_data)) {
        SecurityHelper::sanitizeObjectData('page', $page_data);
        if (!empty($page_data['avail_from_timestamp'])) {
            $page_data['avail_from_timestamp'] = fn_parse_date($page_data['avail_from_timestamp']);
        } else {
            $page_data['avail_from_timestamp'] = 0;
        }
        if (!empty($page_data['avail_till_timestamp'])) {
            $page_data['avail_till_timestamp'] = fn_parse_date($page_data['avail_till_timestamp']) + 86399;
        } else {
            $page_data['avail_till_timestamp'] = 0;
        }
        if (isset($page_data['usergroup_ids'])) {
            $page_data['usergroup_ids'] = empty($page_data['usergroup_ids']) ? '0' : implode(',', $page_data['usergroup_ids']);
        }
        $page_data['add_items'] = empty($page_data['add_items']) ? array() : $page_data['add_items'];
        if (isset($page_data['timestamp'])) {
            $page_data['timestamp'] = fn_parse_date($page_data['timestamp']);
        }
        if (isset($page_data['localization'])) {
            $page_data['localization'] = empty($page_data['localization']) ? '' : fn_implode_localizations($page_data['localization']);
        }
        $old_page_data = array();
        /**
         * Actions after all checks and initializations and before update page
         *
         * @param array $page_data Page data
         * @param int $page_id Page idetifier, if equals zero new page will be created
         * @param string $lang_code 2 letters language code
         */
        fn_set_hook('update_page_before', $page_data, $page_id, $lang_code);
        $parent_id = isset($page_data['parent_id']) ? $page_data['parent_id'] : null;
        unset($page_data['parent_id']);
        if (empty($page_id)) {
            // page title required
            if (empty($page_data['page'])) {
                return false;
            }
            // add new page
            $create = true;
            $page_data['page_id'] = $page_id = db_query('INSERT INTO ?:pages ?e', $page_data);
            foreach (fn_get_translation_languages() as $page_data['lang_code'] => $v) {
                db_query('INSERT INTO ?:page_descriptions ?e', $page_data);
            }
        } else {
            $old_page_data = fn_get_page_data($page_id, $lang_code);
            $create = false;
            // page title is not updated
            if (empty($page_data['page'])) {
                unset($page_data['page']);
            }
            // update existing page
            db_query('UPDATE ?:pages SET ?u WHERE page_id = ?i', $page_data, $page_id);
            db_query('UPDATE ?:page_descriptions SET ?u WHERE page_id = ?i AND lang_code = ?s', $page_data, $page_id, $lang_code);
        }
        $page_data['parent_id'] = $parent_id;
        // regenerate id_path for child pages
        if (isset($page_data['parent_id'])) {
            fn_change_page_parent($page_id, intval($page_data['parent_id']));
        }
    }
    /**
     * Actions after page update
     *
     * @param array  $page_data     Page data
     * @param int    $page_id       Page idetifier, if equals zero new page will be created
     * @param string $lang_code     2 letters language code
     * @param bool   $create        True if page was created, falce otherwise
     * @param array  $old_page_data Page data before update
     */
    fn_set_hook('update_page_post', $page_data, $page_id, $lang_code, $create, $old_page_data);
    return $page_id;
}
예제 #2
0
function fn_update_page($page_data, $page_id = 0, $lang_code = CART_LANGUAGE)
{
    if (!empty($page_data['avail_from_timestamp'])) {
        $page_data['avail_from_timestamp'] = fn_parse_date($page_data['avail_from_timestamp']);
    } else {
        $page_data['avail_from_timestamp'] = 0;
    }
    if (!empty($page_data['avail_till_timestamp'])) {
        $page_data['avail_till_timestamp'] = fn_parse_date($page_data['avail_till_timestamp']) + 86399;
    } else {
        $page_data['avail_till_timestamp'] = 0;
    }
    if (isset($page_data['usergroup_ids'])) {
        $page_data['usergroup_ids'] = empty($page_data['usergroup_ids']) ? '' : implode(',', $page_data['usergroup_ids']);
    }
    $_data = $page_data;
    if (isset($page_data['timestamp'])) {
        $_data['timestamp'] = fn_parse_date($page_data['timestamp']);
    }
    if (isset($_data['localization'])) {
        $_data['localization'] = empty($_data['localization']) ? '' : fn_implode_localizations($_data['localization']);
    }
    fn_set_company_id($_data);
    if (empty($page_id)) {
        // page title required
        if (empty($page_data['page'])) {
            return false;
        }
        // add new page
        $_data['page_id'] = $page_id = db_query('INSERT INTO ?:pages ?e', $_data);
        foreach ((array) Registry::get('languages') as $_data['lang_code'] => $v) {
            db_query('INSERT INTO ?:page_descriptions ?e', $_data);
        }
        // now we need to update 'id_path' field, as we know $page_id
        /* Generate id_path for page */
        $parent_id = intval($_data['parent_id']);
        if ($parent_id == 0) {
            $id_path = $page_id;
        } else {
            $id_path = db_get_row("SELECT id_path FROM ?:pages WHERE page_id = ?i", $parent_id);
            $id_path = $id_path['id_path'] . '/' . $page_id;
        }
        db_query('UPDATE ?:pages SET ?u WHERE page_id = ?i', array('id_path' => $id_path), $page_id);
    } else {
        if (!fn_check_company_id('pages', 'page_id', $page_id)) {
            fn_company_access_denied_notification();
            return false;
        }
        $old_company_id = fn_get_company_id('pages', 'page_id', $page_id);
        if ($_data['company_id'] != $old_company_id) {
            fn_change_page_company($page_id, $_data['company_id']);
        }
        // page title is not updated
        if (empty($page_data['page'])) {
            unset($page_data['page']);
        }
        // update existing page
        db_query('UPDATE ?:pages SET ?u WHERE page_id = ?i', $_data, $page_id);
        db_query('UPDATE ?:page_descriptions SET ?u WHERE page_id = ?i AND lang_code = ?s', $_data, $page_id, $lang_code);
        // regenerate id_path for child pages
        if (isset($page_data['parent_id'])) {
            fn_change_page_parent($page_id, $page_data['parent_id']);
        }
    }
    if (!empty($page_data['block_id'])) {
        fn_add_items_to_block($page_data['block_id'], $page_data['add_items'], $page_id, 'pages');
    }
    fn_set_hook('update_page', $page_data, $page_id, $lang_code);
    return $page_id;
}