Source for file contenthistory.php

Documentation is available at contenthistory.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_contenthistory
  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.  * Categories helper.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_contenthistory
  17.  * @since       3.2
  18.  */
  19. {
  20.     /**
  21.      * Method to put all field names, including nested ones, in a single array for easy lookup.
  22.      *
  23.      * @param   stdClass  $object  Standard class object that may contain one level of nested objects.
  24.      *
  25.      * @return  array  Associative array of all field names, including ones in a nested object.
  26.      *
  27.      * @since   3.2
  28.      */
  29.     public static function createObjectArray($object)
  30.     {
  31.         $result array();
  32.  
  33.         foreach ($object as $name => $value)
  34.         {
  35.             $result[$name$value;
  36.  
  37.             if (is_object($value))
  38.             {
  39.                 foreach ($value as $subName => $subValue)
  40.                 {
  41.                     $result[$subName$subValue;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         return $result;
  47.     }
  48.  
  49.     /**
  50.      * Method to decode JSON-encoded fields in a standard object. Used to unpack JSON strings in the content history data column.
  51.      *
  52.      * @param   stdClass  $jsonString  Standard class object that may contain one or more JSON-encoded fields.
  53.      *
  54.      * @return  stdClass  Object with any JSON-encoded fields unpacked.
  55.      *
  56.      * @since   3.2
  57.      */
  58.     public static function decodeFields($jsonString)
  59.     {
  60.         $object json_decode($jsonString);
  61.  
  62.         if (is_object($object))
  63.         {
  64.             foreach ($object as $name => $value)
  65.             {
  66.                 if ($subObject json_decode($value))
  67.                 {
  68.                     $object->$name $subObject;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         return $object;
  74.     }
  75.  
  76.     /**
  77.      * Method to get field labels for the fields in the JSON-encoded object.
  78.      * First we see if we can find translatable labels for the fields in the object.
  79.      * We translate any we can find and return an array in the format object->name => label.
  80.      *
  81.      * @param   stdClass           $object      Standard class object in the format name->value.
  82.      * @param   JTableContenttype  $typesTable  Table object with content history options.
  83.      *
  84.      * @return  stdClass  Contains two associative arrays.
  85.      *                     $formValues->labels in the format name => label (for example, 'id' => 'Article ID').
  86.      *                     $formValues->values in the format name => value (for example, 'state' => 'Published'.
  87.      *                     This translates the text from the selected option in the form.
  88.      *
  89.      * @since   3.2
  90.      */
  91.     public static function getFormValues($objectJTableContenttype $typesTable)
  92.     {
  93.         $labels array();
  94.         $values array();
  95.         $expandedObjectArray static::createObjectArray($object);
  96.         static::loadLanguageFiles($typesTable->type_alias);
  97.  
  98.         if ($formFile static::getFormFile($typesTable))
  99.         {
  100.             if ($xml simplexml_load_file($formFile))
  101.             {
  102.                 // Now we need to get all of the labels from the form
  103.                 $fieldArray $xml->xpath('//field');
  104.                 $fieldArray array_merge($fieldArray$xml->xpath('//fields'));
  105.  
  106.                 foreach ($fieldArray as $field)
  107.                 {
  108.                     if ($label = (string) $field->attributes()->label)
  109.                     {
  110.                         $labels[(string) $field->attributes()->nameJText::_($label);
  111.                     }
  112.                 }
  113.  
  114.                 // Get values for any list type fields
  115.                 $listFieldArray $xml->xpath('//field[@type="list" or @type="radio"]');
  116.  
  117.                 foreach ($listFieldArray as $field)
  118.                 {
  119.                     $name = (string) $field->attributes()->name;
  120.  
  121.                     if (isset($expandedObjectArray[$name]))
  122.                     {
  123.                         $optionFieldArray $field->xpath('option[@value="' $expandedObjectArray[$name'"]');
  124.                         $valueText trim((string) $optionFieldArray[0]);
  125.                         $values[(string) $field->attributes()->nameJText::_($valueText);
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.  
  131.         $result new stdClass;
  132.         $result->labels $labels;
  133.         $result->values $values;
  134.  
  135.         return $result;
  136.     }
  137.  
  138.     /**
  139.      * Method to get the XML form file for this component. Used to get translated field names for history preview.
  140.      *
  141.      * @param   JTableContenttype  $typesTable  Table object with content history options.
  142.      *
  143.      * @return  mixed  JModel object if successful, false if no model found.
  144.      *
  145.      * @since   3.2
  146.      */
  147.     public static function getFormFile(JTableContenttype $typesTable)
  148.     {
  149.         $result false;
  150.         jimport('joomla.filesystem.file');
  151.         jimport('joomla.filesystem.folder');
  152.  
  153.         // First, see if we have a file name in the $typesTable
  154.         $options json_decode($typesTable->content_history_options);
  155.  
  156.         if (is_object($options&& isset($options->formFile&& JFile::exists(JPATH_ROOT '/' $options->formFile))
  157.         {
  158.             $result JPATH_ROOT '/' $options->formFile;
  159.         }
  160.         else
  161.         {
  162.             $aliasArray explode('.'$typesTable->type_alias);
  163.  
  164.             if (count($aliasArray== 2)
  165.             {
  166.                 $component ($aliasArray[1== 'category''com_categories' $aliasArray[0];
  167.                 $path  JFolder::makeSafe(JPATH_ADMINISTRATOR '/components/' $component '/models/forms/');
  168.                 $file JFile::makeSafe($aliasArray[1'.xml');
  169.                 $result JFile::exists($path $file$path $file false;
  170.             }
  171.         }
  172.  
  173.         return $result;
  174.     }
  175.  
  176.     /**
  177.      * Method to query the database using values from lookup objects.
  178.      *
  179.      * @param   stdClass  $lookup  The std object with the values needed to do the query.
  180.      * @param   mixed     $value   The value used to find the matching title or name. Typically the id.
  181.      *
  182.      * @return  mixed  Value from database (for example, name or title) on success, false on failure.
  183.      *
  184.      * @since   3.2
  185.      */
  186.     public static function getLookupValue($lookup$value)
  187.     {
  188.         $result false;
  189.  
  190.         if (isset($lookup->sourceColumn&& isset($lookup->targetTable&& isset($lookup->targetColumn)&& isset($lookup->displayColumn))
  191.         {
  192.             $db JFactory::getDbo();
  193.             $query $db->getQuery(true);
  194.             $query->select($db->quoteName($lookup->displayColumn))
  195.                 ->from($db->quoteName($lookup->targetTable))
  196.                 ->where($db->quoteName($lookup->targetColumn' = ' $db->quote($value));
  197.             $db->setQuery($query);
  198.  
  199.             try
  200.             {
  201.                 $result $db->loadResult();
  202.             }
  203.             catch (Exception $e)
  204.             {
  205.                 // Ignore any errors and just return false
  206.                 return false;
  207.             }
  208.         }
  209.  
  210.         return $result;
  211.     }
  212.  
  213.     /**
  214.      * Method to remove fields from the object based on values entered in the #__content_types table.
  215.      *
  216.      * @param   stdClass           $object     Object to be passed to view layout file.
  217.      * @param   JTableContenttype  $typeTable  Table object with content history options.
  218.      *
  219.      * @return  stdClass  object with hidden fields removed.
  220.      *
  221.      * @since   3.2
  222.      */
  223.     public static function hideFields($objectJTableContenttype $typeTable)
  224.     {
  225.         if ($options json_decode($typeTable->content_history_options))
  226.         {
  227.             if (isset($options->hideFields&& is_array($options->hideFields))
  228.             {
  229.                 foreach ($options->hideFields as $field)
  230.                 {
  231.                     unset($object->$field);
  232.                 }
  233.             }
  234.         }
  235.  
  236.         return $object;
  237.     }
  238.  
  239.     /**
  240.      * Method to load the language files for the component whose history is being viewed.
  241.      *
  242.      * @param   string  $typeAlias  The type alias, for example 'com_content.article'.
  243.      *
  244.      * @return  void 
  245.      *
  246.      * @since   3.2
  247.      */
  248.     public static function loadLanguageFiles($typeAlias)
  249.     {
  250.         $aliasArray explode('.'$typeAlias);
  251.  
  252.         if (is_array($aliasArray&& count($aliasArray== 2)
  253.         {
  254.             $component ($aliasArray[1== 'category''com_categories' $aliasArray[0];
  255.             $lang JFactory::getLanguage();
  256.  
  257.             /**
  258.              * Loading language file from the administrator/language directory then
  259.              * loading language file from the administrator/components/extension/language directory
  260.              */
  261.             $lang->load($componentJPATH_ADMINISTRATORnullfalsetrue)
  262.             || $lang->load($componentJPath::clean(JPATH_ADMINISTRATOR '/components/' $component)nullfalsetrue);
  263.  
  264.             // Force loading of back-end global language file
  265.             $lang->load('joomla'JPath::clean(JPATH_ADMINISTRATOR)nullfalsetrue);
  266.         }
  267.     }
  268.  
  269.     /**
  270.      * Method to create object to pass to the layout. Format is as follows:
  271.      * field is std object with name, value.
  272.      *
  273.      * Value can be a std object with name, value pairs.
  274.      *
  275.      * @param   stdClass  $object      The std object from the JSON string. Can be nested 1 level deep.
  276.      * @param   stdClass  $formValues  Standard class of label and value in an associative array.
  277.      *
  278.      * @return  stdClass  Object with translated labels where available
  279.      *
  280.      * @since   3.2
  281.      */
  282.     public static function mergeLabels($object$formValues)
  283.     {
  284.         $result new stdClass;
  285.  
  286.         $labelsArray $formValues->labels;
  287.         $valuesArray $formValues->values;
  288.  
  289.         foreach ($object as $name => $value)
  290.         {
  291.             $result->$name new stdClass;
  292.             $result->$name->name $name;
  293.             $result->$name->value = isset($valuesArray[$name]$valuesArray[$name$value;
  294.             $result->$name->label = isset($labelsArray[$name]$labelsArray[$name$name;
  295.  
  296.             if (is_object($value))
  297.             {
  298.                 $subObject new stdClass;
  299.  
  300.                 foreach ($value as $subName => $subValue)
  301.                 {
  302.                     $subObject->$subName new stdClass;
  303.                     $subObject->$subName->name $subName;
  304.                     $subObject->$subName->value = isset($valuesArray[$subName]$valuesArray[$subName$subValue;
  305.                     $subObject->$subName->label = isset($labelsArray[$subName]$labelsArray[$subName$subName;
  306.                     $result->$name->value $subObject;
  307.                 }
  308.             }
  309.         }
  310.  
  311.         return $result;
  312.     }
  313.  
  314.     /**
  315.      * Method to prepare the object for the preview and compare views.
  316.      *
  317.      * @param   JTableContenthistory  $table  Table object loaded with data.
  318.      *
  319.      * @return  stdClass  Object ready for the views.
  320.      *
  321.      * @since   3.2
  322.      */
  323.     public static function prepareData(JTableContenthistory $table)
  324.     {
  325.         $object static::decodeFields($table->version_data);
  326.         $typesTable JTable::getInstance('Contenttype');
  327.         $typesTable->load(array('type_id' => $table->ucm_type_id));
  328.         $formValues static::getFormValues($object$typesTable);
  329.         $object static::mergeLabels($object$formValues);
  330.         $object static::hideFields($object$typesTable);
  331.         $object static::processLookupFields($object$typesTable);
  332.  
  333.         return $object;
  334.     }
  335.  
  336.     /**
  337.      * Method to process any lookup values found in the content_history_options column for this table.
  338.      * This allows category title and user name to be displayed instead of the id column.
  339.      *
  340.      * @param   stdClass           $object      The std object from the JSON string. Can be nested 1 level deep.
  341.      * @param   JTableContenttype  $typesTable  Table object loaded with data.
  342.      *
  343.      * @return  stdClass  Object with lookup values inserted.
  344.      *
  345.      * @since   3.2
  346.      */
  347.     public static function processLookupFields($objectJTableContenttype $typesTable)
  348.     {
  349.         if ($options json_decode($typesTable->content_history_options))
  350.         {
  351.             if (isset($options->displayLookup&& is_array($options->displayLookup))
  352.             {
  353.                 foreach ($options->displayLookup as $lookup)
  354.                 {
  355.                     $sourceColumn = isset($lookup->sourceColumn$lookup->sourceColumn false;
  356.                     $sourceValue = isset($object->$sourceColumn->value$object->$sourceColumn->value false;
  357.  
  358.                     if ($sourceColumn && $sourceValue && ($lookupValue static::getLookupValue($lookup$sourceValue)))
  359.                     {
  360.                         $object->$sourceColumn->value $lookupValue;
  361.                     }
  362.                 }
  363.             }
  364.         }
  365.  
  366.         return $object;
  367.     }
  368. }

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