1 <?php
2 /**
3 * @package Joomla.Legacy
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.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Module table
16 *
17 * @since 1.5
18 */
19 class JTableModule extends JTable
20 {
21 /**
22 * Constructor.
23 *
24 * @param JDatabaseDriver $db Database driver object.
25 *
26 * @since 1.5
27 */
28 public function __construct(JDatabaseDriver $db)
29 {
30 parent::__construct('#__modules', 'id', $db);
31
32 $this->access = (int) JFactory::getConfig()->get('access');
33 }
34
35 /**
36 * Method to compute the default name of the asset.
37 * The default name is in the form table_name.id
38 * where id is the value of the primary key of the table.
39 *
40 * @return string
41 *
42 * @since 3.2
43 */
44 protected function _getAssetName()
45 {
46 $k = $this->_tbl_key;
47
48 return 'com_modules.module.' . (int) $this->$k;
49 }
50
51 /**
52 * Method to return the title to use for the asset table.
53 *
54 * @return string
55 *
56 * @since 3.2
57 */
58 protected function _getAssetTitle()
59 {
60 return $this->title;
61 }
62
63 /**
64 * Method to get the parent asset id for the record
65 *
66 * @param JTable $table A JTable object (optional) for the asset parent
67 * @param integer $id The id (optional) of the content.
68 *
69 * @return integer
70 *
71 * @since 3.2
72 */
73 protected function _getAssetParentId(JTable $table = null, $id = null)
74 {
75 $assetId = null;
76
77 // This is a module that needs to parent with the extension.
78 if ($assetId === null)
79 {
80 // Build the query to get the asset id of the parent component.
81 $query = $this->_db->getQuery(true)
82 ->select($this->_db->quoteName('id'))
83 ->from($this->_db->quoteName('#__assets'))
84 ->where($this->_db->quoteName('name') . ' = ' . $this->_db->quote('com_modules'));
85
86 // Get the asset id from the database.
87 $this->_db->setQuery($query);
88
89 if ($result = $this->_db->loadResult())
90 {
91 $assetId = (int) $result;
92 }
93 }
94
95 // Return the asset id.
96 if ($assetId)
97 {
98 return $assetId;
99 }
100 else
101 {
102 return parent::_getAssetParentId($table, $id);
103 }
104 }
105
106 /**
107 * Overloaded check function.
108 *
109 * @return boolean True if the instance is sane and able to be stored in the database.
110 *
111 * @see JTable::check()
112 * @since 1.5
113 */
114 public function check()
115 {
116 // Check for valid name
117 if (trim($this->title) == '')
118 {
119 $this->setError(JText::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_MODULE'));
120
121 return false;
122 }
123
124 // Check the publish down date is not earlier than publish up.
125 if ((int) $this->publish_down > 0 && $this->publish_down < $this->publish_up)
126 {
127 // Swap the dates.
128 $temp = $this->publish_up;
129 $this->publish_up = $this->publish_down;
130 $this->publish_down = $temp;
131 }
132
133 return true;
134 }
135
136 /**
137 * Overloaded bind function.
138 *
139 * @param array $array Named array.
140 * @param mixed $ignore An optional array or space separated list of properties to ignore while binding.
141 *
142 * @return mixed Null if operation was satisfactory, otherwise returns an error
143 *
144 * @see JTable::bind()
145 * @since 1.5
146 */
147 public function bind($array, $ignore = '')
148 {
149 if (isset($array['params']) && is_array($array['params']))
150 {
151 $registry = new Registry($array['params']);
152 $array['params'] = (string) $registry;
153 }
154
155 // Bind the rules.
156 if (isset($array['rules']) && is_array($array['rules']))
157 {
158 $rules = new JAccessRules($array['rules']);
159 $this->setRules($rules);
160 }
161
162 return parent::bind($array, $ignore);
163 }
164
165 /**
166 * Stores a module.
167 *
168 * @param boolean $updateNulls True to update fields even if they are null.
169 *
170 * @return boolean True on success, false on failure.
171 *
172 * @since 3.7.0
173 */
174 public function store($updateNulls = false)
175 {
176 // Set publish_up, publish_down and checked_out_time to null date if not set
177 if (!$this->publish_up)
178 {
179 $this->publish_up = $this->_db->getNullDate();
180 }
181
182 if (!$this->publish_down)
183 {
184 $this->publish_down = $this->_db->getNullDate();
185 }
186
187 return parent::store($updateNulls);
188 }
189 }
190