Ejemplo n.º 1
0
function run()
{
    global $layout;
    global $DB;
    global $website;
    $out = '';
    $item = new block();
    switch ($_REQUEST['act']) {
        case 'json':
        case 1:
            // json data retrieval & operations
            switch ($_REQUEST['oper']) {
                case 'del':
                    // remove rows
                    $ids = $_REQUEST['ids'];
                    foreach ($ids as $id) {
                        $item->load($id);
                        $item->delete();
                    }
                    echo json_encode(true);
                    break;
                default:
                    // list or search
                    // translation of request search & order fields
                    switch ($_REQUEST['searchField']) {
                        case 'id':
                            $_REQUEST['searchField'] = 'b.id';
                            break;
                        case 'type':
                            $_REQUEST['searchField'] = 'b.type';
                            break;
                        case 'title':
                            $_REQUEST['searchField'] = 'd.text';
                            break;
                        case 'category':
                            $_REQUEST['searchField'] = 'b.category';
                            break;
                        case 'dates':
                            $_REQUEST['searchField'] = 'b.date_published';
                            break;
                        case 'enabled':
                            $_REQUEST['searchField'] = 'b.enabled';
                            break;
                        case 'date_modified':
                        default:
                            $_REQUEST['searchField'] = 'b.date_modified';
                    }
                    if ($_REQUEST['sidx'] == 'dates') {
                        $_REQUEST['sidx'] = 'b.date_published';
                    }
                    $page = intval($_REQUEST['page']);
                    $max = intval($_REQUEST['rows']);
                    $offset = ($page - 1) * $max;
                    $orderby = $_REQUEST['sidx'] . ' ' . $_REQUEST['sord'];
                    $where = " 1=1 ";
                    if ($_REQUEST['_search'] == 'true' || isset($_REQUEST['quicksearch'])) {
                        if (isset($_REQUEST['quicksearch'])) {
                            $where .= $item->quicksearch($_REQUEST['quicksearch']);
                        } else {
                            if (isset($_REQUEST['filters'])) {
                                $where .= navitable::jqgridsearch($_REQUEST['filters']);
                                // special case
                                if (strpos($where, 'title LIKE') !== false) {
                                    $where = substr_replace($where, 'd.text', strpos($where, 'title LIKE'), 5);
                                }
                            } else {
                                // single search
                                $where .= ' AND ' . navitable::jqgridcompare($_REQUEST['searchField'], $_REQUEST['searchOper'], $_REQUEST['searchString']);
                            }
                        }
                    }
                    $sql = ' SELECT SQL_CALC_FOUND_ROWS b.*, d.text as title 
							   FROM nv_blocks b
						  LEFT JOIN nv_webdictionary d
						  		 	 ON b.id = d.node_id
								 	AND d.node_type = "block"
									AND d.subtype = "title"
									AND d.lang = "' . $website->languages_list[0] . '"
									AND d.website = ' . $website->id . '
							  WHERE ' . $where . '
							    AND b.website = ' . $website->id . ' 
						   ORDER BY ' . $orderby . ' 
							  LIMIT ' . $max . '
							 OFFSET ' . $offset;
                    if (!$DB->query($sql, 'array')) {
                        throw new Exception($DB->get_last_error());
                    }
                    $dataset = $DB->result();
                    $total = $DB->foundRows();
                    $block_types = block::types();
                    $block_types_list = array();
                    for ($i = 0; $i < count($block_types); $i++) {
                        if (is_numeric($block_types[$i]['id'])) {
                            $block_types_list[$block_types[$i]['code']] = $block_types[$i]['title'];
                        } else {
                            $block_types_list[$block_types[$i]['id']] = $block_types[$i]['title'];
                        }
                    }
                    $dataset = grid_notes::summary($dataset, 'block', 'id');
                    // we need to format the values and retrieve the needed strings from the dictionary
                    $out = array();
                    for ($i = 0; $i < count($dataset); $i++) {
                        if (empty($dataset[$i])) {
                            continue;
                        }
                        $access = array(0 => '<img src="img/icons/silk/page_white_go.png" align="absmiddle" title="' . t(254, 'Everybody') . '" />', 1 => '<img src="img/icons/silk/lock.png" align="absmiddle" title="' . t(361, 'Web users only') . '" />', 2 => '<img src="img/icons/silk/user_gray.png" align="absmiddle" title="' . t(363, 'Users who have not yet signed up or signed in') . '" />', 3 => '<img src="img/icons/silk/group_key.png" align="absmiddle" title="' . t(512, "Selected web user groups") . '" />');
                        if (empty($dataset[$i]['date_published'])) {
                            $dataset[$i]['date_published'] = '&infin;';
                        } else {
                            $dataset[$i]['date_published'] = core_ts2date($dataset[$i]['date_published'], false);
                        }
                        if (empty($dataset[$i]['date_unpublish'])) {
                            $dataset[$i]['date_unpublish'] = '&infin;';
                        } else {
                            $dataset[$i]['date_unpublish'] = core_ts2date($dataset[$i]['date_unpublish'], false);
                        }
                        if ($dataset[$i]['category'] > 0) {
                            $dataset[$i]['category'] = $DB->query_single('text', 'nv_webdictionary', ' 	node_type = "structure" AND
                                    node_id = "' . $dataset[$i]['category'] . '" AND
                                    subtype = "title" AND
                                    lang = "' . $website->languages_list[0] . '"
                                ');
                        }
                        $out[$i] = array(0 => $dataset[$i]['id'], 1 => $block_types_list[$dataset[$i]['type']], 2 => '<div class="list-row" data-enabled="' . $dataset[$i]['enabled'] . '">' . $dataset[$i]['title'] . '</div>', 3 => $dataset[$i]['date_published'] . ' - ' . $dataset[$i]['date_unpublish'], 4 => $access[$dataset[$i]['access']], 5 => $dataset[$i]['enabled'] == 1 ? '<img src="img/icons/silk/accept.png" />' : '<img src="img/icons/silk/cancel.png" />', 6 => $dataset[$i]['_grid_notes_html']);
                    }
                    navitable::jqgridJson($out, $page, $offset, $max, $total);
                    break;
            }
            session_write_close();
            exit;
            break;
        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('block', $item->id);
                    $id = $item->id;
                    // set block order
                    if (!empty($item->type) && !empty($_REQUEST['blocks-order'])) {
                        block::reorder($item->type, $_REQUEST['blocks-order'], $_REQUEST['blocks-order-fixed']);
                    }
                    unset($item);
                    $item = new block();
                    $item->load($id);
                    $layout->navigate_notification(t(53, "Data saved successfully."), false, false, 'fa fa-check');
                } catch (Exception $e) {
                    $layout->navigate_notification($e->getMessage(), true, true);
                }
                users_log::action($_REQUEST['fid'], $item->id, 'save', $item->dictionary[$website->languages_list[0]]['title'], json_encode($_REQUEST));
            } else {
                users_log::action($_REQUEST['fid'], $item->id, 'load', $item->dictionary[$website->languages_list[0]]['title']);
            }
            $out = blocks_form($item);
            break;
        case 'delete':
        case 4:
            // remove
            if (!empty($_REQUEST['id'])) {
                $item->load(intval($_REQUEST['id']));
                if ($item->delete() > 0) {
                    $layout->navigate_notification(t(55, 'Item removed successfully.'), false);
                    $out = blocks_list();
                } else {
                    $layout->navigate_notification(t(56, 'Unexpected error.'), false);
                    $out = blocks_form($item);
                }
                users_log::action($_REQUEST['fid'], $item->id, 'remove', $item->dictionary[$website->languages_list[0]]['title']);
            }
            break;
        case 'path':
        case 5:
            // search an existing path
            $DB->query('SELECT path as id, path as label, path as value
						  FROM nv_paths
						 WHERE path LIKE ' . protect('%' . $_REQUEST['term'] . '%') . ' 
						   AND website = ' . $website->id . '
				      ORDER BY path ASC
					     LIMIT 10', 'array');
            echo json_encode($DB->result());
            core_terminate();
            break;
        case 'block_groups_list':
            $out = block_groups_list();
            break;
        case 'block_groups_json':
            // block groups: json data retrieval
            $page = intval($_REQUEST['page']);
            $max = intval($_REQUEST['rows']);
            $offset = ($page - 1) * $max;
            list($rs, $total) = block_group::paginated_list($offset, $max, $_REQUEST['sidx'], $_REQUEST['sord']);
            $rs = grid_notes::summary($rs, 'block_group', 'id');
            // translate $rs to an array of ordered fields
            foreach ($rs as $row) {
                if (substr($row['blocks'], 0, 2) == 'a:') {
                    // nv < 2.1
                    $row['blocks'] = mb_unserialize($row['blocks']);
                } else {
                    // nv >= 2.1
                    $row['blocks'] = json_decode($row['blocks'], true);
                }
                $dataset[] = array('id' => $row['id'], 'code' => $row['code'], 'title' => $row['title'], 'blocks' => count($row['blocks']), 'notes' => $row['_grid_notes_html']);
            }
            navitable::jqgridJson($dataset, $page, $offset, $max, $total, 'id');
            session_write_close();
            exit;
            break;
        case 'block_group_edit':
            $item = new block_group();
            if (!empty($_REQUEST['id'])) {
                $item->load(intval($_REQUEST['id']));
            }
            if (isset($_REQUEST['form-sent'])) {
                $item->load_from_post();
                try {
                    $item->save();
                    $layout->navigate_notification(t(53, "Data saved successfully."), false, false, 'fa fa-check');
                } catch (Exception $e) {
                    $layout->navigate_notification($e->getMessage(), true, true);
                }
                users_log::action($_REQUEST['fid'], $item->id, 'save', $item->title, json_encode($_REQUEST));
            } else {
                if (!empty($_REQUEST['id'])) {
                    users_log::action($_REQUEST['fid'], $item->id, 'edit', $item->title);
                }
            }
            $out = block_group_form($item);
            break;
        case 'block_group_delete':
            $item = new block_group();
            if (!empty($_REQUEST['id'])) {
                $item->load(intval($_REQUEST['id']));
                if ($item->delete() > 0) {
                    $layout->navigate_notification(t(55, 'Item removed successfully.'), false);
                    $out = block_groups_list();
                } else {
                    $layout->navigate_notification(t(56, 'Unexpected error.'), false);
                    $out = block_group_form($item);
                }
                users_log::action($_REQUEST['fid'], $item->id, 'remove', $item->title);
            }
            break;
        case 'block_types_list':
            $out = blocks_types_list();
            break;
        case 'block_types_json':
            // block types: json data retrieval
            $page = intval($_REQUEST['page']);
            $max = intval($_REQUEST['rows']);
            $offset = ($page - 1) * $max;
            $rs = block::types($_REQUEST['sidx'], $_REQUEST['sord']);
            $block_modes = block::modes();
            // translate $rs to an array of ordered fields
            foreach ($rs as $row) {
                $dataset[] = array('id' => $row['id'], 'type' => $block_modes[$row['type']], 'code' => $row['code'], 'title' => $row['title'], 'width' => $row['width'], 'height' => $row['height']);
            }
            $total = count($dataset);
            navitable::jqgridJson($dataset, $page, $offset, $max, $total, 'id');
            session_write_close();
            exit;
            break;
        case 'block_type_edit':
        case 82:
            // edit/create block type
            $item = NULL;
            $position = NULL;
            $max_id = 0;
            $dataset = block::custom_types();
            for ($i = 0; $i < count($dataset); $i++) {
                if ($dataset[$i]['id'] > $max_id) {
                    $max_id = $dataset[$i]['id'];
                }
                if ($dataset[$i]['id'] == $_REQUEST['id']) {
                    $item = $dataset[$i];
                    $position = $i;
                }
            }
            if (empty($item)) {
                $layout->navigate_notification(t(599, "Sorry, can't display a theme block type info."));
                $out = blocks_types_list();
            } else {
                if (isset($_REQUEST['form-sent'])) {
                    if (empty($item)) {
                        $item = array('id' => $max_id + 1);
                    }
                    $item['type'] = $_REQUEST['type'];
                    $item['title'] = $_REQUEST['title'];
                    $item['code'] = $_REQUEST['code'];
                    $item['width'] = $_REQUEST['width'];
                    $item['height'] = $_REQUEST['height'];
                    $item['order'] = $_REQUEST['order'];
                    $item['maximum'] = $_REQUEST['maximum'];
                    $item['notes'] = pquotes($_REQUEST['notes']);
                    if (!is_null($position)) {
                        $dataset[$position] = $item;
                    } else {
                        $dataset[] = $item;
                    }
                    try {
                        // save
                        $ok = block::types_update($dataset);
                        $layout->navigate_notification(t(53, "Data saved successfully."), false, false, 'fa fa-check');
                    } catch (Exception $e) {
                        $layout->navigate_notification($e->getMessage(), true, true);
                    }
                }
                $out = blocks_type_form($item);
            }
            break;
        case 'block_type_delete':
        case 84:
            // remove block type
            $dataset = block::custom_types();
            $item = NULL;
            for ($i = 0; $i < count($dataset); $i++) {
                if ($dataset[$i]['id'] == $_REQUEST['id']) {
                    unset($dataset[$i]);
                    break;
                }
            }
            try {
                block::types_update($dataset);
                $layout->navigate_notification(t(55, 'Item removed successfully.'), false);
                $out = blocks_types_list();
            } catch (Exception $e) {
                $out = $layout->navigate_message("error", t(23, 'Blocks'), t(56, 'Unexpected error.'));
            }
            break;
        case 'block_property_load':
            $property = new property();
            if (!empty($_REQUEST['id'])) {
                if (is_numeric($_REQUEST['id'])) {
                    $property->load(intval($_REQUEST['id']));
                } else {
                    $property->load_from_theme($_REQUEST['id'], null, 'block', $_REQUEST['block']);
                }
            }
            header('Content-type: text/json');
            $types = property::types();
            $property->type_text = $types[$property->type];
            echo json_encode($property);
            session_write_close();
            exit;
            break;
        case 'block_property_save':
            // save property details
            $property = new property();
            if (!empty($_REQUEST['property-id'])) {
                $property->load(intval($_REQUEST['property-id']));
            }
            $property->load_from_post();
            $property->save();
            header('Content-type: text/json');
            $types = property::types();
            $property->type_text = $types[$property->type];
            echo json_encode($property);
            session_write_close();
            exit;
            break;
        case 'block_property_remove':
            // remove property
            $property = new property();
            if (!empty($_REQUEST['property-id'])) {
                $property->load(intval($_REQUEST['property-id']));
            }
            $property->delete();
            session_write_close();
            exit;
            break;
        case 'block_group_block_options':
            $status = null;
            $block_group = $_REQUEST['block_group'];
            $block_code = $_REQUEST['code'];
            $block_uid = $_REQUEST['block_uid'];
            if (isset($_REQUEST['form-sent'])) {
                $status = property::save_properties_from_post('block_group_block', $block_code, $block_group, $block_code, $block_uid);
            }
            $out = block_group_block_options($block_group, $block_code, $block_uid, $status);
            echo $out;
            core_terminate();
            break;
        case 'block_group_extension_block_options':
            $status = null;
            $block_group = $_REQUEST['block_group'];
            // block_group type
            $block_id = $_REQUEST['block_id'];
            // extension block id (type)
            $block_uid = $_REQUEST['block_uid'];
            // extension block unique id
            $block_extension = $_REQUEST['block_extension'];
            // extension name
            if (isset($_REQUEST['form-sent'])) {
                $status = property::save_properties_from_post('extension_block', $block_group, $block_id, null, $block_uid);
            }
            $out = block_group_extension_block_options($block_group, $block_extension, $block_id, $block_uid, $status);
            echo $out;
            core_terminate();
            break;
        case 0:
            // list / search result
        // list / search result
        default:
            $out = blocks_list();
            break;
    }
    return $out;
}