PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_k2/models/extrafields.php

http://getk2.googlecode.com/
PHP | 313 lines | 255 code | 50 blank | 8 comment | 25 complexity | 115184f0ecc21deb6984debd08f29c67 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: extrafields.php 1531 2012-03-26 09:54:29Z lefteris.kavadas $
  4. * @package K2
  5. * @author JoomlaWorks http://www.joomlaworks.net
  6. * @copyright Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
  7. * @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die('Restricted access');
  11. jimport('joomla.application.component.model');
  12. JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
  13. class K2ModelExtraFields extends JModel
  14. {
  15. function getData() {
  16. $mainframe = &JFactory::getApplication();
  17. $option = JRequest::getCmd('option');
  18. $view = JRequest::getCmd('view');
  19. $db = & JFactory::getDBO();
  20. $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  21. $limitstart = $mainframe->getUserStateFromRequest($option.$view.'.limitstart', 'limitstart', 0, 'int');
  22. $filter_order = $mainframe->getUserStateFromRequest($option.$view.'filter_order', 'filter_order', 'groupname', 'cmd');
  23. $filter_order_Dir = $mainframe->getUserStateFromRequest($option.$view.'filter_order_Dir', 'filter_order_Dir', 'ASC', 'word');
  24. $filter_state = $mainframe->getUserStateFromRequest($option.$view.'filter_state', 'filter_state', -1, 'int');
  25. $search = $mainframe->getUserStateFromRequest($option.$view.'search', 'search', '', 'string');
  26. $search = JString::strtolower($search);
  27. $filter_type = $mainframe->getUserStateFromRequest($option.$view.'filter_type', 'filter_type', '', 'string');
  28. $filter_group = $mainframe->getUserStateFromRequest($option.$view.'filter_group', 'filter_group', 0, 'int');
  29. $query = "SELECT exf.*, exfg.name as groupname FROM #__k2_extra_fields AS exf LEFT JOIN #__k2_extra_fields_groups exfg ON exf.group=exfg.id WHERE exf.id>0";
  30. if ($filter_state > -1) {
  31. $query .= " AND published={$filter_state}";
  32. }
  33. if ($search) {
  34. $query .= " AND LOWER( exf.name ) LIKE ".$db->Quote('%'.$db->getEscaped($search, true).'%', false);
  35. }
  36. if ($filter_type) {
  37. $query .= " AND `type`=".$db->Quote($filter_type);
  38. }
  39. if ($filter_group) {
  40. $query .= " AND `group`={$filter_group}";
  41. }
  42. if (!$filter_order) {
  43. $filter_order = '`group`';
  44. }
  45. if ($filter_order == 'ordering') {
  46. $query .= " ORDER BY `group`, ordering {$filter_order_Dir}";
  47. }
  48. else {
  49. $query .= " ORDER BY {$filter_order} {$filter_order_Dir}, `group`, ordering";
  50. }
  51. $db->setQuery($query, $limitstart, $limit);
  52. $rows = $db->loadObjectList();
  53. return $rows;
  54. }
  55. function getTotal() {
  56. $mainframe = &JFactory::getApplication();
  57. $option = JRequest::getCmd('option');
  58. $view = JRequest::getCmd('view');
  59. $db = & JFactory::getDBO();
  60. $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  61. $limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');
  62. $filter_state = $mainframe->getUserStateFromRequest($option.$view.'filter_state', 'filter_state', 1, 'int');
  63. $search = $mainframe->getUserStateFromRequest($option.$view.'search', 'search', '', 'string');
  64. $search = JString::strtolower($search);
  65. $filter_type = $mainframe->getUserStateFromRequest($option.$view.'filter_type', 'filter_type', '', 'string');
  66. $filter_group = $mainframe->getUserStateFromRequest($option.$view.'filter_group', 'filter_group', '', 'string');
  67. $query = "SELECT COUNT(*) FROM #__k2_extra_fields WHERE id>0";
  68. if ($filter_state > -1) {
  69. $query .= " AND published={$filter_state}";
  70. }
  71. if ($search) {
  72. $query .= " AND LOWER( name ) LIKE ".$db->Quote('%'.$db->getEscaped($search, true).'%', false);
  73. }
  74. if ($filter_type) {
  75. $query .= " AND `type`=".$db->Quote($filter_type);
  76. }
  77. if ($filter_group) {
  78. $query .= " AND `group`=".$db->Quote($filter_group);
  79. }
  80. $db->setQuery($query);
  81. $total = $db->loadresult();
  82. return $total;
  83. }
  84. function publish() {
  85. $mainframe = &JFactory::getApplication();
  86. $cid = JRequest::getVar('cid');
  87. $row = & JTable::getInstance('K2ExtraField', 'Table');
  88. foreach ($cid as $id) {
  89. $row->load($id);
  90. $row->publish($id, 1);
  91. }
  92. $cache = & JFactory::getCache('com_k2');
  93. $cache->clean();
  94. $mainframe->redirect('index.php?option=com_k2&view=extrafields');
  95. }
  96. function unpublish() {
  97. $mainframe = &JFactory::getApplication();
  98. $cid = JRequest::getVar('cid');
  99. $row = & JTable::getInstance('K2ExtraField', 'Table');
  100. foreach ($cid as $id) {
  101. $row->load($id);
  102. $row->publish($id, 0);
  103. }
  104. $cache = & JFactory::getCache('com_k2');
  105. $cache->clean();
  106. $mainframe->redirect('index.php?option=com_k2&view=extrafields');
  107. }
  108. function saveorder() {
  109. $mainframe = &JFactory::getApplication();
  110. $db = & JFactory::getDBO();
  111. $cid = JRequest::getVar('cid', array (0), 'post', 'array');
  112. $total = count($cid);
  113. $order = JRequest::getVar('order', array (0), 'post', 'array');
  114. JArrayHelper::toInteger($order, array (0));
  115. $row = & JTable::getInstance('K2ExtraField', 'Table');
  116. $groupings = array ();
  117. for ($i = 0; $i < $total; $i++) {
  118. $row->load((int)$cid[$i]);
  119. $groupings[] = $row->group;
  120. if ($row->ordering != $order[$i]) {
  121. $row->ordering = $order[$i];
  122. if (!$row->store()) {
  123. JError::raiseError(500, $db->getErrorMsg());
  124. }
  125. }
  126. }
  127. $params = &JComponentHelper::getParams('com_k2');
  128. if(!$params->get('disableCompactOrdering')){
  129. $groupings = array_unique($groupings);
  130. foreach ($groupings as $group) {
  131. $row->reorder("`group` = {$group}");
  132. }
  133. }
  134. $cache = & JFactory::getCache('com_k2');
  135. $cache->clean();
  136. $msg = JText::_('K2_NEW_ORDERING_SAVED');
  137. $mainframe->redirect('index.php?option=com_k2&view=extrafields', $msg);
  138. }
  139. function orderup() {
  140. $mainframe = &JFactory::getApplication();
  141. $cid = JRequest::getVar('cid');
  142. $row = & JTable::getInstance('K2ExtraField', 'Table');
  143. $row->load($cid[0]);
  144. $row->move(-1, "`group` = '{$row->group}'");
  145. $params = &JComponentHelper::getParams('com_k2');
  146. if(!$params->get('disableCompactOrdering'))
  147. $row->reorder("`group` = '{$row->group}'");
  148. $cache = & JFactory::getCache('com_k2');
  149. $cache->clean();
  150. $msg = JText::_('K2_NEW_ORDERING_SAVED');
  151. $mainframe->redirect('index.php?option=com_k2&view=extrafields', $msg);
  152. }
  153. function orderdown() {
  154. $mainframe = &JFactory::getApplication();
  155. $cid = JRequest::getVar('cid');
  156. $row = & JTable::getInstance('K2ExtraField', 'Table');
  157. $row->load($cid[0]);
  158. $row->move(1, "`group` = '{$row->group}'");
  159. $params = &JComponentHelper::getParams('com_k2');
  160. if(!$params->get('disableCompactOrdering'))
  161. $row->reorder("`group` = '{$row->group}'");
  162. $cache = & JFactory::getCache('com_k2');
  163. $cache->clean();
  164. $msg = JText::_('K2_NEW_ORDERING_SAVED');
  165. $mainframe->redirect('index.php?option=com_k2&view=extrafields', $msg);
  166. }
  167. function remove() {
  168. $mainframe = &JFactory::getApplication();
  169. $db = & JFactory::getDBO();
  170. $cid = JRequest::getVar('cid');
  171. $row = & JTable::getInstance('K2ExtraField', 'Table');
  172. foreach ($cid as $id) {
  173. $row->load($id);
  174. $row->delete($id);
  175. }
  176. $cache = & JFactory::getCache('com_k2');
  177. $cache->clean();
  178. $mainframe->redirect('index.php?option=com_k2&view=extrafields', JText::_('K2_DELETE_COMPLETED'));
  179. }
  180. function getExtraFieldsGroup() {
  181. $cid = JRequest::getVar('cid');
  182. $row = & JTable::getInstance('K2ExtraFieldsGroup', 'Table');
  183. $row->load($cid);
  184. return $row;
  185. }
  186. function getGroups($filter = false) {
  187. $mainframe = &JFactory::getApplication();
  188. $option = JRequest::getCmd('option');
  189. $view = JRequest::getCmd('view');
  190. $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  191. $limitstart = $mainframe->getUserStateFromRequest($option.$view.'.limitstart', 'limitstart', 0, 'int');
  192. $db = & JFactory::getDBO();
  193. $query = "SELECT * FROM #__k2_extra_fields_groups ORDER BY `name`";
  194. if($filter)
  195. {
  196. $db->setQuery($query);
  197. }
  198. else
  199. {
  200. $db->setQuery($query, $limitstart, $limit);
  201. }
  202. $rows = $db->loadObjectList();
  203. for ($i=0;$i<sizeof($rows);$i++){
  204. $query = "SELECT name FROM #__k2_categories WHERE extraFieldsGroup=".(int)$rows[$i]->id;
  205. $db->setQuery($query);
  206. $categories = $db->loadResultArray();
  207. $rows[$i]->categories = implode(', ', $categories);
  208. }
  209. return $rows;
  210. }
  211. function getTotalGroups() {
  212. $db = & JFactory::getDBO();
  213. $query = "SELECT COUNT(*) FROM #__k2_extra_fields_groups";
  214. $db->setQuery($query);
  215. $total = $db->loadResult();
  216. return $total;
  217. }
  218. function saveGroup(){
  219. $mainframe = &JFactory::getApplication();
  220. $id = JRequest::getInt('id');
  221. $row = & JTable::getInstance('K2ExtraFieldsGroup', 'Table');
  222. if (!$row->bind(JRequest::get('post'))) {
  223. $mainframe->redirect('index.php?option=com_k2&view=extrafieldsgroups', $row->getError(), 'error');
  224. }
  225. if (!$row->check()) {
  226. $mainframe->redirect('index.php?option=com_k2&view=extrafieldsgroup&cid='.$row->id, $row->getError(), 'error');
  227. }
  228. if (!$row->store()) {
  229. $mainframe->redirect('index.php?option=com_k2&view=extrafieldsgroup', $row->getError(), 'error');
  230. }
  231. switch(JRequest::getCmd('task')) {
  232. case 'apply':
  233. $msg = JText::_('K2_CHANGES_TO_GROUP_SAVED');
  234. $link = 'index.php?option=com_k2&view=extrafieldsgroup&cid='.$row->id;
  235. break;
  236. case 'save':
  237. default:
  238. $msg = JText::_('K2_GROUP_SAVED');
  239. $link = 'index.php?option=com_k2&view=extrafieldsgroups';
  240. break;
  241. }
  242. $cache = & JFactory::getCache('com_k2');
  243. $cache->clean();
  244. $mainframe->redirect($link, $msg);
  245. }
  246. function removeGroups(){
  247. $mainframe = &JFactory::getApplication();
  248. $db = & JFactory::getDBO();
  249. $cid = JRequest::getVar('cid');
  250. JArrayHelper::toInteger($cid);
  251. $row = & JTable::getInstance('K2ExtraFieldsGroup', 'Table');
  252. foreach ($cid as $id) {
  253. $row->load($id);
  254. $query = "DELETE FROM #__k2_extra_fields WHERE `group`={$id}";
  255. $db->setQuery($query);
  256. $db->query();
  257. $row->delete($id);
  258. }
  259. $cache = & JFactory::getCache('com_k2');
  260. $cache->clean();
  261. $mainframe->redirect('index.php?option=com_k2&view=extrafieldsgroups', JText::_('K2_DELETE_COMPLETED'));
  262. }
  263. }