示例#1
0
function run()
{
    global $user;
    global $layout;
    global $DB;
    global $website;
    global $theme;
    $out = '';
    $item = new structure();
    switch ($_REQUEST['act']) {
        case 'load':
        case 'edit':
        case 2:
            // edit/new form
            if (!empty($_REQUEST['id'])) {
                $item->load(intval($_REQUEST['id']));
            }
            if (isset($_REQUEST['form-sent'])) {
                $item->load_from_post();
                try {
                    $item->save();
                    property::save_properties_from_post('structure', $item->id);
                    $item = $item->reload();
                    // reorder associated category elements
                    if (!empty($_POST['elements-order'])) {
                        $response = item::reorder($_POST['elements-order']);
                        if ($response !== true) {
                            throw new Exception($response);
                        }
                    }
                    $layout->navigate_notification(t(53, "Data saved successfully."), false, false, 'fa fa-check');
                } catch (Exception $e) {
                    $layout->navigate_notification($e->getMessage(), true, true);
                }
                if (!empty($item->id)) {
                    users_log::action($_REQUEST['fid'], $item->id, 'save', $item->dictionary[$website->languages_list[0]]['title'], json_encode($_REQUEST));
                }
            } else {
                if (!empty($item->id)) {
                    users_log::action($_REQUEST['fid'], $item->id, 'load', $item->dictionary[$website->languages_list[0]]['title']);
                }
            }
            $out = structure_form($item);
            break;
        case 3:
        case "reorder":
            $ok = structure::reorder($_REQUEST['parent'], $_REQUEST['children_order']);
            echo json_encode($ok);
            core_terminate();
            break;
        case "homepager":
            $node = $_REQUEST['node'];
            $website->homepage = $node;
            $ok = $website->save();
            echo json_encode($ok);
            core_terminate();
            break;
        case 4:
        case "remove":
            if (!empty($_REQUEST['id'])) {
                $item->load(intval($_REQUEST['id']));
                if ($item->delete() > 0) {
                    $layout->navigate_notification(t(55, 'Item removed successfully.'), false);
                    $structure = structure::hierarchy(-1);
                    // root level (0) including Web node (-1)
                    $out = structure_tree($structure);
                    users_log::action($_REQUEST['fid'], $item->id, 'remove');
                } else {
                    $layout->navigate_notification(t(56, 'Unexpected error.'), false);
                    $out = structure_form($item);
                }
            }
            break;
        case 95:
            // free path checking
            $path = $_REQUEST['path'];
            $id = $_REQUEST['id'];
            $DB->query('SELECT type, object_id, lang
	 					  FROM nv_paths
						 WHERE path = ' . protect($path) . '
						   AND website = ' . $website->id);
            $rs = $DB->result();
            echo json_encode($rs);
            core_terminate();
            break;
        case "category_path":
            // return category paths
            echo json_encode(path::loadElementPaths('structure', intval($_REQUEST['id'])));
            core_terminate();
            break;
        case 'json_find_item':
            // find items by its title
            // the items must have its own path (free OR not embedded to a category)
            $DB->query('
				SELECT SQL_CALC_FOUND_ROWS nvw.node_id as id, nvw.text as text
				  FROM nv_webdictionary nvw, nv_items nvi
				 WHERE nvw.node_type = "item"
				   AND nvw.node_id = nvi.id
				   AND nvw.subtype = "title"
				   AND (	nvi.association = "free" OR
				            (nvi.association = "category" AND nvi.embedding=0)
				   )
				   AND nvw.lang = ' . protect($_REQUEST['lang']) . '
				   AND nvw.website = ' . $website->id . '
				   AND nvw.website = nvi.website
				   AND nvw.text LIKE ' . protect('%' . $_REQUEST['title'] . '%') . '
		      ORDER BY nvw.text ASC
			     LIMIT ' . intval($_REQUEST['page_limit']) . '
			     OFFSET ' . max(0, intval($_REQUEST['page_limit']) * (intval($_REQUEST['page']) - 1)), 'array');
            $rows = $DB->result();
            $total = $DB->foundRows();
            echo json_encode(array('items' => $rows, 'totalCount' => $total));
            core_terminate();
            break;
        case "search_by_title":
            // json search title request (for "copy from" properties dialog)
            $DB->query('
				SELECT node_id as id, text as label, text as value
					  FROM nv_webdictionary
					 WHERE node_type = "structure"
					   AND subtype = "title"
					   AND lang = ' . protect($_REQUEST['lang']) . '
					   AND website = ' . $website->id . '
					   AND text LIKE ' . protect('%' . $_REQUEST['title'] . '%') . '
			      ORDER BY text ASC
				     LIMIT 30', 'array');
            echo json_encode($DB->result());
            core_terminate();
            break;
        case "copy_from_template_zones":
            // return template properties for a structure id
            $item = new structure();
            $item->load(intval($_REQUEST['id']));
            $template = new template();
            $template->load($item->template);
            $zones = array();
            for ($ps = 0; $ps < count($template->properties); $ps++) {
                // ignore non structure properties
                if (!isset($template->properties[$ps]->element) || $template->properties[$ps]->element != 'structure') {
                    continue;
                }
                // ignore non-textual properties
                if (!in_array($template->properties[$ps]->type, array("text", "textarea", "rich_textarea"))) {
                    continue;
                }
                $title = $template->properties[$ps]->name;
                if (!empty($theme)) {
                    $title = $theme->t($title);
                }
                $zones[] = array('type' => 'property', 'code' => $template->properties[$ps]->id, 'title' => $title);
            }
            echo json_encode($zones);
            core_terminate();
            break;
        case "raw_zone_content":
            // return raw item contents
            if ($_REQUEST['zone'] == 'property') {
                $DB->query('SELECT text
							  FROM nv_webdictionary
							 WHERE node_type = "property-structure"
							   AND subtype = ' . protect('property-' . $_REQUEST['section'] . '-' . $_REQUEST['lang']) . '
							   AND lang = ' . protect($_REQUEST['lang']) . '
							   AND website = ' . $website->id . '
							   AND node_id = ' . protect($_REQUEST['node_id']), 'array');
                $data = $DB->first();
                echo $data['text'];
            }
            core_terminate();
            break;
        case 'votes_reset':
            webuser_vote::remove_object_votes('structure', intval($_REQUEST['id']));
            echo 'true';
            core_terminate();
            break;
        case 'votes_by_webuser':
            if ($_POST['oper'] == 'del') {
                $ids = explode(',', $_POST['id']);
                for ($i = 0; $i < count($ids); $i++) {
                    if ($ids[$i] > 0) {
                        $vote = new webuser_vote();
                        $vote->load($ids[$i]);
                        $vote->delete();
                    }
                }
                webuser_vote::update_object_score('structure', $vote->object_id);
                echo 'true';
                core_terminate();
            }
            $max = intval($_GET['rows']);
            $page = intval($_GET['page']);
            $offset = ($page - 1) * $max;
            if ($_REQUEST['_search'] == 'false') {
                list($dataset, $total) = webuser_vote::object_votes_by_webuser('structure', intval($_REQUEST['id']), $_REQUEST['sidx'] . ' ' . $_REQUEST['sord'], $offset, $max);
            }
            $out = array();
            for ($i = 0; $i < count($dataset); $i++) {
                if (empty($dataset[$i])) {
                    continue;
                }
                $out[$i] = array(0 => $dataset[$i]['id'], 1 => core_ts2date($dataset[$i]['date'], true), 2 => $dataset[$i]['username']);
            }
            navitable::jqgridJson($out, $page, $offset, $max, $total);
            core_terminate();
            break;
        case 0:
            // tree / search result
        // tree / search result
        default:
            $structure = structure::hierarchy(-1);
            // root level (0) including Web node (-1)
            $out = structure_tree($structure);
            break;
    }
    return $out;
}