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 table
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_banners
  17.  * @since       1.5
  18.  */
  19. class BannersTableBanner extends JTable
  20. {
  21.     /**
  22.      * Constructor
  23.      *
  24.      * @since   1.5
  25.      */
  26.     public function __construct(&$_db)
  27.     {
  28.         parent::__construct('#__banners''id'$_db);
  29.         $date JFactory::getDate();
  30.         $this->created $date->toSql();
  31.     }
  32.  
  33.     public function clicks()
  34.     {
  35.         $query 'UPDATE #__banners'
  36.         . ' SET clicks = (clicks + 1)'
  37.         . ' WHERE id = ' . (int) $this->id;
  38.  
  39.         $this->_db->setQuery($query);
  40.         $this->_db->execute();
  41.     }
  42.  
  43.     /**
  44.      * Overloaded check function
  45.      *
  46.      * @return  boolean 
  47.      * @see     JTable::check
  48.      * @since   1.5
  49.      */
  50.     public function check()
  51.     {
  52.         // Set name
  53.         $this->name htmlspecialchars_decode($this->nameENT_QUOTES);
  54.  
  55.         // Set alias
  56.         $this->alias JApplication::stringURLSafe($this->alias);
  57.         if (empty($this->alias))
  58.         {
  59.             $this->alias JApplication::stringURLSafe($this->name);
  60.         }
  61.  
  62.         // Check the publish down date is not earlier than publish up.
  63.         if ($this->publish_down $this->_db->getNullDate(&& $this->publish_down $this->publish_up)
  64.         {
  65.             $this->setError(JText::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
  66.             return false;
  67.         }
  68.  
  69.         // Set ordering
  70.         if ($this->state 0)
  71.         {
  72.             // Set ordering to 0 if state is archived or trashed
  73.             $this->ordering 0;
  74.         elseif (empty($this->ordering))
  75.         {
  76.             // Set ordering to last if ordering was 0
  77.             $this->ordering self::getNextOrder($this->_db->quoteName('catid').'=' $this->_db->quote($this->catid).' AND state>=0');
  78.         }
  79.  
  80.         return true;
  81.     }
  82.  
  83.     /**
  84.      * Overloaded bind function
  85.      *
  86.      * @param   array  $hash named array
  87.      * @return  null|string   null is operation was satisfactory, otherwise returns an error
  88.      * @see JTable:bind
  89.      * @since 1.5
  90.      */
  91.     public function bind($array$ignore array())
  92.     {
  93.         if (isset($array['params']&& is_array($array['params']))
  94.         {
  95.             $registry new JRegistry;
  96.             $registry->loadArray($array['params']);
  97.  
  98.             if ((int) $registry->get('width'00){
  99.                 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED'JText::_('COM_BANNERS_FIELD_WIDTH_LABEL')));
  100.                 return false;
  101.             }
  102.  
  103.             if ((int) $registry->get('height'00){
  104.                 $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED'JText::_('COM_BANNERS_FIELD_HEIGHT_LABEL')));
  105.                 return false;
  106.             }
  107.  
  108.             // Converts the width and height to an absolute numeric value:
  109.             $width abs((int) $registry->get('width'0));
  110.             $height abs((int) $registry->get('height'0));
  111.  
  112.             // Sets the width and height to an empty string if = 0
  113.             $registry->set('width'($width $width ''));
  114.             $registry->set('height'($height $height ''));
  115.  
  116.             $array['params'= (string) $registry;
  117.         }
  118.  
  119.         if (isset($array['imptotal']))
  120.         {
  121.             $array['imptotal'abs((int) $array['imptotal']);
  122.         }
  123.  
  124.         return parent::bind($array$ignore);
  125.     }
  126.     /**
  127.      * Method to store a row
  128.      *
  129.      * @param boolean $updateNulls True to update fields even if they are null.
  130.      */
  131.     public function store($updateNulls false)
  132.     {
  133.         if (empty($this->id))
  134.         {
  135.             $purchase_type $this->purchase_type;
  136.             if ($purchase_type && $this->cid)
  137.             {
  138.                 $client JTable::getInstance('Client''BannersTable');
  139.                 $client->load($this->cid);
  140.                 $purchase_type $client->purchase_type;
  141.             }
  142.             if ($purchase_type 0)
  143.             {
  144.                 $params JComponentHelper::getParams('com_banners');
  145.                 $purchase_type $params->get('purchase_type');
  146.             }
  147.  
  148.             switch($purchase_type)
  149.             {
  150.                 case 1:
  151.                     $this->reset $this->_db->getNullDate();
  152.                     break;
  153.                 case 2:
  154.                     $date JFactory::getDate('+1 year '.date('Y-m-d'strtotime('now')));
  155.                     $this->reset $this->_db->quote($date->toSql());
  156.                     break;
  157.                 case 3:
  158.                     $date JFactory::getDate('+1 month '.date('Y-m-d'strtotime('now')));
  159.                     $this->reset $this->_db->quote($date->toSql());
  160.                     break;
  161.                 case 4:
  162.                     $date JFactory::getDate('+7 day '.date('Y-m-d'strtotime('now')));
  163.                     $this->reset $this->_db->quote($date->toSql());
  164.                     break;
  165.                 case 5:
  166.                     $date JFactory::getDate('+1 day '.date('Y-m-d'strtotime('now')));
  167.                     $this->reset $this->_db->quote($date->toSql());
  168.                     break;
  169.             }
  170.             // Store the row
  171.             parent::store($updateNulls);
  172.         }
  173.         else
  174.         {
  175.             // Get the old row
  176.             $oldrow JTable::getInstance('Banner''BannersTable');
  177.             if (!$oldrow->load($this->id&& $oldrow->getError())
  178.             {
  179.                 $this->setError($oldrow->getError());
  180.             }
  181.  
  182.             // Verify that the alias is unique
  183.             $table JTable::getInstance('Banner''BannersTable');
  184.             if ($table->load(array('alias' => $this->alias'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
  185.             {
  186.                 $this->setError(JText::_('COM_BANNERS_ERROR_UNIQUE_ALIAS'));
  187.                 return false;
  188.             }
  189.  
  190.             // Store the new row
  191.             parent::store($updateNulls);
  192.  
  193.             // Need to reorder ?
  194.             if ($oldrow->state >= && ($this->state || $oldrow->catid != $this->catid))
  195.             {
  196.                 // Reorder the oldrow
  197.                 $this->reorder($this->_db->quoteName('catid').'=' $this->_db->quote($oldrow->catid).' AND state>=0');
  198.             }
  199.         }
  200.         return count($this->getErrors()) == 0;
  201.     }
  202.  
  203.     /**
  204.      * Method to set the publishing state for a row or list of rows in the database
  205.      * table.  The method respects checked out rows by other users and will attempt
  206.      * to checkin rows that it can after adjustments are made.
  207.      *
  208.      * @param   mixed    An optional array of primary key values to update.  If not
  209.      *                     set the instance property value is used.
  210.      * @param   integer The publishing state. eg. [0 = unpublished, 1 = published, 2=archived, -2=trashed]
  211.      * @param   integer The user id of the user performing the operation.
  212.      * @return  boolean  True on success.
  213.      * @since   1.6
  214.      */
  215.     public function publish($pks null$state 1$userId 0)
  216.     {
  217.         $k $this->_tbl_key;
  218.  
  219.         // Sanitize input.
  220.         JArrayHelper::toInteger($pks);
  221.         $userId = (int) $userId;
  222.         $state  = (int) $state;
  223.  
  224.         // If there are no primary keys set check to see if the instance key is set.
  225.         if (empty($pks))
  226.         {
  227.             if ($this->$k)
  228.             {
  229.                 $pks array($this->$k);
  230.             }
  231.             // Nothing to set publishing state on, return false.
  232.             else {
  233.                 $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  234.                 return false;
  235.             }
  236.         }
  237.  
  238.         // Get an instance of the table
  239.         $table JTable::getInstance('Banner''BannersTable');
  240.  
  241.         // For all keys
  242.         foreach ($pks as $pk)
  243.         {
  244.             // Load the banner
  245.             if (!$table->load($pk))
  246.             {
  247.                 $this->setError($table->getError());
  248.             }
  249.  
  250.             // Verify checkout
  251.             if ($table->checked_out == || $table->checked_out == $userId)
  252.             {
  253.                 // Change the state
  254.                 $table->state $state;
  255.                 $table->checked_out 0;
  256.                 $table->checked_out_time $this->_db->getNullDate();
  257.  
  258.                 // Check the row
  259.                 $table->check();
  260.  
  261.                 // Store the row
  262.                 if (!$table->store())
  263.                 {
  264.                     $this->setError($table->getError());
  265.                 }
  266.             }
  267.         }
  268.         return count($this->getErrors()) == 0;
  269.     }
  270.  
  271.     /**
  272.      * Method to set the sticky state for a row or list of rows in the database
  273.      * table.  The method respects checked out rows by other users and will attempt
  274.      * to checkin rows that it can after adjustments are made.
  275.      *
  276.      * @param   mixed    An optional array of primary key values to update.  If not
  277.      *                     set the instance property value is used.
  278.      * @param   integer The sticky state. eg. [0 = unsticked, 1 = sticked]
  279.      * @param   integer The user id of the user performing the operation.
  280.      * @return  boolean  True on success.
  281.      * @since   1.6
  282.      */
  283.     public function stick($pks null$state 1$userId 0)
  284.     {
  285.         $k $this->_tbl_key;
  286.  
  287.         // Sanitize input.
  288.         JArrayHelper::toInteger($pks);
  289.         $userId = (int) $userId;
  290.         $state  = (int) $state;
  291.  
  292.         // If there are no primary keys set check to see if the instance key is set.
  293.         if (empty($pks))
  294.         {
  295.             if ($this->$k)
  296.             {
  297.                 $pks array($this->$k);
  298.             }
  299.             // Nothing to set publishing state on, return false.
  300.             else {
  301.                 $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  302.                 return false;
  303.             }
  304.         }
  305.  
  306.         // Get an instance of the table
  307.         $table JTable::getInstance('Banner''BannersTable');
  308.  
  309.         // For all keys
  310.         foreach ($pks as $pk)
  311.         {
  312.             // Load the banner
  313.             if (!$table->load($pk))
  314.             {
  315.                 $this->setError($table->getError());
  316.             }
  317.  
  318.             // Verify checkout
  319.             if ($table->checked_out == || $table->checked_out == $userId)
  320.             {
  321.                 // Change the state
  322.                 $table->sticky $state;
  323.                 $table->checked_out 0;
  324.                 $table->checked_out_time $this->_db->getNullDate();
  325.  
  326.                 // Check the row
  327.                 $table->check();
  328.  
  329.                 // Store the row
  330.                 if (!$table->store())
  331.                 {
  332.                     $this->setError($table->getError());
  333.                 }
  334.             }
  335.         }
  336.         return count($this->getErrors()) == 0;
  337.     }
  338. }

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