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.  * Weblink Table class
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_weblinks
  17.  * @since       1.5
  18.  */
  19. class WeblinksTableWeblink extends JTable
  20. {
  21.     /**
  22.      * Constructor
  23.      *
  24.      * @param   JDatabaseDriver  &$db  A database connector object
  25.      */
  26.     public function __construct(&$db)
  27.     {
  28.         parent::__construct('#__weblinks''id'$db);
  29.     }
  30.  
  31.     /**
  32.      * Overloaded bind function to pre-process the params.
  33.      *
  34.      * @param   mixed  $array   An associative array or object to bind to the JTable instance.
  35.      * @param   mixed  $ignore  An optional array or space separated list of properties to ignore while binding.
  36.      *
  37.      * @return  boolean  True on success.
  38.      *
  39.      * @see     JTable:bind
  40.      * @since   1.5
  41.      */
  42.     public function bind($array$ignore '')
  43.     {
  44.         if (isset($array['params']&& is_array($array['params']))
  45.         {
  46.             $registry new JRegistry;
  47.             $registry->loadArray($array['params']);
  48.             $array['params'= (string) $registry;
  49.         }
  50.  
  51.         if (isset($array['metadata']&& is_array($array['metadata']))
  52.         {
  53.             $registry new JRegistry;
  54.             $registry->loadArray($array['metadata']);
  55.             $array['metadata'= (string) $registry;
  56.         }
  57.  
  58.         if (isset($array['images']&& is_array($array['images']))
  59.         {
  60.             $registry new JRegistry;
  61.             $registry->loadArray($array['images']);
  62.             $array['images'= (string) $registry;
  63.         }
  64.  
  65.         return parent::bind($array$ignore);
  66.     }
  67.  
  68.     /**
  69.      * Overload the store method for the Weblinks table.
  70.      *
  71.      * @param   boolean    Toggle whether null values should be updated.
  72.      * @return  boolean  True on success, false on failure.
  73.      * @since   1.6
  74.      */
  75.     public function store($updateNulls false)
  76.     {
  77.         $date    JFactory::getDate();
  78.         $user    JFactory::getUser();
  79.  
  80.         if ($this->id)
  81.         {
  82.             // Existing item
  83.             $this->modified        $date->toSql();
  84.             $this->modified_by    $user->get('id');
  85.         }
  86.         else
  87.         {
  88.             // New weblink. A weblink created and created_by field can be set by the user,
  89.             // so we don't touch either of these if they are set.
  90.             if (!(int) $this->created)
  91.             {
  92.                 $this->created $date->toSql();
  93.             }
  94.             if (empty($this->created_by))
  95.             {
  96.                 $this->created_by $user->get('id');
  97.             }
  98.         }
  99.  
  100.         // Set publish_up to null date if not set
  101.         if (!$this->publish_up)
  102.         {
  103.             $this->publish_up $this->_db->getNullDate();
  104.         }
  105.  
  106.         // Set publish_down to null date if not set
  107.         if (!$this->publish_down)
  108.         {
  109.             $this->publish_down $this->_db->getNullDate();
  110.         }
  111.  
  112.         // Verify that the alias is unique
  113.         $table JTable::getInstance('Weblink''WeblinksTable');
  114.  
  115.         if ($table->load(array('alias' => $this->alias'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
  116.         {
  117.             $this->setError(JText::_('COM_WEBLINKS_ERROR_UNIQUE_ALIAS'));
  118.             return false;
  119.         }
  120.  
  121.         // Convert IDN urls to punycode
  122.         $this->url JStringPunycode::urlToPunycode($this->url);
  123.  
  124.         return parent::store($updateNulls);
  125.     }
  126.  
  127.     /**
  128.      * Overloaded check method to ensure data integrity.
  129.      *
  130.      * @return  boolean  True on success.
  131.      */
  132.     public function check()
  133.     {
  134.         if (JFilterInput::checkAttribute(array ('href'$this->url)))
  135.         {
  136.             $this->setError(JText::_('COM_WEBLINKS_ERR_TABLES_PROVIDE_URL'));
  137.             return false;
  138.         }
  139.  
  140.         // check for valid name
  141.         if (trim($this->title== '')
  142.         {
  143.             $this->setError(JText::_('COM_WEBLINKS_ERR_TABLES_TITLE'));
  144.             return false;
  145.         }
  146.  
  147.         // check for existing name
  148.         $query 'SELECT id FROM #__weblinks WHERE title = '.$this->_db->quote($this->title).' AND catid = '.(int) $this->catid;
  149.         $this->_db->setQuery($query);
  150.  
  151.         $xid = (int) $this->_db->loadResult();
  152.         if ($xid && $xid != (int) $this->id)
  153.         {
  154.             $this->setError(JText::_('COM_WEBLINKS_ERR_TABLES_NAME'));
  155.             return false;
  156.         }
  157.  
  158.         if (empty($this->alias))
  159.         {
  160.             $this->alias $this->title;
  161.         }
  162.         $this->alias JApplication::stringURLSafe($this->alias);
  163.         if (trim(str_replace('-'''$this->alias)) == '')
  164.         {
  165.             $this->alias JFactory::getDate()->format("Y-m-d-H-i-s");
  166.         }
  167.  
  168.         // Check the publish down date is not earlier than publish up.
  169.         if ($this->publish_down $this->_db->getNullDate(&& $this->publish_down $this->publish_up)
  170.         {
  171.             $this->setError(JText::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
  172.             return false;
  173.         }
  174.  
  175.         // clean up keywords -- eliminate extra spaces between phrases
  176.         // and cr (\r) and lf (\n) characters from string
  177.         if (!empty($this->metakey))
  178.         {
  179.             // only process if not empty
  180.             $bad_characters array("\n""\r""\"""<"">")// array of characters to remove
  181.             $after_clean JString::str_ireplace($bad_characters""$this->metakey)// remove bad characters
  182.             $keys explode(','$after_clean)// create array using commas as delimiter
  183.             $clean_keys array();
  184.  
  185.             foreach ($keys as $key)
  186.             {
  187.                 if (trim($key)) {  // ignore blank keywords
  188.                     $clean_keys[trim($key);
  189.                 }
  190.             }
  191.             $this->metakey implode(", "$clean_keys)// put array back together delimited by ", "
  192.         }
  193.  
  194.         return true;
  195.     }
  196.  
  197.     /**
  198.      * Method to set the publishing state for a row or list of rows in the database
  199.      * table.  The method respects checked out rows by other users and will attempt
  200.      * to checkin rows that it can after adjustments are made.
  201.      *
  202.      * @param   mixed    An optional array of primary key values to update.  If not
  203.      *                     set the instance property value is used.
  204.      * @param   integer The publishing state. eg. [0 = unpublished, 1 = published]
  205.      * @param   integer The user id of the user performing the operation.
  206.      * @return  boolean  True on success.
  207.      * @since   1.0.4
  208.      */
  209.     public function publish($pks null$state 1$userId 0)
  210.     {
  211.         $k $this->_tbl_key;
  212.  
  213.         // Sanitize input.
  214.         JArrayHelper::toInteger($pks);
  215.         $userId = (int) $userId;
  216.         $state  = (int) $state;
  217.  
  218.         // If there are no primary keys set check to see if the instance key is set.
  219.         if (empty($pks))
  220.         {
  221.             if ($this->$k)
  222.             {
  223.                 $pks array($this->$k);
  224.             }
  225.             // Nothing to set publishing state on, return false.
  226.             else {
  227.                 $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  228.                 return false;
  229.             }
  230.         }
  231.  
  232.         // Build the WHERE clause for the primary keys.
  233.         $where $k.'='.implode(' OR '.$k.'='$pks);
  234.  
  235.         // Determine if there is checkin support for the table.
  236.         if (!property_exists($this'checked_out'|| !property_exists($this'checked_out_time'))
  237.         {
  238.             $checkin '';
  239.         }
  240.         else
  241.         {
  242.             $checkin ' AND (checked_out = 0 OR checked_out = '.(int) $userId.')';
  243.         }
  244.  
  245.         // Update the publishing state for rows with the given primary keys.
  246.         $this->_db->setQuery(
  247.             'UPDATE '.$this->_db->quoteName($this->_tbl.
  248.             ' SET '.$this->_db->quoteName('state').' = '.(int) $state .
  249.             ' WHERE ('.$where.')' .
  250.             $checkin
  251.         );
  252.  
  253.         try
  254.         {
  255.             $this->_db->execute();
  256.         }
  257.         catch (RuntimeException $e)
  258.         {
  259.             $this->setError($e->getMessage());
  260.             return false;
  261.         }
  262.  
  263.         // If checkin is supported and all rows were adjusted, check them in.
  264.         if ($checkin && (count($pks== $this->_db->getAffectedRows()))
  265.         {
  266.             // Checkin the rows.
  267.             foreach ($pks as $pk)
  268.             {
  269.                 $this->checkin($pk);
  270.             }
  271.         }
  272.  
  273.         // If the JTable instance value is in the list of primary keys that were set, set the instance.
  274.         if (in_array($this->$k$pks))
  275.         {
  276.             $this->state $state;
  277.         }
  278.  
  279.         $this->setError('');
  280.         return true;
  281.     }
  282. }

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