/system/cms/modules/navigation/controllers/admin.php
PHP | 473 lines | 299 code | 80 blank | 94 comment | 27 complexity | 4579c3774dc9fc9ecbf48c474b672477 MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, CC0-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT
- <?php
- use Pyro\Module\Users;
- use Pyro\Module\Navigation;
- use Pyro\Module\Pages;
- use Pyro\Module\Addons;
- /**
- * Admin controller for the navigation module. Handles actions such as editing links or creating new ones.
- *
- * @author PyroCMS Development Team
- * @package PyroCMS\Core\Modules\Navigation\Controllers
- *
- */
- class Admin extends Admin_Controller
- {
- /**
- * The current active section.
- *
- * @var int
- */
- protected $section = 'links';
- /**
- * The array containing the rules for the navigation items.
- *
- * @var array
- */
- private $validation_rules = array(
- array(
- 'field' => 'title',
- 'label' => 'lang:global:title',
- 'rules' => 'trim|required|max_length[100]'
- ),
- array(
- 'field' => 'link_type',
- 'label' => 'lang:nav_type_label',
- 'rules' => 'trim|required|alpha|callback__link_check'
- ),
- array(
- 'field' => 'url',
- 'label' => 'lang:nav_url_label',
- 'rules' => 'trim'
- ),
- array(
- 'field' => 'uri',
- 'label' => 'lang:nav_uri_label',
- 'rules' => 'trim'
- ),
- array(
- 'field' => 'module_name',
- 'label' => 'lang:nav_module_label',
- 'rules' => 'trim|alpha_dash'
- ),
- array(
- 'field' => 'page_id',
- 'label' => 'lang:nav_page_label',
- 'rules' => 'trim|numeric'
- ),
- array(
- 'field' => 'navigation_group_id',
- 'label' => 'lang:nav_group_label',
- 'rules' => 'trim|numeric'
- ),
- array(
- 'field' => 'current_group_id',
- 'label' => 'lang:nav_group_label',
- 'rules' => 'trim|numeric'
- ),
- array(
- 'field' => 'target',
- 'label' => 'lang:nav_target_label',
- 'rules' => 'trim|max_length[10]'
- ),
- array(
- 'field' => 'restricted_to',
- 'label' => 'lang:nav_restricted_to',
- 'rules' => ''
- ),
- array(
- 'field' => 'class',
- 'label' => 'lang:nav_class_label',
- 'rules' => 'trim'
- )
- );
- /**
- * Constructor method
- */
- public function __construct()
- {
- parent::__construct();
- // Load the required classes
- $this->load->library('form_validation');
- $this->lang->load('navigation');
- $this->template
- ->append_js('module::navigation.js')
- ->append_css('module::navigation.css');
- // Get Navigation Groups
- $this->template->groups = Navigation\Model\Group::all();
- $this->template->groups_select = Navigation\Model\Group::getGroupOptions();
- $all_modules = $this->moduleManager->getAll(array('is_frontend'=>true));
- //only allow modules that user has permissions for
- foreach($all_modules as $module) {
- if ($this->current_user->hasAccess($module['slug']) or $this->current_user->isSuperUser()) {
- $modules[] = $module;
- }
- }
- $this->template->modules_select = array_for_select($modules, 'slug', 'name');
- // Get Pages and create pages tree
- $tree = array();
- if ($pages = Pages\Model\Page::all()) {
- foreach ($pages as $page) {
- $tree[$page->parent_id][] = $page;
- }
- }
- unset($pages);
- $this->template->pages_select = $tree;
- // Set the validation rules for the navigation items
- $this->form_validation->set_rules($this->validation_rules);
- // Inject the model.
- $this->links = new Navigation\Model\Link();
- }
- /**
- * List all navigation elements
- */
- public function index()
- {
- $navigation = array();
- // Go through all the groups
- foreach ($this->template->groups as $group) {
- //... and get navigation links for each one
- $navigation[$group->id] = Navigation\Model\Link::getTreeByGroup($group->id);
- }
- // Create the layout
- $this->template
- ->append_js('jquery/jquery.ui.nestedSortable.js')
- ->append_js('jquery/jquery.cooki.js')
- ->title($this->module_details['name'])
- ->set('navigation', $navigation)
- ->build('admin/index');
- }
- /**
- * Order the links and record their children
- */
- public function order()
- {
- $order = $this->input->post('order');
- $data = $this->input->post('data');
- $group = isset($data['group']) ? (int) $data['group'] : 0;
- if (is_array($order)) {
- Navigation\Model\Link::setOrder($order, $group);
- $this->links->flushCacheCollection();
- Events::trigger('post_navigation_order', array(
- 'order' => $order,
- 'group' => $group
- ));
- }
- }
- /**
- * Get the details of a link using Ajax
- *
- * @param int $link_id The ID of the link
- */
- public function ajax_link_details($link_id)
- {
- $link = Navigation\Model\Link::getUrl($link_id);
- $ids = explode(',', $link->restricted_to);
- $group_options = Users\Model\Group::findManyGroupOptionsInId($ids);
- $link->restricted_to = implode(', ', $group_options);
- $this->load->view('admin/ajax/link_details', array('link' => $link));
- }
- /**
- * Create a new navigation item
- *
- * @param string $group_id
- *
- * @return
- */
- public function create($group_id = '')
- {
- // Set the options for restricted to
- $this->template->group_options = Users\Model\Group::getGroupOptions();
- // Run if valid
- if ($this->form_validation->run()) {
- $last_position = Navigation\Model\Link::findByGroupIdAndOrderByPosition($this->input->post('navigation_group_id'),'desc');
- $link = Navigation\Model\Link::create(array(
- 'title' => $this->input->post('title'),
- 'parent' => $this->input->post('parent') ? $this->input->post('parent') : 0,
- 'link_type' => $this->input->post('link_type'),
- 'url' => $this->input->post('url') ? $this->input->post('url') : '',
- 'uri' => $this->input->post('uri') ? $this->input->post('uri') : '',
- 'module_name' => $this->input->post('module_name') ? $this->input->post('module_name') : '',
- 'page_id' => (int) $this->input->post('page_id'),
- 'position' => $last_position ? $last_position->position + 1 : 1,
- 'target' => $this->input->post('target') ? $this->input->post('target') : '',
- 'class' => $this->input->post('class') ? $this->input->post('class') : '',
- 'navigation_group_id' => (int) $this->input->post('navigation_group_id'),
- 'restricted_to' => $this->input->post('restricted_to') ? implode(',', $this->input->post('restricted_to')) : 0
- ));
- if ($link) {
- //@TODO Fix Me Bro https://github.com/pyrocms/pyrocms/pull/2514
- $this->cache->forget('navigation_m');
- Events::trigger('post_navigation_create', $link);
- $this->session->set_flashdata('success', lang('nav:link_add_success'));
- // echo success to let the js refresh the page
- echo 'success';
- return;
- } else {
- $this->template->messages['error'] = lang('nav:link_add_error');
- echo $this->load->view('admin/partials/notices', $this->template);
- return;
- }
- }
- // check for errors
- if (validation_errors()) {
- echo $this->load->view('admin/partials/notices');
- return;
- }
- $link = (object) array();
- // Loop through each validation rule
- foreach ($this->validation_rules as $rule) {
- $link->{$rule['field']} = set_value($rule['field']);
- }
- $link->navigation_group_id = $group_id;
- $this->template
- ->set('link',$link);
- // Get Pages and create pages tree
- $this->template->tree_select = $this->_build_tree_select(array('current_parent' => $this->template->link->page_id));
- $this->template
- ->set_layout(false)
- ->build('admin/ajax/form');
- }
- /**
- * Edit a navigation item
- *
- * @param int $id The ID of the navigation item.
- */
- public function edit($id = 0)
- {
- // Got ID?
- if (empty($id)) {
- return;
- }
- // Get the navigation item based on the ID
- $link = Navigation\Model\Link::find($id);
- // Set the options for restricted to
- $group_options = Users\Model\Group::getGroupOptions();
- if (! $link) {
- $this->template->messages['error'] = lang('nav:link_not_exist_error');
- exit($this->load->view('admin/partials/notices', compact('link', 'group_options')));
- }
- // Valid data?
- if ($this->form_validation->run()) {
- if ($this->input->post('current_group_id') != $this->input->post('navigation_group_id')) {
- $link->parent = 0;
- Navigation\Model\Link::resetChildByParentId($id);
- }
- $link->title = $this->input->post('title');
- $link->link_type = $this->input->post('link_type');
- $link->url = $this->input->post('url') == 'http://' ? '' : $this->input->post('url');
- $link->uri = $this->input->post('uri');
- $link->module_name = $this->input->post('module_name');
- $link->page_id = (int) $this->input->post('page_id');
- $link->target = $this->input->post('target');
- $link->class = $this->input->post('class');
- $link->navigation_group_id = (int) $this->input->post('navigation_group_id');
- $link->restricted_to = $this->input->post('restricted_to') ? implode(',', $this->input->post('restricted_to')) : '';
- // Update the link and flush the cache
- if ($link->save()) {
- //@TODO Fix Me Bro https://github.com/pyrocms/pyrocms/pull/2514
- $this->cache->forget('navigation_m');
- Events::trigger('post_navigation_edit', $link);
- $this->session->set_flashdata('success', lang('nav:link_edit_success'));
- // echo success to let the js refresh the page
- exit('success');
- }
- }
- // check for errors
- if (validation_errors()) {
- exit($this->load->view('admin/partials/notices', $this->template));
- }
- // Loop through each rule
- foreach ($this->validation_rules as $rule) {
- if ($this->input->post($rule['field']) !== null) {
- $link->{$rule['field']} = $this->input->post($rule['field']);
- }
- }
- // Get Pages and create pages tree
- $this->template->tree_select = $this->_build_tree_select(array('current_parent' => $link->page_id));
- // Render the view
- $this->template
- ->set_layout(false)
- ->build('admin/ajax/form', compact('link', 'group_options'));
- }
- /**
- * Delete an existing navigation link
- *
- * @param int $id The ID of the navigation link
- */
- public function delete($id = 0)
- {
- $id_array = (!empty($id)) ? array($id) : $this->input->post('action_to');
- // Loop through each item to delete
- if (!empty($id_array)) {
- foreach ($id_array as $id) {
- Navigation\Model\Link::deleteLinkChildren($id);
- }
- Events::trigger('post_navigation_delete', $id_array);
- }
- // Flush the cache and redirect
- $this->links->flushCacheCollection();
- $this->session->set_flashdata('success', $this->lang->line('nav:link_delete_success'));
- redirect('admin/navigation');
- }
- /**
- * Tree select function
- *
- * Creates a tree to form select
- *
- * @param $params
- *
- * @return string
- */
- public function _build_tree_select($params)
- {
- $params = array_merge(array(
- 'tree' => array(),
- 'parent_id' => 0,
- 'current_parent'=> 0,
- 'current_id' => 0,
- 'level' => 0
- ), $params);
- extract($params);
- if (! $tree) {
- if ($pages = Pages\Model\Page::all()) {
- foreach ($pages as $page) {
- $tree[$page->parent_id][] = $page;
- }
- }
- }
- if ( ! isset($tree[$parent_id])) {
- return;
- }
- $html = '';
- foreach ($tree[$parent_id] as $item) {
- if ($current_id == $item->id) {
- continue;
- }
- $html .= '<option value="' . $item->id . '"';
- $html .= $current_parent == $item->id ? ' selected="selected">': '>';
- if ($level > 0) {
- for ($i = 0; $i < ($level*2); $i++) {
- $html .= ' ';
- }
- $html .= '- ';
- }
- $html .= $item->title . '</option>';
- $html .= $this->_build_tree_select(array(
- 'tree' => $tree,
- 'parent_id' => (int) $item->id,
- 'current_parent'=> $current_parent,
- 'current_id' => $current_id,
- 'level' => $level + 1
- ));
- }
- return $html;
- }
- /**
- * Validate the link value.
- *
- * Only the URI field may be submitted blank.
- *
- * @param string $link The link value
- * @return bool
- */
- public function _link_check($link)
- {
- $status = true;
- switch ($link) {
- case 'url':
- $status = ($this->input->post('url') > '' AND $this->input->post('url') !== 'http://');
- break;
- case 'module':
- $status = ($this->input->post('module_name') > '');
- break;
- case 'page':
- $status = ($this->input->post('page_id') > '');
- break;
- }
- if (! $status) {
- $this->form_validation->set_message('_link_check', sprintf(lang('nav:choose_value'), lang('nav:'.$link.'_label')));
- return false;
- }
- return true;
- }
- }