Source for file item.php

Documentation is available at item.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_menus
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * The Menu Item Controller
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_menus
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * Method to add a new menu item.
  22.      *
  23.      * @return  mixed  True if the record can be added, a JError object if not.
  24.      *
  25.      * @since   1.6
  26.      */
  27.     public function add()
  28.     {
  29.         $app JFactory::getApplication();
  30.         $context 'com_menus.edit.item';
  31.  
  32.         $result parent::add();
  33.         if ($result)
  34.         {
  35.             $app->setUserState($context '.type'null);
  36.             $app->setUserState($context '.link'null);
  37.  
  38.             $menuType $app->getUserStateFromRequest($this->context . '.filter.menutype''menutype''mainmenu''cmd');
  39.  
  40.             $this->setRedirect(JRoute::_('index.php?option=com_menus&view=item&menutype=' $menuType $this->getRedirectToItemAppend()false));
  41.         }
  42.  
  43.         return $result;
  44.     }
  45.  
  46.     /**
  47.      * Method to run batch operations.
  48.      *
  49.      * @param   object  $model  The model.
  50.      *
  51.      * @return  boolean     True if successful, false otherwise and internal error is set.
  52.      *
  53.      * @since   1.6
  54.      */
  55.     public function batch($model null)
  56.     {
  57.         JSession::checkToken(or jexit(JText::_('JINVALID_TOKEN'));
  58.  
  59.         $model $this->getModel('Item'''array());
  60.  
  61.         // Preset the redirect
  62.         $this->setRedirect(JRoute::_('index.php?option=com_menus&view=items' $this->getRedirectToListAppend()false));
  63.  
  64.         return parent::batch($model);
  65.     }
  66.  
  67.     /**
  68.      * Method to cancel an edit.
  69.      *
  70.      * @param   string  $key  The name of the primary key of the URL variable.
  71.      *
  72.      * @return  boolean  True if access level checks pass, false otherwise.
  73.      *
  74.      * @since   1.6
  75.      */
  76.     public function cancel($key null)
  77.     {
  78.         JSession::checkToken(or jexit(JText::_('JINVALID_TOKEN'));
  79.  
  80.         $app JFactory::getApplication();
  81.         $context 'com_menus.edit.item';
  82.         $result parent::cancel();
  83.  
  84.         if ($result)
  85.         {
  86.             // Clear the ancillary data from the session.
  87.             $app->setUserState($context '.type'null);
  88.             $app->setUserState($context '.link'null);
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Method to edit an existing record.
  94.      *
  95.      * @param   string  $key     The name of the primary key of the URL variable.
  96.      * @param   string  $urlVar  The name of the URL variable if different from the primary key
  97.      *  (sometimes required to avoid router collisions).
  98.      *
  99.      * @return  boolean  True if access level check and checkout passes, false otherwise.
  100.      *
  101.      * @since   1.6
  102.      */
  103.     public function edit($key null$urlVar null)
  104.     {
  105.         $app JFactory::getApplication();
  106.         $result parent::edit();
  107.  
  108.         if ($result)
  109.         {
  110.             // Push the new ancillary data into the session.
  111.             $app->setUserState('com_menus.edit.item.type'null);
  112.             $app->setUserState('com_menus.edit.item.link'null);
  113.         }
  114.  
  115.         return true;
  116.     }
  117.  
  118.     /**
  119.      * Method to save a record.
  120.      *
  121.      * @param   string  $key     The name of the primary key of the URL variable.
  122.      * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  123.      *
  124.      * @return  boolean  True if successful, false otherwise.
  125.      *
  126.      * @since   1.6
  127.      */
  128.     public function save($key null$urlVar null)
  129.     {
  130.         // Check for request forgeries.
  131.         JSession::checkToken(or jexit(JText::_('JINVALID_TOKEN'));
  132.  
  133.         $app      JFactory::getApplication();
  134.         $model    $this->getModel('Item'''array());
  135.         $data     $this->input->post->get('jform'array()'array');
  136.         $task     $this->getTask();
  137.         $context  'com_menus.edit.item';
  138.         $recordId $this->input->getInt('id');
  139.  
  140.         // Populate the row id from the session.
  141.         $data['id'$recordId;
  142.  
  143.         // The save2copy task needs to be handled slightly differently.
  144.         if ($task == 'save2copy')
  145.         {
  146.             // Check-in the original row.
  147.             if ($model->checkin($data['id']=== false)
  148.             {
  149.                 // Check-in failed, go back to the item and display a notice.
  150.                 $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED'$model->getError())'warning');
  151.                 return false;
  152.             }
  153.  
  154.             // Reset the ID and then treat the request as for Apply.
  155.             $data['id'0;
  156.             $data['associations'array();
  157.             $task 'apply';
  158.         }
  159.  
  160.         // Validate the posted data.
  161.         // This post is made up of two forms, one for the item and one for params.
  162.         $form $model->getForm($data);
  163.  
  164.         if (!$form)
  165.         {
  166.             JError::raiseError(500$model->getError());
  167.  
  168.             return false;
  169.         }
  170.  
  171.         if ($data['type'== 'url')
  172.         {
  173.              $data['link'str_replace(array('"''>''<')''$data['link']);
  174.  
  175.             if (strstr($data['link']':'))
  176.             {
  177.                 $segments explode(':'$data['link']);
  178.                 $protocol strtolower($segments[0]);
  179.                 $scheme array('http''https''ftp''ftps''gopher''mailto''news''prospero''telnet''rlogin''tn3270''wais''url',
  180.                     'mid''cid''nntp''tel''urn''ldap''file''fax''modem''git');
  181.  
  182.                 if (!in_array($protocol$scheme))
  183.                 {
  184.                     $app->enqueueMessage(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED')'warning');
  185.                     $this->setRedirect(
  186.                          JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false)
  187.                     );
  188.  
  189.                     return false;
  190.                 }
  191.             }
  192.         }
  193.  
  194.         $data $model->validate($form$data);
  195.  
  196.         // Check for the special 'request' entry.
  197.         if ($data['type'== 'component' && isset($data['request']&& is_array($data['request']&& !empty($data['request']))
  198.         {
  199.             $removeArgs array();
  200.  
  201.             // Preprocess request fields to ensure that we remove not set or empty request params
  202.             $request $form->getGroup('request');
  203.  
  204.             if (!empty($request))
  205.             {
  206.                 foreach ($request as $field)
  207.                 {
  208.                     $fieldName $field->getAttribute('name');
  209.  
  210.                     if (!isset($data['request'][$fieldName]|| $data['request'][$fieldName== '')
  211.                     {
  212.                         $removeArgs[$fieldName'';
  213.                     }
  214.                 }
  215.             }
  216.  
  217.             // Parse the submitted link arguments.
  218.             $args array();
  219.             parse_str(parse_url($data['link']PHP_URL_QUERY)$args);
  220.  
  221.             // Merge in the user supplied request arguments.
  222.             $args array_merge($args$data['request']);
  223.  
  224.             // Remove the unused request params
  225.             if (!empty($args&& !empty($removeArgs))
  226.             {
  227.                 $args array_diff_key($args$removeArgs);
  228.             }
  229.  
  230.             $data['link''index.php?' urldecode(http_build_query($args'''&'));
  231.             unset($data['request']);
  232.         }
  233.  
  234.         // Check for validation errors.
  235.         if ($data === false)
  236.         {
  237.             // Get the validation messages.
  238.             $errors $model->getErrors();
  239.  
  240.             // Push up to three validation messages out to the user.
  241.             for ($i 0$n count($errors)$i $n && $i 3$i++)
  242.             {
  243.                 if ($errors[$iinstanceof Exception)
  244.                 {
  245.                     $app->enqueueMessage($errors[$i]->getMessage()'warning');
  246.                 }
  247.                 else
  248.                 {
  249.                     $app->enqueueMessage($errors[$i]'warning');
  250.                 }
  251.             }
  252.  
  253.             // Save the data in the session.
  254.             $app->setUserState('com_menus.edit.item.data'$data);
  255.  
  256.             // Redirect back to the edit screen.
  257.             $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false));
  258.  
  259.             return false;
  260.         }
  261.  
  262.         // Attempt to save the data.
  263.         if (!$model->save($data))
  264.         {
  265.             // Save the data in the session.
  266.             $app->setUserState('com_menus.edit.item.data'$data);
  267.  
  268.             // Redirect back to the edit screen.
  269.             $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED'$model->getError())'warning');
  270.             $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false));
  271.  
  272.             return false;
  273.         }
  274.  
  275.         // Save succeeded, check-in the row.
  276.         if ($model->checkin($data['id']=== false)
  277.         {
  278.             // Check-in failed, go back to the row and display a notice.
  279.             $this->setMessage(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED'$model->getError())'warning');
  280.             $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false));
  281.  
  282.             return false;
  283.         }
  284.  
  285.         $this->setMessage(JText::_('COM_MENUS_SAVE_SUCCESS'));
  286.  
  287.         // Redirect the user and adjust session state based on the chosen task.
  288.         switch ($task)
  289.         {
  290.             case 'apply':
  291.                 // Set the row data in the session.
  292.                 $recordId $model->getState($this->context . '.id');
  293.                 $this->holdEditId($context$recordId);
  294.                 $app->setUserState('com_menus.edit.item.data'null);
  295.                 $app->setUserState('com_menus.edit.item.type'null);
  296.                 $app->setUserState('com_menus.edit.item.link'null);
  297.  
  298.                 // Redirect back to the edit screen.
  299.                 $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false));
  300.                 break;
  301.  
  302.             case 'save2new':
  303.                 // Clear the row id and data in the session.
  304.                 $this->releaseEditId($context$recordId);
  305.                 $app->setUserState('com_menus.edit.item.data'null);
  306.                 $app->setUserState('com_menus.edit.item.type'null);
  307.                 $app->setUserState('com_menus.edit.item.link'null);
  308.                 $app->setUserState('com_menus.edit.item.menutype'$model->getState('item.menutype'));
  309.  
  310.                 // Redirect back to the edit screen.
  311.                 $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend()false));
  312.                 break;
  313.  
  314.             default:
  315.                 // Clear the row id and data in the session.
  316.                 $this->releaseEditId($context$recordId);
  317.                 $app->setUserState('com_menus.edit.item.data'null);
  318.                 $app->setUserState('com_menus.edit.item.type'null);
  319.                 $app->setUserState('com_menus.edit.item.link'null);
  320.  
  321.                 // Redirect to the list screen.
  322.                 $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_list . $this->getRedirectToListAppend()false));
  323.                 break;
  324.         }
  325.     }
  326.  
  327.     /**
  328.      * Sets the type of the menu item currently being edited.
  329.      *
  330.      * @return  void 
  331.      *
  332.      * @since   1.6
  333.      */
  334.     public function setType()
  335.     {
  336.         $app JFactory::getApplication();
  337.  
  338.         // Get the posted values from the request.
  339.         $data $this->input->post->get('jform'array()'array');
  340.  
  341.         // Get the type.
  342.         $type $data['type'];
  343.  
  344.         $type json_decode(base64_decode($type));
  345.         $title = isset($type->title$type->title null;
  346.         $recordId = isset($type->id$type->id 0;
  347.  
  348.         $specialTypes array('alias''separator''url''heading');
  349.         if (!in_array($title$specialTypes))
  350.         {
  351.             $title 'component';
  352.         }
  353.  
  354.         $app->setUserState('com_menus.edit.item.type'$title);
  355.         if ($title == 'component')
  356.         {
  357.             if (isset($type->request))
  358.             {
  359.                 $component JComponentHelper::getComponent($type->request->option);
  360.                 $data['component_id'$component->id;
  361.  
  362.                 $app->setUserState('com_menus.edit.item.link''index.php?' JUri::buildQuery((array) $type->request));
  363.             }
  364.         }
  365.         // If the type is alias you just need the item id from the menu item referenced.
  366.         elseif ($title == 'alias')
  367.         {
  368.             $app->setUserState('com_menus.edit.item.link''index.php?Itemid=');
  369.         }
  370.  
  371.         unset($data['request']);
  372.         $data['type'$title;
  373.         if ($this->input->get('fieldtype'== 'type')
  374.         {
  375.             $data['link'$app->getUserState('com_menus.edit.item.link');
  376.         }
  377.  
  378.         //Save the data in the session.
  379.         $app->setUserState('com_menus.edit.item.data'$data);
  380.  
  381.         $this->type $type;
  382.         $this->setRedirect(JRoute::_('index.php?option=' $this->option . '&view=' $this->view_item . $this->getRedirectToItemAppend($recordId)false));
  383.     }
  384.  
  385.     /**
  386.      * Gets the parent items of the menu location currently.
  387.      *
  388.      * @return  void 
  389.      *
  390.      * @since   3.2
  391.      */
  392.     function getParentItem()
  393.     {
  394.         $app JFactory::getApplication();
  395.  
  396.         $menutype $this->input->get->get('menutype');
  397.  
  398.         $model $this->getModel('Items'''array());
  399.         $model->setState('filter.menutype'$menutype);
  400.         $model->setState('list.select''a.id, a.title, a.level');
  401.  
  402.         $results $model->getItems();
  403.  
  404.         // Pad the option text with spaces using depth level as a multiplier.
  405.         for ($i 0$n count($results)$i $n$i++)
  406.         {
  407.             $results[$i]->title str_repeat('- '$results[$i]->level$results[$i]->title;
  408.         }
  409.  
  410.         // Output a JSON object
  411.         echo json_encode($results);
  412.  
  413.         $app->close();
  414.     }
  415. }

Documentation generated on Tue, 19 Nov 2013 15:05:59 +0100 by phpDocumentor 1.4.3