Source for file banner.php

Documentation is available at banner.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_banners
  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.  * Banner model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_banners
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * @var    string  The prefix to use with controller messages.
  22.      * @since  1.6
  23.      */
  24.     protected $text_prefix = 'COM_BANNERS_BANNER';
  25.  
  26.     /**
  27.      * The type alias for this content type.
  28.      *
  29.      * @var      string 
  30.      * @since    3.2
  31.      */
  32.     public $typeAlias = 'com_banners.banner';
  33.  
  34.     /**
  35.      * Method to perform batch operations on an item or a set of items.
  36.      *
  37.      * @param   array   $commands   An array of commands to perform.
  38.      * @param   array   $pks        An array of item ids.
  39.      * @param   array   $contexts   An array of item contexts.
  40.      *
  41.      * @return  boolean   Returns true on success, false on failure.
  42.      *
  43.      * @since    2.5
  44.      */
  45.     public function batch($commands$pks$contexts)
  46.     {
  47.         // Sanitize user ids.
  48.         $pks array_unique($pks);
  49.         JArrayHelper::toInteger($pks);
  50.  
  51.         // Remove any values of zero.
  52.         if (array_search(0$pkstrue))
  53.         {
  54.             unset($pks[array_search(0$pkstrue)]);
  55.         }
  56.  
  57.         if (empty($pks))
  58.         {
  59.             $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
  60.             return false;
  61.         }
  62.  
  63.         $done false;
  64.  
  65.         if (!empty($commands['category_id']))
  66.         {
  67.             $cmd JArrayHelper::getValue($commands'move_copy''c');
  68.  
  69.             if ($cmd == 'c')
  70.             {
  71.                 $result $this->batchCopy($commands['category_id']$pks$contexts);
  72.                 if (is_array($result))
  73.                 {
  74.                     $pks $result;
  75.                 }
  76.                 else
  77.                 {
  78.                     return false;
  79.                 }
  80.             }
  81.             elseif ($cmd == 'm' && !$this->batchMove($commands['category_id']$pks$contexts))
  82.             {
  83.                 return false;
  84.             }
  85.             $done true;
  86.         }
  87.  
  88.         if (strlen($commands['client_id']0)
  89.         {
  90.             if (!$this->batchClient($commands['client_id']$pks$contexts))
  91.             {
  92.                 return false;
  93.             }
  94.  
  95.             $done true;
  96.         }
  97.  
  98.         if (!empty($commands['language_id']))
  99.         {
  100.             if (!$this->batchLanguage($commands['language_id']$pks$contexts))
  101.             {
  102.                 return false;
  103.             }
  104.  
  105.             $done true;
  106.         }
  107.  
  108.         if (!$done)
  109.         {
  110.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
  111.             return false;
  112.         }
  113.  
  114.         // Clear the cache
  115.         $this->cleanCache();
  116.  
  117.         return true;
  118.     }
  119.  
  120.     /**
  121.      * Batch client changes for a group of banners.
  122.      *
  123.      * @param   string  $value     The new value matching a client.
  124.      * @param   array   $pks       An array of row IDs.
  125.      * @param   array   $contexts  An array of item contexts.
  126.      *
  127.      * @return  boolean  True if successful, false otherwise and internal error is set.
  128.      *
  129.      * @since   2.5
  130.      */
  131.     protected function batchClient($value$pks$contexts)
  132.     {
  133.         // Set the variables
  134.         $user JFactory::getUser();
  135.         $table $this->getTable();
  136.  
  137.         foreach ($pks as $pk)
  138.         {
  139.             if ($user->authorise('core.edit'$contexts[$pk]))
  140.             {
  141.                 $table->reset();
  142.                 $table->load($pk);
  143.                 $table->cid = (int) $value;
  144.  
  145.                 if (!$table->store())
  146.                 {
  147.                     $this->setError($table->getError());
  148.                     return false;
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
  154.                 return false;
  155.             }
  156.         }
  157.  
  158.         // Clean the cache
  159.         $this->cleanCache();
  160.  
  161.         return true;
  162.     }
  163.  
  164.     /**
  165.      * Batch copy items to a new category or current.
  166.      *
  167.      * @param   integer  $value     The new category.
  168.      * @param   array    $pks       An array of row IDs.
  169.      * @param   array    $contexts  An array of item contexts.
  170.      *
  171.      * @return  mixed  An array of new IDs on success, boolean false on failure.
  172.      *
  173.      * @since    2.5
  174.      */
  175.     protected function batchCopy($value$pks$contexts)
  176.     {
  177.         $categoryId = (int) $value;
  178.  
  179.         $table $this->getTable();
  180.         $i 0;
  181.  
  182.         // Check that the category exists
  183.         if ($categoryId)
  184.         {
  185.             $categoryTable JTable::getInstance('Category');
  186.             if (!$categoryTable->load($categoryId))
  187.             {
  188.                 if ($error $categoryTable->getError())
  189.                 {
  190.                     // Fatal error
  191.                     $this->setError($error);
  192.                     return false;
  193.                 }
  194.                 else
  195.                 {
  196.                     $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  197.                     return false;
  198.                 }
  199.             }
  200.         }
  201.  
  202.         if (empty($categoryId))
  203.         {
  204.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  205.             return false;
  206.         }
  207.  
  208.         // Check that the user has create permission for the component
  209.         $user JFactory::getUser();
  210.         if (!$user->authorise('core.create''com_banners.category.' $categoryId))
  211.         {
  212.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
  213.             return false;
  214.         }
  215.  
  216.         // Parent exists so we let's proceed
  217.         while (!empty($pks))
  218.         {
  219.             // Pop the first ID off the stack
  220.             $pk array_shift($pks);
  221.  
  222.             $table->reset();
  223.  
  224.             // Check that the row actually exists
  225.             if (!$table->load($pk))
  226.             {
  227.                 if ($error $table->getError())
  228.                 {
  229.                     // Fatal error
  230.                     $this->setError($error);
  231.                     return false;
  232.                 }
  233.                 else
  234.                 {
  235.                     // Not fatal error
  236.                     $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND'$pk));
  237.                     continue;
  238.                 }
  239.             }
  240.  
  241.             // Alter the title & alias
  242.             $data $this->generateNewTitle($categoryId$table->alias$table->name);
  243.             $table->name $data['0'];
  244.             $table->alias $data['1'];
  245.  
  246.             // Reset the ID because we are making a copy
  247.             $table->id 0;
  248.  
  249.             // New category ID
  250.             $table->catid $categoryId;
  251.  
  252.             // TODO: Deal with ordering?
  253.             //$table->ordering    = 1;
  254.  
  255.             // Check the row.
  256.             if (!$table->check())
  257.             {
  258.                 $this->setError($table->getError());
  259.                 return false;
  260.             }
  261.  
  262.             // Store the row.
  263.             if (!$table->store())
  264.             {
  265.                 $this->setError($table->getError());
  266.                 return false;
  267.             }
  268.  
  269.             // Get the new item ID
  270.             $newId $table->get('id');
  271.  
  272.             // Add the new ID to the array
  273.             $newIds[$i]    $newId;
  274.             $i++;
  275.         }
  276.  
  277.         // Clean the cache
  278.         $this->cleanCache();
  279.  
  280.         return $newIds;
  281.     }
  282.  
  283.     /**
  284.      * Method to test whether a record can be deleted.
  285.      *
  286.      * @param   object  $record  A record object.
  287.      *
  288.      * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  289.      *
  290.      * @since   1.6
  291.      */
  292.     protected function canDelete($record)
  293.     {
  294.         if (!empty($record->id))
  295.         {
  296.             if ($record->state != -2)
  297.             {
  298.                 return;
  299.             }
  300.             $user JFactory::getUser();
  301.  
  302.             if (!empty($record->catid))
  303.             {
  304.                 return $user->authorise('core.delete''com_banners.category.' . (int) $record->catid);
  305.             }
  306.             else
  307.             {
  308.                 return parent::canDelete($record);
  309.             }
  310.         }
  311.     }
  312.  
  313.     /**
  314.      * Method to test whether a record can have its state changed.
  315.      *
  316.      * @param   object  $record  A record object.
  317.      *
  318.      * @return  boolean  True if allowed to change the state of the record. Defaults to the permission set in the component.
  319.      *
  320.      * @since   1.6
  321.      */
  322.     protected function canEditState($record)
  323.     {
  324.         $user JFactory::getUser();
  325.  
  326.         // Check against the category.
  327.         if (!empty($record->catid))
  328.         {
  329.             return $user->authorise('core.edit.state''com_banners.category.' . (int) $record->catid);
  330.         }
  331.         // Default to component settings if category not known.
  332.         else
  333.         {
  334.             return parent::canEditState($record);
  335.         }
  336.     }
  337.  
  338.     /**
  339.      * Returns a JTable object, always creating it.
  340.      *
  341.      * @param   string  $type    The table type to instantiate. [optional]
  342.      * @param   string  $prefix  A prefix for the table class name. [optional]
  343.      * @param   array   $config  Configuration array for model. [optional]
  344.      *
  345.      * @return  JTable  A database object
  346.      *
  347.      * @since   1.6
  348.      */
  349.     public function getTable($type 'Banner'$prefix 'BannersTable'$config array())
  350.     {
  351.         return JTable::getInstance($type$prefix$config);
  352.     }
  353.  
  354.     /**
  355.      * Method to get the record form.
  356.      *
  357.      * @param   array    $data      Data for the form. [optional]
  358.      * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not. [optional]
  359.      *
  360.      * @return  mixed  A JForm object on success, false on failure
  361.      *
  362.      * @since   1.6
  363.      */
  364.     public function getForm($data array()$loadData true)
  365.     {
  366.         // Get the form.
  367.         $form $this->loadForm('com_banners.banner''banner'array('control' => 'jform''load_data' => $loadData));
  368.         if (empty($form))
  369.         {
  370.             return false;
  371.         }
  372.  
  373.         // Determine correct permissions to check.
  374.         if ($this->getState('banner.id'))
  375.         {
  376.             // Existing record. Can only edit in selected categories.
  377.             $form->setFieldAttribute('catid''action''core.edit');
  378.         }
  379.         else
  380.         {
  381.             // New record. Can only create in selected categories.
  382.             $form->setFieldAttribute('catid''action''core.create');
  383.         }
  384.  
  385.         // Modify the form based on access controls.
  386.         if (!$this->canEditState((object) $data))
  387.         {
  388.             // Disable fields for display.
  389.             $form->setFieldAttribute('ordering''disabled''true');
  390.             $form->setFieldAttribute('publish_up''disabled''true');
  391.             $form->setFieldAttribute('publish_down''disabled''true');
  392.             $form->setFieldAttribute('state''disabled''true');
  393.             $form->setFieldAttribute('sticky''disabled''true');
  394.  
  395.             // Disable fields while saving.
  396.             // The controller has already verified this is a record you can edit.
  397.             $form->setFieldAttribute('ordering''filter''unset');
  398.             $form->setFieldAttribute('publish_up''filter''unset');
  399.             $form->setFieldAttribute('publish_down''filter''unset');
  400.             $form->setFieldAttribute('state''filter''unset');
  401.             $form->setFieldAttribute('sticky''filter''unset');
  402.         }
  403.  
  404.         return $form;
  405.     }
  406.  
  407.     /**
  408.      * Method to get the data that should be injected in the form.
  409.      *
  410.      * @return  mixed  The data for the form.
  411.      *
  412.      * @since   1.6
  413.      */
  414.     protected function loadFormData()
  415.     {
  416.         // Check the session for previously entered form data.
  417.         $app  JFactory::getApplication();
  418.         $data $app->getUserState('com_banners.edit.banner.data'array());
  419.  
  420.         if (empty($data))
  421.         {
  422.             $data $this->getItem();
  423.  
  424.             // Prime some default values.
  425.             if ($this->getState('banner.id'== 0)
  426.             {
  427.                 $data->set('catid'$app->input->getInt('catid'$app->getUserState('com_banners.banners.filter.category_id')));
  428.             }
  429.         }
  430.  
  431.         $this->preprocessData('com_banners.banner'$data);
  432.  
  433.         return $data;
  434.     }
  435.  
  436.     /**
  437.      * Method to stick records.
  438.      *
  439.      * @param   array    &$pks   The ids of the items to publish.
  440.      * @param   integer  $value  The value of the published state
  441.      *
  442.      * @return  boolean  True on success.
  443.      *
  444.      * @since   1.6
  445.      */
  446.     public function stick(&$pks$value 1)
  447.     {
  448.         $user JFactory::getUser();
  449.         $table $this->getTable();
  450.         $pks = (array) $pks;
  451.  
  452.         // Access checks.
  453.         foreach ($pks as $i => $pk)
  454.         {
  455.             if ($table->load($pk))
  456.             {
  457.                 if (!$this->canEditState($table))
  458.                 {
  459.                     // Prune items that you can't change.
  460.                     unset($pks[$i]);
  461.                     JError::raiseWarning(403JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  462.                 }
  463.             }
  464.         }
  465.  
  466.         // Attempt to change the state of the records.
  467.         if (!$table->stick($pks$value$user->get('id')))
  468.         {
  469.             $this->setError($table->getError());
  470.             return false;
  471.         }
  472.  
  473.         return true;
  474.     }
  475.  
  476.     /**
  477.      * A protected method to get a set of ordering conditions.
  478.      *
  479.      * @param   JTable  $table  A record object.
  480.      *
  481.      * @return  array  An array of conditions to add to add to ordering queries.
  482.      *
  483.      * @since   1.6
  484.      */
  485.     protected function getReorderConditions($table)
  486.     {
  487.         $condition array();
  488.         $condition['catid = '. (int) $table->catid;
  489.         $condition['state >= 0';
  490.         return $condition;
  491.     }
  492.  
  493.     /**
  494.      * @since  3.0
  495.      */
  496.     protected function prepareTable($table)
  497.     {
  498.         $date JFactory::getDate();
  499.         $user JFactory::getUser();
  500.  
  501.         if (empty($table->id))
  502.         {
  503.             // Set the values
  504.             $table->created    $date->toSql();
  505.  
  506.             // Set ordering to the last item if not set
  507.             if (empty($table->ordering))
  508.             {
  509.                 $db JFactory::getDbo();
  510.                 $query $db->getQuery(true)
  511.                     ->select('MAX(ordering)')
  512.                     ->from('#__banners');
  513.  
  514.                 $db->setQuery($query);
  515.                 $max $db->loadResult();
  516.  
  517.                 $table->ordering $max 1;
  518.             }
  519.         }
  520.         else
  521.         {
  522.             // Set the values
  523.             $table->modified    $date->toSql();
  524.             $table->modified_by    $user->get('id');
  525.         }
  526.         // Increment the content version number.
  527.         $table->version++;
  528.     }
  529.  
  530.     /**
  531.      * Method to save the form data.
  532.      *
  533.      * @param   array  The form data.
  534.      *
  535.      * @return  boolean  True on success.
  536.      * @since   1.6
  537.      */
  538.  
  539.     public function save($data)
  540.     {
  541.         $app JFactory::getApplication();
  542.  
  543.         // Alter the name for save as copy
  544.         if ($app->input->get('task'== 'save2copy')
  545.         {
  546.             list($name$alias$this->generateNewTitle($data['catid']$data['alias']$data['name']);
  547.             $data['name']    $name;
  548.             $data['alias']    $alias;
  549.             $data['state']    0;
  550.         }
  551.  
  552.         if (parent::save($data))
  553.         {
  554.             return true;
  555.         }
  556.  
  557.         return false;
  558.     }
  559.  
  560. }

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