Source for file level.php

Documentation is available at level.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_users
  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.  * User view level model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_users
  17.  * @since       1.6
  18.  */
  19. class UsersModelLevel extends JModelAdmin
  20. {
  21.     /**
  22.      * @var    array    A list of the access levels in use.
  23.      * @since   1.6
  24.      */
  25.     protected $levelsInUse = null;
  26.  
  27.     /**
  28.      * Method to test whether a record can be deleted.
  29.      *
  30.      * @param   object    $record    A record object.
  31.      *
  32.      * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  33.      * @since   1.6
  34.      */
  35.     protected function canDelete($record)
  36.     {
  37.         // Check if the access level is being used by any content.
  38.         if ($this->levelsInUse === null)
  39.         {
  40.             // Populate the list once.
  41.             $this->levelsInUse = array();
  42.  
  43.             $db        $this->getDbo();
  44.             $query    $db->getQuery(true)
  45.                 ->select('DISTINCT access');
  46.                 // from is added dynamically
  47.  
  48.             // Get all the tables and the prefix
  49.             $tables $db->getTableList();
  50.             //$fields = $db->getTableFields($tables);
  51.             $prefix $db->getPrefix();
  52.  
  53.             foreach ($tables as $table)
  54.             {
  55.                 // Get all of the columns in the table
  56.                 $fields $db->getTableColumns($table);
  57.  
  58.                 // We are looking for the access field.  If custom tables are using something other
  59.                 // than the 'access' field they are on their own unfortunately.
  60.                 // Also make sure the table prefix matches the live db prefix (eg, it is not a "bak_" table)
  61.                 if ((strpos($table$prefix=== 0&& (isset($fields['access'])))
  62.                 {
  63.                     // Lookup the distinct values of the field.
  64.                     $query->clear('from')
  65.                         ->from($db->quoteName($table));
  66.                     $db->setQuery($query);
  67.  
  68.                     try
  69.                     {
  70.                         $values $db->loadColumn();
  71.                     }
  72.                     catch (RuntimeException $e)
  73.                     {
  74.                         $this->setError($e->getMessage());
  75.                         return false;
  76.                     }
  77.  
  78.                     $this->levelsInUse = array_merge($this->levelsInUse$values);
  79.  
  80.                     // TODO Could assemble an array of the tables used by each view level list those,
  81.                     // giving the user a clue in the error where to look.
  82.                 }
  83.             }
  84.  
  85.             // Get uniques.
  86.             $this->levelsInUse = array_unique($this->levelsInUse);
  87.  
  88.             // Ok, after all that we are ready to check the record :)
  89.         }
  90.  
  91.         if (in_array($record->id$this->levelsInUse))
  92.         {
  93.             $this->setError(JText::sprintf('COM_USERS_ERROR_VIEW_LEVEL_IN_USE'$record->id$record->title));
  94.  
  95.             return false;
  96.         }
  97.  
  98.         return parent::canDelete($record);
  99.     }
  100.  
  101.     /**
  102.      * Returns a reference to the a Table object, always creating it.
  103.      *
  104.      * @param   type    The table type to instantiate
  105.      * @param   string    A prefix for the table class name. Optional.
  106.      * @param   array  Configuration array for model. Optional.
  107.      * @return  JTable    A database object
  108.      * @since   1.6
  109.     */
  110.     public function getTable($type 'Viewlevel'$prefix 'JTable'$config array())
  111.     {
  112.         $return JTable::getInstance($type$prefix$config);
  113.  
  114.         return $return;
  115.     }
  116.  
  117.     /**
  118.      * Method to get a single record.
  119.      *
  120.      * @param   integer    The id of the primary key.
  121.      * @return  mixed  Object on success, false on failure.
  122.      * @since   1.6
  123.      */
  124.     public function getItem($pk null)
  125.     {
  126.         $result parent::getItem($pk);
  127.  
  128.         // Convert the params field to an array.
  129.         $result->rules json_decode($result->rules);
  130.  
  131.         return $result;
  132.     }
  133.  
  134.     /**
  135.      * Method to get the record form.
  136.      *
  137.      * @param   array  $data        An optional array of data for the form to interogate.
  138.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  139.      * @return  JForm    A JForm object on success, false on failure
  140.      * @since   1.6
  141.      */
  142.     public function getForm($data array()$loadData true)
  143.     {
  144.         // Get the form.
  145.         $form $this->loadForm('com_users.level''level'array('control' => 'jform''load_data' => $loadData));
  146.  
  147.         if (empty($form))
  148.         {
  149.             return false;
  150.         }
  151.  
  152.         return $form;
  153.     }
  154.  
  155.     /**
  156.      * Method to get the data that should be injected in the form.
  157.      *
  158.      * @return  mixed  The data for the form.
  159.      * @since   1.6
  160.      */
  161.     protected function loadFormData()
  162.     {
  163.         // Check the session for previously entered form data.
  164.         $data JFactory::getApplication()->getUserState('com_users.edit.level.data'array());
  165.  
  166.         if (empty($data))
  167.         {
  168.             $data $this->getItem();
  169.         }
  170.  
  171.         $this->preprocessData('com_users.level'$data);
  172.  
  173.         return $data;
  174.     }
  175.  
  176.     /**
  177.      * Override preprocessForm to load the user plugin group instead of content.
  178.      *
  179.      * @param   object    form object.
  180.      * @param   mixed    The data expected for the form.
  181.      * @throws    Exception if there is an error in the form event.
  182.      * @since   1.6
  183.      */
  184.     protected function preprocessForm(JForm $form$data$groups '')
  185.     {
  186.         parent::preprocessForm($form$data'user');
  187.     }
  188.  
  189.     /**
  190.      * Method to save the form data.
  191.      *
  192.      * @param   array  The form data.
  193.      * @return  boolean  True on success.
  194.      * @since   1.6
  195.      */
  196.     public function save($data)
  197.     {
  198.         if (!isset($data['rules']))
  199.         {
  200.             $data['rules'array();
  201.         }
  202.  
  203.         return parent::save($data);
  204.     }
  205. }

Documentation generated on Tue, 19 Nov 2013 15:07:01 +0100 by phpDocumentor 1.4.3