Source for file weblink.php

Documentation is available at weblink.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  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.  * Weblinks model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_weblinks
  17.  * @since       1.5
  18.  */
  19. {
  20.  
  21.     /**
  22.      * The type alias for this content type.
  23.      *
  24.      * @var      string 
  25.      * @since    3.2
  26.      */
  27.     public $typeAlias = 'com_weblinks.weblink';
  28.  
  29.     /**
  30.      * The prefix to use with controller messages.
  31.      *
  32.      * @var    string 
  33.      * @since  1.6
  34.      */
  35.     protected $text_prefix = 'COM_WEBLINKS';
  36.  
  37.     /**
  38.      * Method to test whether a record can be deleted.
  39.      *
  40.      * @param   object  $record  A record object.
  41.      *
  42.      * @return  boolean  True if allowed to delete the record. Defaults to the permission for the component.
  43.      *
  44.      * @since   1.6
  45.      */
  46.     protected function canDelete($record)
  47.     {
  48.         if (!empty($record->id))
  49.         {
  50.             if ($record->state != -2)
  51.             {
  52.                 return;
  53.             }
  54.             $user JFactory::getUser();
  55.  
  56.             if ($record->catid)
  57.             {
  58.                 return $user->authorise('core.delete''com_weblinks.category.'.(int) $record->catid);
  59.             }
  60.             else
  61.             {
  62.                 return parent::canDelete($record);
  63.             }
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Method to test whether a record can be deleted.
  69.      *
  70.      * @param   object  $record  A record object.
  71.      *
  72.      * @return  boolean  True if allowed to change the state of the record. Defaults to the permission for the component.
  73.      *
  74.      * @since   1.6
  75.      */
  76.     protected function canEditState($record)
  77.     {
  78.         $user JFactory::getUser();
  79.  
  80.         if (!empty($record->catid))
  81.         {
  82.             return $user->authorise('core.edit.state''com_weblinks.category.'.(int) $record->catid);
  83.         }
  84.         else
  85.         {
  86.             return parent::canEditState($record);
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Method to get a table object, load it if necessary.
  92.      *
  93.      * @param   string  $type    The table name. Optional.
  94.      * @param   string  $prefix  The class prefix. Optional.
  95.      * @param   array   $config  Configuration array for model. Optional.
  96.      *
  97.      * @return  JTable  A JTable object
  98.      *
  99.      * @since   1.6
  100.      */
  101.     public function getTable($type 'Weblink'$prefix 'WeblinksTable'$config array())
  102.     {
  103.         return JTable::getInstance($type$prefix$config);
  104.     }
  105.  
  106.     /**
  107.      * Abstract method for getting the form from the model.
  108.      *
  109.      * @param   array    $data      Data for the form.
  110.      * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  111.      *
  112.      * @return  mixed  A JForm object on success, false on failure
  113.      *
  114.      * @since   1.6
  115.      */
  116.     public function getForm($data array()$loadData true)
  117.     {
  118.         // Get the form.
  119.         $form $this->loadForm('com_weblinks.weblink''weblink'array('control' => 'jform''load_data' => $loadData));
  120.  
  121.         if (empty($form))
  122.         {
  123.             return false;
  124.         }
  125.  
  126.         // Determine correct permissions to check.
  127.         if ($this->getState('weblink.id'))
  128.         {
  129.             // Existing record. Can only edit in selected categories.
  130.             $form->setFieldAttribute('catid''action''core.edit');
  131.         }
  132.         else
  133.         {
  134.             // New record. Can only create in selected categories.
  135.             $form->setFieldAttribute('catid''action''core.create');
  136.         }
  137.  
  138.         // Modify the form based on access controls.
  139.         if (!$this->canEditState((object) $data))
  140.         {
  141.             // Disable fields for display.
  142.             $form->setFieldAttribute('ordering''disabled''true');
  143.             $form->setFieldAttribute('state''disabled''true');
  144.             $form->setFieldAttribute('publish_up''disabled''true');
  145.             $form->setFieldAttribute('publish_down''disabled''true');
  146.  
  147.             // Disable fields while saving.
  148.             // The controller has already verified this is a record you can edit.
  149.             $form->setFieldAttribute('ordering''filter''unset');
  150.             $form->setFieldAttribute('state''filter''unset');
  151.             $form->setFieldAttribute('publish_up''filter''unset');
  152.             $form->setFieldAttribute('publish_down''filter''unset');
  153.         }
  154.  
  155.         return $form;
  156.     }
  157.  
  158.     /**
  159.      * Method to get the data that should be injected in the form.
  160.      *
  161.      * @return  array  The default data is an empty array.
  162.      *
  163.      * @since   1.6
  164.      */
  165.     protected function loadFormData()
  166.     {
  167.         // Check the session for previously entered form data.
  168.         $data JFactory::getApplication()->getUserState('com_weblinks.edit.weblink.data'array());
  169.  
  170.         if (empty($data))
  171.         {
  172.             $data $this->getItem();
  173.  
  174.             // Prime some default values.
  175.             if ($this->getState('weblink.id'== 0)
  176.             {
  177.                 $app JFactory::getApplication();
  178.                 $data->set('catid'$app->input->get('catid'$app->getUserState('com_weblinks.weblinks.filter.category_id')'int'));
  179.             }
  180.         }
  181.  
  182.         $this->preprocessData('com_weblinks.weblink'$data);
  183.  
  184.         return $data;
  185.     }
  186.  
  187.     /**
  188.      * Method to get a single record.
  189.      *
  190.      * @param   integer  $pk  The id of the primary key.
  191.      *
  192.      * @return  mixed  Object on success, false on failure.
  193.      *
  194.      * @since   1.6
  195.      */
  196.     public function getItem($pk null)
  197.     {
  198.         if ($item parent::getItem($pk))
  199.         {
  200.             // Convert the metadata field to an array.
  201.             $registry new JRegistry;
  202.             $registry->loadString($item->metadata);
  203.             $item->metadata $registry->toArray();
  204.  
  205.             // Convert the images field to an array.
  206.             $registry new JRegistry;
  207.             $registry->loadString($item->images);
  208.             $item->images $registry->toArray();
  209.  
  210.             if (!empty($item->id))
  211.             {
  212.                 $item->tags new JHelperTags;
  213.                 $item->tags->getTagIds($item->id'com_weblinks.weblink');
  214.                 $item->metadata['tags'$item->tags;
  215.             }
  216.         }
  217.  
  218.         return $item;
  219.     }
  220.  
  221.     /**
  222.      * Prepare and sanitise the table data prior to saving.
  223.      *
  224.      * @param   JTable  $table  A reference to a JTable object.
  225.      *
  226.      * @return  void 
  227.      *
  228.      * @since   1.6
  229.      */
  230.     protected function prepareTable($table)
  231.     {
  232.         $date JFactory::getDate();
  233.         $user JFactory::getUser();
  234.  
  235.         $table->title htmlspecialchars_decode($table->titleENT_QUOTES);
  236.         $table->alias JApplication::stringURLSafe($table->alias);
  237.  
  238.         if (empty($table->alias))
  239.         {
  240.             $table->alias JApplication::stringURLSafe($table->title);
  241.         }
  242.  
  243.         if (empty($table->id))
  244.         {
  245.             // Set the values
  246.  
  247.             // Set ordering to the last item if not set
  248.             if (empty($table->ordering))
  249.             {
  250.                 $db JFactory::getDbo();
  251.                 $db->setQuery('SELECT MAX(ordering) FROM #__weblinks');
  252.                 $max $db->loadResult();
  253.  
  254.                 $table->ordering $max 1;
  255.             }
  256.             else
  257.             {
  258.                 // Set the values
  259.                 $table->modified    $date->toSql();
  260.                 $table->modified_by $user->get('id');
  261.             }
  262.         }
  263.  
  264.         // Increment the weblink version number.
  265.         $table->version++;
  266.     }
  267.  
  268.     /**
  269.      * A protected method to get a set of ordering conditions.
  270.      *
  271.      * @param   JTable  $table  A JTable object.
  272.      *
  273.      * @return  array  An array of conditions to add to ordering queries.
  274.      *
  275.      * @since   1.6
  276.      */
  277.     protected function getReorderConditions($table)
  278.     {
  279.         $condition array();
  280.         $condition['catid = ' . (int) $table->catid;
  281.  
  282.         return $condition;
  283.     }
  284.  
  285.     /**
  286.      * Method to save the form data.
  287.      *
  288.      * @param   array  $data  The form data.
  289.      *
  290.      * @return  boolean  True on success.
  291.      *
  292.      * @since    3.1
  293.      */
  294.     public function save($data)
  295.     {
  296.         $app JFactory::getApplication();
  297.  
  298.         // Alter the title for save as copy
  299.         if ($app->input->get('task'== 'save2copy')
  300.         {
  301.             list($name$alias$this->generateNewTitle($data['catid']$data['alias']$data['title']);
  302.             $data['title']    $name;
  303.             $data['alias']    $alias;
  304.             $data['state']    0;
  305.         }
  306.  
  307.         return parent::save($data);
  308.     }
  309.  
  310.     /**
  311.      * Method to change the title & alias.
  312.      *
  313.      * @param   integer  $category_id  The id of the parent.
  314.      * @param   string   $alias        The alias.
  315.      * @param   string   $name         The title.
  316.      *
  317.      * @return  array  Contains the modified title and alias.
  318.      *
  319.      * @since   3.1
  320.      */
  321.     protected function generateNewTitle($category_id$alias$name)
  322.     {
  323.         // Alter the title & alias
  324.         $table $this->getTable();
  325.  
  326.         while ($table->load(array('alias' => $alias'catid' => $category_id)))
  327.         {
  328.             if ($name == $table->title)
  329.             {
  330.                 $name JString::increment($name);
  331.             }
  332.  
  333.             $alias JString::increment($alias'dash');
  334.         }
  335.  
  336.         return array($name$alias);
  337.     }
  338. }

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