1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Table
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
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13 use Joomla\Utilities\ArrayHelper;
14
15 /**
16 * Extension table
17 *
18 * @since 11.1
19 */
20 class JTableExtension extends JTable
21 {
22 /**
23 * Constructor
24 *
25 * @param JDatabaseDriver $db Database driver object.
26 *
27 * @since 11.1
28 */
29 public function __construct($db)
30 {
31 parent::__construct('#__extensions', 'extension_id', $db);
32 }
33
34 /**
35 * Overloaded check function
36 *
37 * @return boolean True if the object is ok
38 *
39 * @see JTable::check()
40 * @since 11.1
41 */
42 public function check()
43 {
44 // Check for valid name
45 if (trim($this->name) == '' || trim($this->element) == '')
46 {
47 $this->setError(JText::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_EXTENSION'));
48
49 return false;
50 }
51
52 if (!$this->extension_id)
53 {
54 if (!$this->custom_data)
55 {
56 $this->custom_data = '';
57 }
58
59 if (!$this->system_data)
60 {
61 $this->system_data = '';
62 }
63 }
64
65 return true;
66 }
67
68 /**
69 * Overloaded bind function
70 *
71 * @param array $array Named array
72 * @param mixed $ignore An optional array or space separated list of properties
73 * to ignore while binding.
74 *
75 * @return mixed Null if operation was satisfactory, otherwise returns an error
76 *
77 * @see JTable::bind()
78 * @since 11.1
79 */
80 public function bind($array, $ignore = '')
81 {
82 if (isset($array['params']) && is_array($array['params']))
83 {
84 $registry = new Registry($array['params']);
85 $array['params'] = (string) $registry;
86 }
87
88 if (isset($array['control']) && is_array($array['control']))
89 {
90 $registry = new Registry($array['control']);
91 $array['control'] = (string) $registry;
92 }
93
94 return parent::bind($array, $ignore);
95 }
96
97 /**
98 * Method to create and execute a SELECT WHERE query.
99 *
100 * @param array $options Array of options
101 *
102 * @return string The database query result
103 *
104 * @since 11.1
105 */
106 public function find($options = array())
107 {
108 // Get the JDatabaseQuery object
109 $query = $this->_db->getQuery(true);
110
111 foreach ($options as $col => $val)
112 {
113 $query->where($col . ' = ' . $this->_db->quote($val));
114 }
115
116 $query->select($this->_db->quoteName('extension_id'))
117 ->from($this->_db->quoteName('#__extensions'));
118 $this->_db->setQuery($query);
119
120 return $this->_db->loadResult();
121 }
122
123 /**
124 * Method to set the publishing state for a row or list of rows in the database
125 * table. The method respects checked out rows by other users and will attempt
126 * to checkin rows that it can after adjustments are made.
127 *
128 * @param mixed $pks An optional array of primary key values to update. If not
129 * set the instance property value is used.
130 * @param integer $state The publishing state. eg. [0 = unpublished, 1 = published]
131 * @param integer $userId The user id of the user performing the operation.
132 *
133 * @return boolean True on success.
134 *
135 * @since 11.1
136 */
137 public function publish($pks = null, $state = 1, $userId = 0)
138 {
139 $k = $this->_tbl_key;
140
141 // Sanitize input.
142 $pks = ArrayHelper::toInteger($pks);
143 $userId = (int) $userId;
144 $state = (int) $state;
145
146 // If there are no primary keys set check to see if the instance key is set.
147 if (empty($pks))
148 {
149 if ($this->$k)
150 {
151 $pks = array($this->$k);
152 }
153 // Nothing to set publishing state on, return false.
154 else
155 {
156 $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
157
158 return false;
159 }
160 }
161
162 // Build the WHERE clause for the primary keys.
163 $where = $k . '=' . implode(' OR ' . $k . '=', $pks);
164
165 // Determine if there is checkin support for the table.
166 if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time'))
167 {
168 $checkin = '';
169 }
170 else
171 {
172 $checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';
173 }
174
175 // Update the publishing state for rows with the given primary keys.
176 $query = $this->_db->getQuery(true)
177 ->update($this->_db->quoteName($this->_tbl))
178 ->set($this->_db->quoteName('enabled') . ' = ' . (int) $state)
179 ->where('(' . $where . ')' . $checkin);
180 $this->_db->setQuery($query);
181 $this->_db->execute();
182
183 // If checkin is supported and all rows were adjusted, check them in.
184 if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
185 {
186 // Checkin the rows.
187 foreach ($pks as $pk)
188 {
189 $this->checkin($pk);
190 }
191 }
192
193 // If the JTable instance value is in the list of primary keys that were set, set the instance.
194 if (in_array($this->$k, $pks))
195 {
196 $this->enabled = $state;
197 }
198
199 $this->setError('');
200
201 return true;
202 }
203 }
204