Source for file content.php

Documentation is available at content.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  UCM
  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('JPATH_BASE') or die;
  11.  
  12. /**
  13.  * Base class for implementing UCM
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  UCM
  17.  * @since       3.1
  18.  */
  19. class JUcmContent extends JUcmBase
  20. {
  21.     /**
  22.      * The related table object
  23.      *
  24.      * @var    JTable 
  25.      * @since  3.1
  26.      */
  27.     protected $table;
  28.  
  29.     /**
  30.      * The UCM type object
  31.      *
  32.      * @var    JUcmType 
  33.      * @since  3.1
  34.      */
  35.     public $type;
  36.  
  37.     /**
  38.      * The alias for the content table
  39.      *
  40.      * @var    string 
  41.      * @since  3.1
  42.      */
  43.     protected $alias;
  44.  
  45.     /**
  46.      * The UCM data array
  47.      *
  48.      * @var    array 
  49.      * @since  3.1
  50.      */
  51.     public $ucmData;
  52.  
  53.     /**
  54.      * Instantiate JUcmContent.
  55.      *
  56.      * @param   JTable    $table  The table object
  57.      * @param   sring     $alias  The type alias
  58.      * @param   JUcmType  $type   The type object
  59.      *
  60.      * @since   3.1
  61.      */
  62.     public function __construct(JTable $table = null, $alias = null, JUcmType $type = null)
  63.     {
  64.         // Setup dependencies.
  65.         $input = JFactory::getApplication()->input;
  66.         $this->alias = isset($alias) ? $alias : $input->get('option') . '.' . $input->get('view');
  67.  
  68.         $this->type = isset($type) ? $type : $this->getType();
  69.  
  70.         if ($table)
  71.         {
  72.             $this->table = $table;
  73.         }
  74.         else
  75.         {
  76.             $tableObject = json_decode($this->type->type->table);
  77.             $this->table = JTable::getInstance($tableObject->special->type, $tableObject->special->prefix, $tableObject->special->config);
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Method to save the data
  83.      *
  84.      * @param   array     $original  The original data to be saved
  85.      * @param   JUcmType  $type      The UCM Type object
  86.      *
  87.      * @return  boolean  true
  88.      *
  89.      * @since   3.1
  90.      */
  91.     public function save($original = null, JUcmType $type = null)
  92.     {
  93.         $type    = $type ? $type : $this->type;
  94.         $ucmData = $original ? $this->mapData($original, $type) : $this->ucmData;
  95.  
  96.         // Store the Common fields
  97.         $this->store($ucmData['common']);
  98.  
  99.         // Store the special fields
  100.         if (isset($ucmData['special']))
  101.         {
  102.             $table = $this->table;
  103.             $this->store($ucmData['special'], $table, '');
  104.         }
  105.  
  106.         return true;
  107.     }
  108.  
  109.     /**
  110.      * Delete content from the Core Content table
  111.      *
  112.      * @param   mixed     $pk    The string/array of id's to delete
  113.      * @param   JUcmType  $type  The content type object
  114.      *
  115.      * @return  boolean  True if success
  116.      *
  117.      * @since   3.1
  118.      */
  119.     public function delete($pk, JUcmType $type = null)
  120.     {
  121.         $db   = JFactory::getDbo();
  122.         $type = $type ? $type : $this->type;
  123.  
  124.         if (is_array($pk))
  125.         {
  126.             $pk = implode(',', $pk);
  127.         }
  128.  
  129.         $query = $db->getQuery(true)
  130.             ->delete('#__ucm_content')
  131.             ->where($db->quoteName('core_type_id') . ' = ' . (int) $type->type_id)
  132.             ->where($db->quoteName('core_content_item_id') . ' IN (' . $pk . ')');
  133.  
  134.         $db->setQuery($query);
  135.         $db->execute();
  136.  
  137.         return true;
  138.     }
  139.  
  140.     /**
  141.      * Map the original content to the Core Content fields
  142.      *
  143.      * @param   array     $original  The original data array
  144.      * @param   JUcmType  $type      Type object for this data
  145.      *
  146.      * @return  object  $ucmData  The mapped UCM data
  147.      *
  148.      * @since   3.1
  149.      */
  150.     public function mapData($original, JUcmType $type = null)
  151.     {
  152.         $contentType = isset($type) ? $type : $this->type;
  153.  
  154.         $fields = json_decode($contentType->type->field_mappings);
  155.  
  156.         $ucmData = array();
  157.  
  158.         $common = (is_object($fields->common)) ? $fields->common : $fields->common[0];
  159.  
  160.         foreach ($common as $i => $field)
  161.         {
  162.             if ($field && $field != 'null' && array_key_exists($field, $original))
  163.             {
  164.                 $ucmData['common'][$i] = $original[$field];
  165.             }
  166.         }
  167.  
  168.         if (array_key_exists('special', $ucmData))
  169.         {
  170.             $special = (is_object($fields->special)) ? $fields->special : $fields->special[0];
  171.  
  172.             foreach ($special as $i => $field)
  173.             {
  174.                 if ($field && $field != 'null' && array_key_exists($field, $original))
  175.                 {
  176.                     $ucmData['special'][$i] = $original[$field];
  177.                 }
  178.             }
  179.         }
  180.  
  181.         $ucmData['common']['core_type_alias'] = $contentType->type->type_alias;
  182.         $ucmData['common']['core_type_id']    = $contentType->type->type_id;
  183.  
  184.         if (isset($ucmData['special']))
  185.         {
  186.             $ucmData['special']['ucm_id'] = $ucmData['common']['ucm_id'];
  187.         }
  188.  
  189.         $this->ucmData = $ucmData;
  190.  
  191.         return $this->ucmData;
  192.     }
  193.  
  194.     /**
  195.      * Store data to the appropriate table
  196.      *
  197.      * @param   array    $data        Data to be stored
  198.      * @param   JTable   $table       JTable Object
  199.      * @param   boolean  $primaryKey  Flag that is true for data that are using #__ucm_content as their primary table
  200.      *
  201.      * @return  Boolean  true on success
  202.      *
  203.      * @since   3.1
  204.      */
  205.     protected function store($data, JTable $table = null, $primaryKey = null)
  206.     {
  207.         $table = $table ? $table : JTable::getInstance('Corecontent');
  208.  
  209.         $typeId     = $this->getType()->type->type_id;
  210.         $primaryKey = $primaryKey ? $primaryKey : $this->getPrimaryKey($typeId, $data['core_content_item_id']);
  211.  
  212.         if (!$primaryKey)
  213.         {
  214.             // Store the core UCM mappings
  215.             $baseData = array();
  216.             $baseData['ucm_type_id']     = $typeId;
  217.             $baseData['ucm_item_id']     = $data['core_content_item_id'];
  218.             $baseData['ucm_language_id'] = JHelperContent::getLanguageId($data['core_language']);
  219.  
  220.             if (parent::store($baseData))
  221.             {
  222.                 $primaryKey = $this->getPrimaryKey($typeId, $data['core_content_item_id']);
  223.             }
  224.         }
  225.  
  226.         return parent::store($data, $table, $primaryKey);
  227.     }
  228.  
  229.     /**
  230.      * Get the value of the primary key from #__ucm_base
  231.      *
  232.      * @param   string   $typeId         The ID for the type
  233.      * @param   integer  $contentItemId  Value of the primary key in the legacy or secondary table
  234.      *
  235.      * @return  integer  The integer of the primary key
  236.      *
  237.      * @since   3.1
  238.      */
  239.     public function getPrimaryKey($typeId, $contentItemId)
  240.     {
  241.         $db = JFactory::getDbo();
  242.         $queryccid = $db->getQuery(true);
  243.         $queryccid->select($db->quoteName('ucm_id'))
  244.             ->from($db->quoteName('#__ucm_base'))
  245.             ->where(
  246.                 array(
  247.                     $db->quoteName('ucm_item_id') . ' = ' . $db->quote($contentItemId),
  248.                     $db->quoteName('ucm_type_id') . ' = ' . $db->quote($typeId)
  249.                 )
  250.             );
  251.         $db->setQuery($queryccid);
  252.         $primaryKey = $db->loadResult();
  253.  
  254.         return $primaryKey;
  255.     }
  256. }

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