Ejemplo n.º 1
0
/**
* Assign vars to Smarty var, then this var can be used as index of the resource
* @param int Id of the section parent
* @param int Jumps (level)
* @param object Resource (owner)
* @param string Smarty var to append
* @param string Index number to add (eg. 1.1)
* @param bool Indicates if the array will be assigned to Smarty var or not
* @param array Reference to an array for fill.
* @return empty
*/
function assignSectionTree($parent = 0, $jumps = 0, AHResource $res, $var = 'index', $number = '', $assign = true, &$array = null)
{
    global $tpl;
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    if (get_class($res) != 'AHResource') {
        return;
    }
    $sql = "SELECT * FROM " . $db->prefix("pa_sections") . " WHERE " . ($res->id() > 0 ? "id_res='" . $res->id() . "' AND" : '') . "\n\t\t\tparent='{$parent}' ORDER BY `order`";
    $result = $db->query($sql);
    $sec = new AHSection();
    $i = 1;
    // Counter
    $num = 1;
    while ($row = $db->fetchArray($result)) {
        $sec->assignVars($row);
        $link = ah_make_link($res->nameId() . '/' . $sec->nameId());
        if ($assign) {
            $tpl->append($var, array('title' => $sec->title(), 'nameid' => $sec->nameId(), 'jump' => $jumps, 'link' => $link, 'number' => $jumps == 0 ? $num : ($number != '' ? $number . '.' : '') . $i));
        } else {
            $array[] = array('title' => $sec->title(), 'nameid' => $sec->nameId(), 'jump' => $jumps, 'link' => $link, 'number' => $jumps == 0 ? $num : ($number != '' ? $number . '.' : '') . $i);
        }
        assignSectionTree($sec->id(), $jumps + 1, $res, $var, ($number != '' ? $number . '.' : '') . $i, $assign, $array);
        $i++;
        if ($jumps == 0) {
            $num++;
        }
    }
    return true;
}
Ejemplo n.º 2
0
/**
* @desc Almacena toda la información referente a la sección
**/
function saveSection($edit = 0, $ret = 0)
{
    global $xoopsUser, $xoopsModuleConfig;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    //Verifica si se proporcionó una publicación para la sección
    if ($res <= 0) {
        redirect_header(RDURL, 1, __('Operation not allowed!', 'docs'));
        die;
    }
    //Verifica si la publicación existe
    $res = new RDResource($res);
    if ($res->isNew()) {
        redirect_header(RDURL, 1, __('Operation not allowed!', 'docs'));
        die;
    }
    //Verificamos si es una publicación aprobada
    if (!$res->getVar('approved')) {
        redirect_header(RDURL, 2, __('This Document has not been approved yet!', 'docs'));
        die;
    }
    // TODO: Crear el link correcto de retorno
    if ($xoopsModuleConfig['permalinks']) {
        $retlink = RDFunctions::url() . '/list/' . $res->getVar('nameid') . '/';
    } else {
        $retlink = RDFunctions::url() . '?page=edit&action=list&res=' . $res->id();
    }
    //Verificamos si el usuario tiene permisos de edicion
    if (!$xoopsUser->uid() == $res->getVar('owner') && !$res->isEditor($xoopsUser->uid()) && !$xoopsUser->isAdmin()) {
        redirect_header(RDURL, 2, __('You can not edit this content!', 'docs'));
        die;
    }
    $db = XoopsDatabaseFactory::getDatabaseConnection();
    if ($edit) {
        //Verifica si la sección es válida
        if ($id == '') {
            redirect_header($retlink, 1, __('Specified section is not valid!', 'docs'));
            die;
        }
        //Comprueba si la sección es existente
        $sec = new RDSection($id);
        if ($sec->isNew()) {
            redirect_header($retlink, 1, __('Specified section does not exists!', 'docs'));
            die;
        }
        //Comprueba que el título de la sección no exista
        $sql = "SELECT COUNT(*) FROM " . $db->prefix('rd_sections') . " WHERE title='{$title}' AND id_res='{$res}' AND id_sec<>" . $sec->id();
        list($num) = $db->fetchRow($db->queryF($sql));
        if ($num > 0) {
            redirect_header($sec->editlink(), 1, __('Already exists another section with same title!', 'docs'));
            die;
        }
        /**
         * Comprobamos si debemos almacenar las ediciones en la
         * tabla temporal o directamente en la tabla de secciones
         */
        if (!$res->getVar('editor_approve') && !$xoopsUser->isAdmin()) {
            $sec = new RDEdit(null, $id_sec);
        }
    } else {
        //Comprueba que el título de la sección no exista
        $sql = "SELECT COUNT(*) FROM " . $db->prefix('rd_sections') . " WHERE title='{$title}' AND id_res='" . $res->id() . "'";
        list($num) = $db->fetchRow($db->queryF($sql));
        if ($num > 0) {
            redirect_header(ah_make_link('publish/' . $res->nameId() . '/'), 1, _MS_AH_ERRTITLE);
            die;
        }
        $sec = new RDSection();
    }
    //Genera $nameid Nombre identificador
    if ($title != $sec->getVar('title')) {
        $found = false;
        $i = 0;
        do {
            $nameid = TextCleaner::getInstance()->sweetstring($title) . ($found ? $i : '');
            $sql = "SELECT COUNT(*) FROM " . $db->prefix('rd_sections') . " WHERE nameid = '{$nameid}'";
            list($num) = $db->fetchRow($db->queryF($sql));
            if ($num > 0) {
                $found = true;
                $i++;
            } else {
                $found = false;
            }
        } while ($found == true);
    }
    if (!$res->getVar('editor_approve') && !$xoopsUser->isAdmin() && !($res->getVar('owner') == $xoopsUser->uid())) {
        $sec->setVar('id_sec', $id);
    }
    $sec->setVar('title', $title);
    $sec->setVar('content', $content);
    $sec->setVar('order', $order);
    $sec->setVar('id_res', $res->id());
    isset($nameid) ? $sec->setVar('nameid', $nameid) : '';
    $sec->setVar('parent', $parent);
    $sec->setVar('uid', $xoopsUser->uid());
    $sec->setVar('uname', $xoopsUser->uname());
    if ($edit) {
        $sec->setVar('modified', time());
    } else {
        $sec->setVar('created', time());
        $sec->setVar('modified', time());
    }
    // Metas
    if ($edit) {
        $sec->clear_metas();
    }
    // Clear all metas
    // Initialize metas array if not exists
    if (!isset($metas)) {
        $metas = array();
    }
    // Get meta key if "select" is visible
    if (isset($meta_name_sel) && $meta_name_sel != '') {
        $meta_name = $meta_name_sel;
    }
    // Add meta to metas array
    if (isset($meta_name) && $meta_name != '') {
        array_push($metas, array('key' => $meta_name, 'value' => $meta_value));
    }
    // Assign metas
    foreach ($metas as $value) {
        $sec->add_meta($value['key'], $value['value']);
    }
    RMEvents::get()->run_event('docs.saving.section', $sec);
    if (!$sec->save()) {
        redirect_header($sec->editlink(), 3, __('Section could not be saved!', 'docs'));
    } else {
        if ($edit) {
            $sec = new RDSection($sec->getVar('id_sec'));
        }
        if ($return == 1) {
            redirect_header($sec->permalink(), 1, __('Database updated successfully!', 'docs'));
        } elseif ($return == 2) {
            redirect_header($sec->editlink(), 1, __('Database updated successfully!', 'docs'));
        } else {
            redirect_header($retlink, 1, __('Database updated successfully!', 'docs'));
        }
    }
}