1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage UCM
5 *
6 * @copyright Copyright (C) 2005 - 2017 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_PLATFORM') or die;
11
12 /**
13 * Base class for implementing UCM
14 *
15 * @since 3.1
16 */
17 class JUcmBase implements JUcm
18 {
19 /**
20 * The UCM type object
21 *
22 * @var JUcmType
23 * @since 3.1
24 */
25 protected $type;
26
27 /**
28 * The alias for the content table
29 *
30 * @var string
31 * @since 3.1
32 */
33 protected $alias;
34
35 /**
36 * Instantiate the UcmBase.
37 *
38 * @param string $alias The alias string
39 * @param JUcmType $type The type object
40 *
41 * @since 3.1
42 */
43 public function __construct($alias = null, JUcmType $type = null)
44 {
45 // Setup dependencies.
46 $input = JFactory::getApplication()->input;
47 $this->alias = isset($alias) ? $alias : $input->get('option') . '.' . $input->get('view');
48
49 $this->type = isset($type) ? $type : $this->getType();
50 }
51
52 /**
53 * Store data to the appropriate table
54 *
55 * @param array $data Data to be stored
56 * @param JTableInterface $table JTable Object
57 * @param string $primaryKey The primary key name
58 *
59 * @return boolean True on success
60 *
61 * @since 3.1
62 * @throws Exception
63 */
64 protected function store($data, JTableInterface $table = null, $primaryKey = null)
65 {
66 if (!$table)
67 {
68 $table = JTable::getInstance('Ucm');
69 }
70
71 $ucmId = isset($data['ucm_id']) ? $data['ucm_id'] : null;
72 $primaryKey = $primaryKey ?: $ucmId;
73
74 if (isset($primaryKey))
75 {
76 $table->load($primaryKey);
77 }
78
79 try
80 {
81 $table->bind($data);
82 }
83 catch (RuntimeException $e)
84 {
85 throw new Exception($e->getMessage(), 500, $e);
86 }
87
88 try
89 {
90 $table->store();
91 }
92 catch (RuntimeException $e)
93 {
94 throw new Exception($e->getMessage(), 500, $e);
95 }
96
97 return true;
98 }
99
100 /**
101 * Get the UCM Content type.
102 *
103 * @return JUcmType The UCM content type
104 *
105 * @since 3.1
106 */
107 public function getType()
108 {
109 if (!$this->type)
110 {
111 $this->type = new JUcmType($this->alias);
112 }
113
114 return $this->type;
115 }
116
117 /**
118 * Method to map the base ucm fields
119 *
120 * @param array $original Data array
121 * @param JUcmType $type UCM Content Type
122 *
123 * @return array Data array of UCM mappings
124 *
125 * @since 3.1
126 */
127 public function mapBase($original, JUcmType $type = null)
128 {
129 $type = $type ?: $this->type;
130
131 $data = array(
132 'ucm_type_id' => $type->id,
133 'ucm_item_id' => $original[$type->primary_key],
134 'ucm_language_id' => JHelperContent::getLanguageId($original['language']),
135 );
136
137 return $data;
138 }
139 }
140