Source for file article.php

Documentation is available at article.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_content
  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_content
  15.  */
  16. {
  17.     /**
  18.      * The URL view item variable.
  19.      *
  20.      * @var    string 
  21.      * @since  1.6
  22.      */
  23.     protected $view_item = 'form';
  24.  
  25.     /**
  26.      * The URL view list variable.
  27.      *
  28.      * @var    string 
  29.      * @since  1.6
  30.      */
  31.     protected $view_list = 'categories';
  32.  
  33.     /**
  34.      * The URL edit variable.
  35.      *
  36.      * @var    string 
  37.      * @since  3.2
  38.      */
  39.     protected $urlVar = 'a.id';
  40.  
  41.     /**
  42.      * Method to add a new record.
  43.      *
  44.      * @return  mixed  True if the record can be added, a error object if not.
  45.      *
  46.      * @since   1.6
  47.      */
  48.     public function add()
  49.     {
  50.         if (!parent::add())
  51.         {
  52.             // Redirect to the return page.
  53.             $this->setRedirect($this->getReturnPage());
  54.         }
  55.     }
  56.  
  57.     /**
  58.      * Method override to check if you can add a new record.
  59.      *
  60.      * @param   array  $data  An array of input data.
  61.      *
  62.      * @return  boolean 
  63.      *
  64.      * @since   1.6
  65.      */
  66.     protected function allowAdd($data array())
  67.     {
  68.         $user       JFactory::getUser();
  69.         $categoryId JArrayHelper::getValue($data'catid'$this->input->getInt('catid')'int');
  70.         $allow      null;
  71.  
  72.         if ($categoryId)
  73.         {
  74.             // If the category has been passed in the data or URL check it.
  75.             $allow    $user->authorise('core.create''com_content.category.'.$categoryId);
  76.         }
  77.  
  78.         if ($allow === null)
  79.         {
  80.             // In the absense of better information, revert to the component permissions.
  81.             return parent::allowAdd();
  82.         }
  83.         else
  84.         {
  85.             return $allow;
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Method override to check if you can edit an existing record.
  91.      *
  92.      * @param   array   $data  An array of input data.
  93.      * @param   string  $key   The name of the key for the primary key; default is id.
  94.      *
  95.      * @return  boolean 
  96.      *
  97.      * @since   1.6
  98.      */
  99.     protected function allowEdit($data array()$key 'id')
  100.     {
  101.         $recordId = (int) isset($data[$key]$data[$key0;
  102.         $user     JFactory::getUser();
  103.         $userId   $user->get('id');
  104.         $asset    'com_content.article.' $recordId;
  105.  
  106.         // Check general edit permission first.
  107.         if ($user->authorise('core.edit'$asset))
  108.         {
  109.             return true;
  110.         }
  111.  
  112.         // Fallback on edit.own.
  113.         // First test if the permission is available.
  114.         if ($user->authorise('core.edit.own'$asset))
  115.         {
  116.             // Now test the owner is the user.
  117.             $ownerId = (int) isset($data['created_by']$data['created_by'0;
  118.             if (empty($ownerId&& $recordId)
  119.             {
  120.                 // Need to do a lookup from the model.
  121.                 $record $this->getModel()->getItem($recordId);
  122.  
  123.                 if (empty($record))
  124.                 {
  125.                     return false;
  126.                 }
  127.  
  128.                 $ownerId $record->created_by;
  129.             }
  130.  
  131.             // If the owner matches 'me' then do the test.
  132.             if ($ownerId == $userId)
  133.             {
  134.                 return true;
  135.             }
  136.         }
  137.  
  138.         // Since there is no asset tracking, revert to the component permissions.
  139.         return parent::allowEdit($data$key);
  140.     }
  141.  
  142.     /**
  143.      * Method to cancel an edit.
  144.      *
  145.      * @param   string  $key  The name of the primary key of the URL variable.
  146.      *
  147.      * @return  boolean  True if access level checks pass, false otherwise.
  148.      *
  149.      * @since   1.6
  150.      */
  151.     public function cancel($key 'a_id')
  152.     {
  153.         parent::cancel($key);
  154.  
  155.         // Redirect to the return page.
  156.         $this->setRedirect($this->getReturnPage());
  157.     }
  158.  
  159.     /**
  160.      * Method to edit an existing record.
  161.      *
  162.      * @param   string  $key     The name of the primary key of the URL variable.
  163.      * @param   string  $urlVar  The name of the URL variable if different from the primary key
  164.      *  (sometimes required to avoid router collisions).
  165.      *
  166.      * @return  boolean  True if access level check and checkout passes, false otherwise.
  167.      *
  168.      * @since   1.6
  169.      */
  170.     public function edit($key null$urlVar 'a_id')
  171.     {
  172.         $result parent::edit($key$urlVar);
  173.  
  174.         return $result;
  175.     }
  176.  
  177.     /**
  178.      * Method to get a model object, loading it if required.
  179.      *
  180.      * @param   string  $name    The model name. Optional.
  181.      * @param   string  $prefix  The class prefix. Optional.
  182.      * @param   array   $config  Configuration array for model. Optional.
  183.      *
  184.      * @return  object  The model.
  185.      *
  186.      * @since   1.5
  187.      */
  188.     public function getModel($name 'form'$prefix ''$config array('ignore_request' => true))
  189.     {
  190.         $model parent::getModel($name$prefix$config);
  191.  
  192.         return $model;
  193.     }
  194.  
  195.     /**
  196.      * Gets the URL arguments to append to an item redirect.
  197.      *
  198.      * @param   integer  $recordId  The primary key id for the item.
  199.      * @param   string   $urlVar    The name of the URL variable for the id.
  200.      *
  201.      * @return  string    The arguments to append to the redirect URL.
  202.      *
  203.      * @since   1.6
  204.      */
  205.     protected function getRedirectToItemAppend($recordId null$urlVar 'a_id')
  206.     {
  207.         // Need to override the parent method completely.
  208.         $tmpl   $this->input->get('tmpl');
  209. //        $layout = $this->input->get('layout', 'edit');
  210.         $append '';
  211.  
  212.         // Setup redirect info.
  213.         if ($tmpl)
  214.         {
  215.             $append .= '&tmpl='.$tmpl;
  216.         }
  217.  
  218.         // TODO This is a bandaid, not a long term solution.
  219. //        if ($layout)
  220. //        {
  221. //            $append .= '&layout=' . $layout;
  222. //        }
  223.         $append .= '&layout=edit';
  224.  
  225.         if ($recordId)
  226.         {
  227.             $append .= '&'.$urlVar.'='.$recordId;
  228.         }
  229.  
  230.         $itemId    $this->input->getInt('Itemid');
  231.         $return    $this->getReturnPage();
  232.         $catId  $this->input->getInt('catid'null'get');
  233.  
  234.         if ($itemId)
  235.         {
  236.             $append .= '&Itemid='.$itemId;
  237.         }
  238.  
  239.         if ($catId)
  240.         {
  241.             $append .= '&catid='.$catId;
  242.         }
  243.  
  244.         if ($return)
  245.         {
  246.             $append .= '&return='.base64_encode($return);
  247.         }
  248.  
  249.         return $append;
  250.     }
  251.  
  252.     /**
  253.      * Get the return URL.
  254.      *
  255.      * If a "return" variable has been passed in the request
  256.      *
  257.      * @return  string    The return URL.
  258.      *
  259.      * @since   1.6
  260.      */
  261.     protected function getReturnPage()
  262.     {
  263.         $return $this->input->get('return'null'base64');
  264.  
  265.         if (empty($return|| !JUri::isInternal(base64_decode($return)))
  266.         {
  267.             return JUri::base();
  268.         }
  269.         else
  270.         {
  271.             return base64_decode($return);
  272.         }
  273.     }
  274.  
  275.     /**
  276.      * Function that allows child controller access to model data after the data has been saved.
  277.      *
  278.      * @param   JModelLegacy  $model  The data model object.
  279.      * @param   array         $validData   The validated data.
  280.      *
  281.      * @return  void 
  282.      *
  283.      * @since   1.6
  284.      */
  285.     protected function postSaveHook(JModelLegacy $model$validData array())
  286.     {
  287.         return;
  288.     }
  289.  
  290.     /**
  291.      * Method to save a record.
  292.      *
  293.      * @param   string  $key     The name of the primary key of the URL variable.
  294.      * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  295.      *
  296.      * @return  boolean  True if successful, false otherwise.
  297.      *
  298.      * @since   1.6
  299.      */
  300.     public function save($key null$urlVar 'a_id')
  301.     {
  302.         $result parent::save($key$urlVar);
  303.  
  304.         // If ok, redirect to the return page.
  305.         if ($result)
  306.         {
  307.             $this->setRedirect($this->getReturnPage());
  308.         }
  309.  
  310.         return $result;
  311.     }
  312.  
  313.     /**
  314.      * Method to save a vote.
  315.      *
  316.      * @return  void 
  317.      *
  318.      * @since   1.6
  319.      */
  320.     public function vote()
  321.     {
  322.         // Check for request forgeries.
  323.         JSession::checkToken(or jexit(JText::_('JINVALID_TOKEN'));
  324.  
  325.         $user_rating $this->input->getInt('user_rating'-1);
  326.  
  327.         if ($user_rating > -1)
  328.         {
  329.             $url $this->input->getString('url''');
  330.             $id $this->input->getInt('id'0);
  331.             $viewName $this->input->getString('view'$this->default_view);
  332.             $model $this->getModel($viewName);
  333.  
  334.             if ($model->storeVote($id$user_rating))
  335.             {
  336.                 $this->setRedirect($urlJText::_('COM_CONTENT_ARTICLE_VOTE_SUCCESS'));
  337.             }
  338.             else
  339.             {
  340.                 $this->setRedirect($urlJText::_('COM_CONTENT_ARTICLE_VOTE_FAILURE'));
  341.             }
  342.         }
  343.     }
  344. }

Documentation generated on Tue, 19 Nov 2013 14:53:54 +0100 by phpDocumentor 1.4.3