Source for file weblink.php

Documentation is available at weblink.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_weblinks
  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.  * @package     Joomla.Site
  14.  * @subpackage  com_weblinks
  15.  * @since       1.5
  16.  */
  17. {
  18.     /**
  19.      * @since   1.6
  20.      */
  21.     protected $view_item = 'form';
  22.  
  23.     /**
  24.      * @since   1.6
  25.      */
  26.     protected $view_list = 'categories';
  27.  
  28.     /**
  29.      * The URL edit variable.
  30.      *
  31.      * @var    string 
  32.      * @since  3.2
  33.      */
  34.     protected $urlVar = 'a.id';
  35.  
  36.     /**
  37.      * Method to add a new record.
  38.      *
  39.      * @return  boolean  True if the article can be added, false if not.
  40.      * @since   1.6
  41.      */
  42.     public function add()
  43.     {
  44.  
  45.         if (!parent::add())
  46.         {
  47.             // Redirect to the return page.
  48.             $this->setRedirect($this->getReturnPage());
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * Method override to check if you can add a new record.
  54.      *
  55.      * @param   array  $data    An array of input data.
  56.      * @return  boolean 
  57.      * @since   1.6
  58.      */
  59.     protected function allowAdd($data array())
  60.     {
  61.         $user        JFactory::getUser();
  62.         $categoryId    JArrayHelper::getValue($data'catid'$this->input->getInt('id')'int');
  63.         $allow        null;
  64.  
  65.         if ($categoryId)
  66.         {
  67.             // If the category has been passed in the URL check it.
  68.             $allow    $user->authorise('core.create'$this->option.'.category.'.$categoryId);
  69.         }
  70.  
  71.         if ($allow === null)
  72.         {
  73.             // In the absense of better information, revert to the component permissions.
  74.             return parent::allowAdd($data);
  75.         }
  76.         else
  77.         {
  78.             return $allow;
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Method to check if you can add a new record.
  84.      *
  85.      * @param   array  $data    An array of input data.
  86.      * @param   string    $key    The name of the key for the primary key.
  87.      *
  88.      * @return  boolean 
  89.      * @since   1.6
  90.      */
  91.     protected function allowEdit($data array()$key 'id')
  92.     {
  93.         $recordId    = (int) isset($data[$key]$data[$key0;
  94.         $categoryId 0;
  95.  
  96.         if ($recordId)
  97.         {
  98.             $categoryId = (int) $this->getModel()->getItem($recordId)->catid;
  99.         }
  100.  
  101.         if ($categoryId)
  102.         {
  103.             // The category has been set. Check the category permissions.
  104.             return JFactory::getUser()->authorise('core.edit'$this->option.'.category.'.$categoryId);
  105.         }
  106.         else
  107.         {
  108.             // Since there is no asset tracking, revert to the component permissions.
  109.             return parent::allowEdit($data$key);
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Method to cancel an edit.
  115.      *
  116.      * @param   string    $key    The name of the primary key of the URL variable.
  117.      *
  118.      * @return  Boolean    True if access level checks pass, false otherwise.
  119.      * @since   1.6
  120.      */
  121.     public function cancel($key 'w_id')
  122.     {
  123.         parent::cancel($key);
  124.  
  125.         // Redirect to the return page.
  126.         $this->setRedirect($this->getReturnPage());
  127.     }
  128.  
  129.     /**
  130.      * Method to edit an existing record.
  131.      *
  132.      * @param   string    $key    The name of the primary key of the URL variable.
  133.      * @param   string    $urlVar    The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  134.      *
  135.      * @return  Boolean    True if access level check and checkout passes, false otherwise.
  136.      * @since   1.6
  137.      */
  138.     public function edit($key null$urlVar 'w_id')
  139.     {
  140.         $result parent::edit($key$urlVar);
  141.  
  142.         return $result;
  143.     }
  144.  
  145.     /**
  146.      * Method to get a model object, loading it if required.
  147.      *
  148.      * @param   string    $name    The model name. Optional.
  149.      * @param   string    $prefix    The class prefix. Optional.
  150.      * @param   array  $config    Configuration array for model. Optional.
  151.      *
  152.      * @return  object  The model.
  153.      * @since   1.5
  154.      */
  155.     public function getModel($name 'form'$prefix ''$config array('ignore_request' => true))
  156.     {
  157.         $model parent::getModel($name$prefix$config);
  158.  
  159.         return $model;
  160.     }
  161.  
  162.     /**
  163.      * Gets the URL arguments to append to an item redirect.
  164.      *
  165.      * @param   integer  $recordId    The primary key id for the item.
  166.      * @param   string    $urlVar        The name of the URL variable for the id.
  167.      *
  168.      * @return  string    The arguments to append to the redirect URL.
  169.      * @since   1.6
  170.      */
  171.     protected function getRedirectToItemAppend($recordId null$urlVar null)
  172.     {
  173.         $append parent::getRedirectToItemAppend($recordId$urlVar);
  174.         $itemId    $this->input->getInt('Itemid');
  175.         $return    $this->getReturnPage();
  176.  
  177.         if ($itemId)
  178.         {
  179.             $append .= '&Itemid='.$itemId;
  180.         }
  181.  
  182.         if ($return)
  183.         {
  184.             $append .= '&return='.base64_encode($return);
  185.         }
  186.  
  187.         return $append;
  188.     }
  189.  
  190.     /**
  191.      * Get the return URL.
  192.      *
  193.      * If a "return" variable has been passed in the request
  194.      *
  195.      * @return  string    The return URL.
  196.      * @since   1.6
  197.      */
  198.     protected function getReturnPage()
  199.     {
  200.         $return $this->input->get('return'null'base64');
  201.  
  202.         if (empty($return|| !JUri::isInternal(base64_decode($return)))
  203.         {
  204.             return JUri::base();
  205.         }
  206.         else
  207.         {
  208.             return base64_decode($return);
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Function that allows child controller access to model data after the data has been saved.
  214.      *
  215.      * @param   JModelLegacy  $model      The data model object.
  216.      * @param   array         $validData  The validated data.
  217.      *
  218.      * @return  void 
  219.      * @since   1.6
  220.      */
  221.     protected function postSaveHook(JModelLegacy $model$validData array())
  222.     {
  223.         return;
  224.     }
  225.  
  226.     /**
  227.      * Method to save a record.
  228.      *
  229.      * @param   string    $key    The name of the primary key of the URL variable.
  230.      * @param   string    $urlVar    The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  231.      *
  232.      * @return  Boolean    True if successful, false otherwise.
  233.      * @since   1.6
  234.      */
  235.     public function save($key null$urlVar 'w_id')
  236.     {
  237.         $result parent::save($key$urlVar);
  238.  
  239.         // If ok, redirect to the return page.
  240.         if ($result)
  241.         {
  242.             $this->setRedirect($this->getReturnPage());
  243.         }
  244.  
  245.         return $result;
  246.     }
  247.  
  248.     /**
  249.      * Go to a weblink
  250.      *
  251.      * @return  void 
  252.      * @since   1.6
  253.      */
  254.     public function go()
  255.     {
  256.         // Get the ID from the request
  257.         $id $this->input->getInt('id');
  258.  
  259.         // Get the model, requiring published items
  260.         $modelLink    $this->getModel('Weblink'''array('ignore_request' => true));
  261.         $modelLink->setState('filter.published'1);
  262.  
  263.         // Get the item
  264.         $link    $modelLink->getItem($id);
  265.  
  266.         // Make sure the item was found.
  267.         if (empty($link))
  268.         {
  269.             return JError::raiseWarning(404JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
  270.         }
  271.  
  272.         // Check whether item access level allows access.
  273.         $user    JFactory::getUser();
  274.         $groups    $user->getAuthorisedViewLevels();
  275.  
  276.         if (!in_array($link->access$groups))
  277.         {
  278.             return JError::raiseError(403JText::_('JERROR_ALERTNOAUTHOR'));
  279.         }
  280.  
  281.         // Check whether category access level allows access.
  282.         $modelCat $this->getModel('Category''WeblinksModel'array('ignore_request' => true));
  283.         $modelCat->setState('filter.published'1);
  284.  
  285.         // Get the category
  286.         $category $modelCat->getCategory($link->catid);
  287.  
  288.         // Make sure the category was found.
  289.         if (empty($category))
  290.         {
  291.             return JError::raiseWarning(404JText::_('COM_WEBLINKS_ERROR_WEBLINK_NOT_FOUND'));
  292.         }
  293.  
  294.         // Check whether item access level allows access.
  295.         if (!in_array($category->access$groups))
  296.         {
  297.             return JError::raiseError(403JText::_('JERROR_ALERTNOAUTHOR'));
  298.         }
  299.  
  300.         // Redirect to the URL
  301.         // TODO: Probably should check for a valid http link
  302.         if ($link->url)
  303.         {
  304.             $modelLink->hit($id);
  305.             JFactory::getApplication()->redirect($link->url);
  306.         }
  307.         else
  308.         {
  309.             return JError::raiseWarning(404JText::_('COM_WEBLINKS_ERROR_WEBLINK_URL_INVALID'));
  310.         }
  311.     }
  312. }

Documentation generated on Tue, 19 Nov 2013 15:18:25 +0100 by phpDocumentor 1.4.3