/libraries/molajo/component/tables/content.php

https://github.com/ot2sen/Molajo · PHP · 270 lines · 185 code · 17 blank · 68 comment · 17 complexity · 1a4b8053ba92f398d3ffb54959be0d04 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Molajo
  4. * @subpackage Table
  5. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  6. * @copyright Copyright (C) 2011 Amy Stephen. All rights reserved.
  7. * @license GNU General Public License Version 2, or later http://www.gnu.org/licenses/gpl.html
  8. */
  9. defined('MOLAJO') or die;
  10. /**
  11. * Content Table Class
  12. *
  13. * @package Molajo
  14. * @subpackage Table
  15. * @since 1.0
  16. * @link
  17. */
  18. class MolajoTableContent extends MolajoTable
  19. {
  20. /**
  21. * __construct
  22. *
  23. * @param database A database connector object
  24. *
  25. * @since 1.0
  26. */
  27. function __construct($db)
  28. {
  29. parent::__construct('#'.JRequest::getCmd('ComponentTable'), 'id', $db);
  30. }
  31. /**
  32. * _getAssetTitle
  33. *
  34. * Method to return the title to use for the asset table.
  35. *
  36. * @return string
  37. * @since 1.6
  38. */
  39. protected function _getAssetTitle()
  40. {
  41. return $this->title;
  42. }
  43. /**
  44. * _getAssetParentId
  45. *
  46. * Get the parent asset id for the record
  47. *
  48. * @return int
  49. * @since 1.6
  50. */
  51. protected function _getAssetParentId ($table = null, $id = null)
  52. {
  53. /** initialize **/
  54. $assetId = null;
  55. $db = $this->getDbo();
  56. /** retrieve parent category asset **/
  57. if ($this->catid) {
  58. $query = $db->getQuery(true);
  59. $query->select('asset_id');
  60. $query->from('#__categories');
  61. $query->where('id = '.(int) $this->catid);
  62. $this->_db->setQuery($query);
  63. if ($result = $this->_db->loadResult()) {
  64. $assetId = (int) $result;
  65. }
  66. }
  67. /** return results **/
  68. if ($assetId) {
  69. return $assetId;
  70. } else {
  71. return parent::_getAssetParentId($table, $id);
  72. }
  73. }
  74. /**
  75. * bind
  76. *
  77. * Overloaded bind function
  78. *
  79. * @param array $hash named array
  80. *
  81. * @return null|string null is operation was satisfactory, otherwise returns an error
  82. * @see MolajoTable:bind
  83. * @since 1.6
  84. */
  85. public function bind($array, $ignore = '')
  86. {
  87. $jsonModel = JModel::getInstance('ModelConfiguration', 'Molajo', array('ignore_request' => true));
  88. $results = $jsonModel->getOptionList (MOLAJO_CONFIG_OPTION_ID_JSON_FIELDS);
  89. foreach ($results as $count => $result) {
  90. if (isset($array[$result->value]) && is_array($array[$result->value])) {
  91. $registry = new MolajoRegistry();
  92. $registry->loadArray($array[$result->value]);
  93. $array[$result->value] = (string)$registry;
  94. }
  95. }
  96. /** bind rules **/
  97. if (isset($array['rules']) && is_array($array['rules'])) {
  98. $rules = new JRules($array['rules']);
  99. $this->setRules($rules);
  100. }
  101. /** parent **/
  102. return parent::bind($array, $ignore);
  103. }
  104. /**
  105. * check
  106. *
  107. * Overloaded check function
  108. *
  109. * @return boolean
  110. * @see MolajoTable::check
  111. * @since 1.6
  112. */
  113. public function check()
  114. {
  115. /** title **/
  116. if (trim($this->title) == '') {
  117. $this->setError(MolajoText::_('MOLAJO_WARNING_PROVIDE_VALID_NAME'));
  118. return false;
  119. }
  120. /** alias **/
  121. $this->_getAlias();
  122. if (trim(str_replace('-','',$this->alias)) == '') {
  123. $this->alias = MolajoFactory::getDate()->format('Y-m-d-H-i-s');
  124. }
  125. /** text fields **/
  126. if (trim(str_replace('&nbsp;', '', $this->content_text)) == '') {
  127. $this->content_text = '';
  128. }
  129. /** publish up and down dates **/
  130. if (intval($this->publish_down) > 0 && $this->publish_down < $this->publish_up) {
  131. $temp = $this->publish_up;
  132. $this->publish_up = $this->publish_down;
  133. $this->publish_down = $temp;
  134. }
  135. /** standard cleanup: eliminate extra spaces between phrases, remove cr (\r) and lf (\n) characters from string **/
  136. if (!empty($this->metakey)) {
  137. $bad_characters = array("\n", "\r", "\"", "<", ">");
  138. $after_clean = JString::str_ireplace($bad_characters, "", $this->metakey);
  139. $keys = explode(',', $after_clean);
  140. $clean_keys = array();
  141. foreach($keys as $key) {
  142. if (trim($key)) {
  143. $clean_keys[] = trim($key);
  144. }
  145. }
  146. $this->metakey = implode(", ", $clean_keys);
  147. }
  148. return true;
  149. }
  150. /**
  151. * store
  152. *
  153. * Overriden MolajoTable::store to set modified data and user id.
  154. *
  155. * @param boolean True to update fields even if they are null.
  156. *
  157. * @return boolean True on success.
  158. * @since 1.6
  159. */
  160. public function store($updateNulls = false)
  161. {
  162. $date = MolajoFactory::getDate();
  163. $user = MolajoFactory::getUser();
  164. if ($this->id) {
  165. $this->modified = $date->toMySQL();
  166. $this->modified_by = $user->get('id');
  167. if (intval($this->publish_down) > 0 && $this->publish_down < $this->publish_up) {
  168. $temp = $this->publish_up;
  169. $this->publish_up = $this->publish_down;
  170. $this->publish_down = $temp;
  171. }
  172. } else {
  173. if (intval($this->created)) {
  174. } else {
  175. $this->created = $date->toMySQL();
  176. }
  177. if (empty($this->created_by)) {
  178. $this->created_by = $user->get('id');
  179. }
  180. }
  181. $this->_getAlias();
  182. if (trim(str_replace('-','',$this->alias)) == '') {
  183. $this->alias = MolajoFactory::getDate()->format('Y-m-d-H-i-s');
  184. }
  185. return parent::store($updateNulls);
  186. }
  187. /**
  188. * _getAlias
  189. *
  190. * Verify, or create and then verify, a unique value for Alias
  191. *
  192. * @return boolean
  193. * @since 1.6
  194. */
  195. protected function _getAlias ()
  196. {
  197. /** initialize **/
  198. $aliasFound = false;
  199. $db = $this->getDbo();
  200. /** alias **/
  201. if (trim($this->alias) == '') {
  202. $this->alias = $this->title;
  203. }
  204. $this->alias = JApplication::stringURLSafe($this->alias);
  205. /** do not check alias for version saves **/
  206. if ($this->state == MOLAJO_STATE_VERSION) {
  207. return true;
  208. }
  209. /** check if unique, if not increment until unique value found **/
  210. $i = 1;
  211. $tempAlias = $this->alias;
  212. do {
  213. $query = $db->getQuery(true);
  214. $query->select($db->namequote('alias'));
  215. $query->from($db->namequote('#'.JRequest::getCmd('ComponentTable')));
  216. $query->where($db->namequote('alias').' = '.$db->quote($this->alias));
  217. $query->where($db->namequote('id').' <> '. (int) $this->id);
  218. $query->where($db->namequote('state').' <> '.(int) MOLAJO_STATE_VERSION);
  219. $this->_db->setQuery($query);
  220. if ($result = $this->_db->loadResult()) {
  221. $aliasFound = false;
  222. } else {
  223. $aliasFound = true;
  224. }
  225. if ($aliasFound === false) {
  226. $tempAlias = $this->alias.'-'.$i;
  227. }
  228. $i++;
  229. } while ($aliasFound === false);
  230. $this->alias = $tempAlias;
  231. return;
  232. }
  233. }