/administrator/components/com_phocagallery/models/phocagalleryco.php

https://github.com/thtinh/hb · PHP · 247 lines · 189 code · 37 blank · 21 comment · 32 complexity · f121db29c5f3a863b62bffa9ea494261 MD5 · raw file

  1. <?php
  2. /*
  3. * @package Joomla 1.5
  4. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  5. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  6. *
  7. * @component Phoca Gallery
  8. * @copyright Copyright (C) Jan Pavelka www.phoca.cz
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  10. */
  11. defined('_JEXEC') or die();
  12. jimport('joomla.application.component.model');
  13. class PhocaGalleryCpModelPhocaGalleryCo extends JModel
  14. {
  15. function __construct() {
  16. parent::__construct();
  17. $array = JRequest::getVar('cid', 0, '', 'array');
  18. $this->setId((int)$array[0]);
  19. }
  20. function setId($id) {
  21. $this->_id = $id;
  22. $this->_data = null;
  23. }
  24. function &getData() {
  25. if ($this->_loadData()) {
  26. $user = &JFactory::getUser();
  27. if ($this->_data->cat_access > $user->get('aid', 0)) {
  28. JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
  29. return;
  30. }
  31. } else {
  32. $this->_initData();
  33. }
  34. return $this->_data;
  35. }
  36. function isCheckedOut( $uid=0 ) {
  37. if ($this->_loadData()) {
  38. if ($uid) {
  39. return ($this->_data->checked_out && $this->_data->checked_out != $uid);
  40. } else {
  41. return $this->_data->checked_out;
  42. }
  43. }
  44. }
  45. function checkin() {
  46. if ($this->_id) {
  47. $phocagallery = & $this->getTable('phocagallerycomments');
  48. if(! $phocagallery->checkin($this->_id)) {
  49. $this->setError($this->_db->getErrorMsg());
  50. return false;
  51. }
  52. }
  53. return false;
  54. }
  55. function checkout($uid = null) {
  56. if ($this->_id) {
  57. // Make sure we have a user id to checkout the article with
  58. if (is_null($uid)) {
  59. $user =& JFactory::getUser();
  60. $uid = $user->get('id');
  61. }
  62. // Lets get to it and checkout the thing...
  63. $phocagallery = & $this->getTable('phocagallerycomments');
  64. if(!$phocagallery->checkout($uid, $this->_id)) {
  65. $this->setError($this->_db->getErrorMsg());
  66. return false;
  67. }
  68. return true;
  69. }
  70. return false;
  71. }
  72. function store($data) {
  73. //If there is no title and no alias, use filename as title and alias
  74. if ($data['title'] == '') {
  75. $data['title'] = '-';
  76. }
  77. $row =& $this->getTable('phocagallerycomments');
  78. // Bind the form fields to the Phoca gallery table
  79. if (!$row->bind($data)) {
  80. $this->setError($this->_db->getErrorMsg());
  81. return false;
  82. }
  83. // Create the timestamp for the date
  84. if ($row->date =='') {
  85. $row->date = gmdate('Y-m-d H:i:s');
  86. }
  87. // if new item, order last in appropriate group
  88. if (!$row->id) {
  89. $where = 'catid = ' . (int) $row->catid ;
  90. $row->ordering = $row->getNextOrder( $where );
  91. }
  92. // Make sure the Phoca gallery table is valid
  93. if (!$row->check()) {
  94. $this->setError($this->_db->getErrorMsg());
  95. return false;
  96. }
  97. // Store the Phoca gallery table to the database
  98. if (!$row->store()) {
  99. $this->setError($this->_db->getErrorMsg());
  100. return false;
  101. }
  102. return true;
  103. }
  104. function delete($cid = array()) {
  105. if (count( $cid )) {
  106. JArrayHelper::toInteger($cid);
  107. $cids = implode( ',', $cid );
  108. $query = 'DELETE FROM #__phocagallery_comments'
  109. . ' WHERE id IN ( '.$cids.' )';
  110. $this->_db->setQuery( $query );
  111. if(!$this->_db->query()) {
  112. $this->setError($this->_db->getErrorMsg());
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. function publish($cid = array(), $publish = 1) {
  119. $user =& JFactory::getUser();
  120. if (count( $cid )) {
  121. JArrayHelper::toInteger($cid);
  122. $cids = implode( ',', $cid );
  123. $query = 'UPDATE #__phocagallery_comments'
  124. . ' SET published = '.(int) $publish
  125. . ' WHERE id IN ( '.$cids.' )'
  126. . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
  127. ;
  128. $this->_db->setQuery( $query );
  129. if (!$this->_db->query()) {
  130. $this->setError($this->_db->getErrorMsg());
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. function move($direction) {
  137. $row =& $this->getTable('phocagallerycomments');
  138. if (!$row->load($this->_id)) {
  139. $this->setError($this->_db->getErrorMsg());
  140. return false;
  141. }
  142. if (!$row->move( $direction, ' catid = '.(int) $row->catid.' AND published >= 0 ' )) {
  143. $this->setError($this->_db->getErrorMsg());
  144. return false;
  145. }
  146. return true;
  147. }
  148. function saveorder($cid = array(), $order) {
  149. $row =& $this->getTable('phocagallerycomments');
  150. $groupings = array();
  151. // update ordering values
  152. for( $i=0; $i < count($cid); $i++ )
  153. {
  154. $row->load( (int) $cid[$i] );
  155. // track categories
  156. $groupings[] = $row->catid;
  157. if ($row->ordering != $order[$i])
  158. {
  159. $row->ordering = $order[$i];
  160. if (!$row->store()) {
  161. $this->setError($this->_db->getErrorMsg());
  162. return false;
  163. }
  164. }
  165. }
  166. // execute updateOrder for each parent group
  167. $groupings = array_unique( $groupings );
  168. foreach ($groupings as $group){
  169. $row->reorder('catid = '.(int) $group);
  170. }
  171. return true;
  172. }
  173. function _loadData() {
  174. // Lets load the content if it doesn't already exist
  175. if (empty($this->_data))
  176. {
  177. $query = 'SELECT co.*, cc.title AS category, u.username as commentusername, u.name AS commentname, '.
  178. ' cc.published AS cat_pub, cc.access AS cat_access'.
  179. ' FROM #__phocagallery_comments AS co' .
  180. ' LEFT JOIN #__phocagallery_categories AS cc ON cc.id = co.catid' .
  181. ' LEFT JOIN #__users AS u ON u.id = co.userid' .
  182. ' WHERE co.id = '.(int) $this->_id;
  183. $this->_db->setQuery($query);
  184. $this->_data = $this->_db->loadObject();
  185. return (boolean) $this->_data;
  186. }
  187. return true;
  188. }
  189. function _initData() {
  190. if (empty($this->_data)) {
  191. $phocagallery = new stdClass();
  192. $phocagallery->id = 0;
  193. $phocagallery->catid = 0;
  194. $phocagallery->userid = 0;
  195. $phocagallery->date = null;
  196. $phocagallery->title = null;
  197. $phocagallery->comment = null;
  198. $phocagallery->published = 0;
  199. $phocagallery->checked_out = 0;
  200. $phocagallery->checked_out_time = 0;
  201. $phocagallery->ordering = 0;
  202. $phocagallery->params = null;
  203. $phocagallery->category = null;
  204. $this->_data = $phocagallery;
  205. return (boolean) $this->_data;
  206. }
  207. return true;
  208. }
  209. }
  210. ?>