PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_fpss/models/slides.php

https://gitlab.com/endomorphosis/OLAAaction
PHP | 410 lines | 377 code | 24 blank | 9 comment | 71 complexity | 0d60830cbd0e30c3858f15bba8f6621d MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: slides.php 763 2012-01-04 15:07:52Z joomlaworks $
  4. * @package Frontpage Slideshow
  5. * @author JoomlaWorks http://www.joomlaworks.gr
  6. * @copyright Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
  7. * @license Commercial - This code cannot be redistributed without permission from JoomlaWorks Ltd.
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die('Restricted access');
  11. class FPSSModelSlides extends JModel {
  12. function getData() {
  13. $db = &$this->getDBO();
  14. $query = "SELECT slide.*, `group`.name AS groupname, category.name AS categoryName, category.params AS categoryParams, author.name AS authorName, moderator.name AS moderatorName
  15. FROM #__fpss_slides AS slide
  16. JOIN #__fpss_categories AS category ON slide.catid = category.id
  17. JOIN #__groups AS `group` ON `group`.id = slide.access
  18. LEFT JOIN #__users AS author ON author.id = slide.created_by
  19. LEFT JOIN #__users AS moderator ON moderator.id = slide.modified_by";
  20. $conditions = array();
  21. if ($this->getState('published')!=-1) {
  22. $conditions[]= "slide.published = ".(int)$this->getState('published');
  23. }
  24. if ($this->getState('catid')) {
  25. $catid = $this->getState('catid');
  26. if(is_array($catid)) {
  27. JArrayHelper::toInteger($catid);
  28. $conditions[]= "slide.catid IN (".implode(',', $catid).")";
  29. }
  30. else {
  31. $conditions[]= "slide.catid = ".(int)$catid;
  32. }
  33. }
  34. if ($this->getState('access')!=-1) {
  35. $access = $this->getState('access');
  36. if(is_array($access)) {
  37. JArrayHelper::toInteger($access);
  38. $conditions[]= "slide.access IN (".implode(',', $access).")";
  39. }
  40. else {
  41. $conditions[]= "slide.access <= ".(int)$access;
  42. }
  43. }
  44. if ($this->getState('author')) {
  45. $conditions[]= "slide.created_by = ".(int)$this->getState('author');
  46. }
  47. if ($this->getState('featured')!=-1) {
  48. $conditions[]= "slide.featured = ".(int)$this->getState('featured');
  49. }
  50. if ($this->getState('publish_up')) {
  51. $conditions[]= "(slide.publish_up = ".$db->Quote($db->getNullDate())." OR slide.publish_up <= ".$db->Quote($this->getState('publish_up')).")";
  52. }
  53. if ($this->getState('publish_down')) {
  54. $conditions[]= "(slide.publish_down = ".$db->Quote($db->getNullDate())." OR slide.publish_down >= ".$db->Quote($this->getState('publish_down')).")";
  55. }
  56. if ($this->getState('language') && version_compare( JVERSION, '1.6.0', 'ge' )) {
  57. $conditions[]= "slide.language IN (".$db->Quote($this->getState('language')).",".$db->Quote('*').")";
  58. }
  59. if ($this->getState('search')) {
  60. $conditions[]= "LOWER(slide.title) LIKE ".$db->Quote('%'.$db->getEscaped($this->getState('search'), true).'%', false);
  61. }
  62. if ($this->getState('categoryPublished')!=-1) {
  63. $conditions[]= "category.published = ".(int)$this->getState('published');
  64. }
  65. if (count($conditions)) {
  66. $query.= " WHERE ".implode(' AND ', $conditions);
  67. }
  68. if(version_compare( JVERSION, '1.6.0', 'ge' )) {
  69. $query = JString::str_ireplace('#__groups', '#__viewlevels', $query);
  70. $query = JString::str_ireplace('`group`.name', '`group`.title', $query);
  71. }
  72. $query .= " ORDER BY ".$this->getState('ordering')." ".$this->getState('orderingDir');
  73. $db->setQuery($query, $this->getState('limitstart'), $this->getState('limit'));
  74. $rows = $db->loadObjectList();
  75. return $rows;
  76. }
  77. function getTotal() {
  78. $db = &$this->getDBO();
  79. $query = "SELECT COUNT(*) FROM #__fpss_slides";
  80. $conditions = array();
  81. if ($this->getState('published')!=-1) {
  82. $conditions[]= "published = ".(int)$this->getState('published');
  83. }
  84. if ($this->getState('catid')) {
  85. $catid = $this->getState('catid');
  86. if(is_array($catid)) {
  87. JArrayHelper::toInteger($catid);
  88. $conditions[]= "catid IN (".implode(',', $catid).")";
  89. }
  90. else {
  91. $conditions[]= "catid = ".(int)$catid;
  92. }
  93. }
  94. if ($this->getState('author')) {
  95. $conditions[]= "created_by = ".(int)$this->getState('author');
  96. }
  97. if ($this->getState('featured')!=-1) {
  98. $conditions[]= "featured <= ".(int)$this->getState('featured');
  99. }
  100. if ($this->getState('language') && version_compare( JVERSION, '1.6.0', 'ge' )) {
  101. $conditions[]= "language IN (".$db->Quote($this->getState('language')).",".$db->Quote('*').")";
  102. }
  103. if ($this->getState('search')) {
  104. $conditions[]= "LOWER(title) LIKE ".$db->Quote('%'.$db->getEscaped($this->getState('search'), true).'%', false);
  105. }
  106. if (count($conditions)) {
  107. $query.= " WHERE ".implode(' AND ', $conditions);
  108. }
  109. $db->setQuery($query);
  110. $total = $db->loadResult();
  111. return $total;
  112. }
  113. function publish() {
  114. $row = & JTable::getInstance('slide', 'FPSS');
  115. $row->publish($this->getState('id'), 1);
  116. }
  117. function unpublish() {
  118. $row = & JTable::getInstance('slide', 'FPSS');
  119. $row->publish($this->getState('id'), 0);
  120. }
  121. function saveorder() {
  122. $id = $this->getState('id');
  123. $order = $this->getState('order');
  124. $total = count($id);
  125. JArrayHelper::toInteger($order, array(0));
  126. $row = &JTable::getInstance('slide', 'FPSS');
  127. for ($i = 0; $i < $total; $i++) {
  128. $row->load((int) $id[$i]);
  129. if ($row->ordering != $order[$i]) {
  130. $row->ordering = $order[$i];
  131. $row->store();
  132. }
  133. }
  134. }
  135. function featuredsaveorder() {
  136. $id = $this->getState('id');
  137. $featuredOrder = $this->getState('featuredOrder');
  138. $total = count($id);
  139. JArrayHelper::toInteger($featuredOrder, array(0));
  140. $row = &JTable::getInstance('slide', 'FPSS');
  141. for ($i = 0; $i < $total; $i++) {
  142. $row->load((int) $id[$i]);
  143. if ($row->featured_ordering != $featuredOrder[$i]) {
  144. $row->featured_ordering = $featuredOrder[$i];
  145. $row->store();
  146. }
  147. }
  148. }
  149. function featured() {
  150. $ids = $this->getState('id');
  151. $row = &JTable::getInstance('slide', 'FPSS');
  152. foreach ($ids as $id) {
  153. $row->load($id);
  154. if ($row->featured == 1){
  155. $row->featured = 0;
  156. }
  157. else {
  158. $row->featured = 1;
  159. if($row->featured_ordering == 0){
  160. $row->featured_ordering = $row->getNextOrder('featured=1', 'featured_ordering');
  161. }
  162. }
  163. $row->store();
  164. }
  165. }
  166. function accessregistered() {
  167. $row = &JTable::getInstance('slide', 'FPSS');
  168. $id = $this->getState('id');
  169. $row->load($id[0]);
  170. $row->access = 1;
  171. $row->check();
  172. $row->store();
  173. }
  174. function accessspecial() {
  175. $row = &JTable::getInstance('slide', 'FPSS');
  176. $id = $this->getState('id');
  177. $row->load($id[0]);
  178. $row->access = 2;
  179. $row->check();
  180. $row->store();
  181. }
  182. function accesspublic() {
  183. $row = &JTable::getInstance('slide', 'FPSS');
  184. $id = $this->getState('id');
  185. $row->load($id[0]);
  186. $row->access = 0;
  187. $row->check();
  188. $row->store();
  189. }
  190. function remove() {
  191. $row = & JTable::getInstance('slide', 'FPSS');
  192. $row->delete($this->getState('id'));
  193. }
  194. function cleanUp(){
  195. if(!$this->getState('id')) {
  196. return;
  197. }
  198. if(!is_array($this->getState('id'))) {
  199. $IDs = (array)$this->getState('id');
  200. }
  201. else {
  202. $IDs = $this->getState('id');
  203. JArrayHelper::toInteger($IDs);
  204. }
  205. $IDs = array_filter($IDs);
  206. $IDs = array_unique($IDs);
  207. $savepath = JPATH_SITE.DS.'media'.DS.'com_fpss';
  208. foreach($IDs as $id) {
  209. $filename = $id.'_'.md5('Image'.$id);
  210. if(JFile::exists($savepath.DS.'src'.DS.$filename.'_s.png')) {
  211. JFile::delete($savepath.DS.'src'.DS.$filename.'_s.png');
  212. }
  213. if(JFile::exists($savepath.DS.'src'.DS.$filename.'_s.jpg')) {
  214. JFile::delete($savepath.DS.'src'.DS.$filename.'_s.jpg');
  215. }
  216. if(JFile::exists($savepath.DS.'cache'.DS.$filename.'_m.jpg')) {
  217. JFile::delete($savepath.DS.'cache'.DS.$filename.'_m.jpg');
  218. }
  219. if(JFile::exists($savepath.DS.'cache'.DS.$filename.'_p.jpg')) {
  220. JFile::delete($savepath.DS.'cache'.DS.$filename.'_p.jpg');
  221. }
  222. if(JFile::exists($savepath.DS.'cache'.DS.$filename.'_t.jpg')) {
  223. JFile::delete($savepath.DS.'cache'.DS.$filename.'_t.jpg');
  224. }
  225. }
  226. return;
  227. }
  228. function stats(){
  229. $db = &$this->getDBO();
  230. $date = &JFactory::getDate();
  231. $now = $date->toMySQL();
  232. $query = "SELECT slides.title, slides.id, COUNT(stats.slideID) AS hits FROM #__fpss_slides AS slides
  233. LEFT JOIN #__fpss_stats AS stats ON stats.slideID = slides.id";
  234. $conditions = array();
  235. if($this->getState('catid')) {
  236. $conditions[] = "slides.catid = ".(int)$this->getState('catid');
  237. }
  238. if($this->getState('timeRange')) {
  239. $conditions[] = "stats.time > DATE_SUB(".$db->Quote($now).",INTERVAL ".(int)$this->getState('timeRange')." DAY)";
  240. }
  241. if (count($conditions)) {
  242. $query.= " WHERE ".implode(' AND ', $conditions);
  243. }
  244. $query.= " GROUP BY stats.slideID";
  245. $db->setQuery($query, 0, $this->getState('limit'));
  246. $rows = $db->loadObjectList();
  247. $data = array();
  248. $chartCategories = array();
  249. foreach($rows as $row){
  250. $chartCategories[] = $row->title;
  251. $data[]=(int)$row->hits;
  252. }
  253. $chart = new JObject();
  254. $chart->set('data', $data);
  255. $chart->set('categories', $chartCategories);
  256. return $chart;
  257. }
  258. function import() {
  259. if(!JFolder::exists(JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.'src')) {
  260. JFolder::create(JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.'src');
  261. }
  262. if(!JFolder::exists(JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.'cache')) {
  263. JFolder::create(JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.'cache');
  264. }
  265. $manifest = JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.'samples'.DS.'data.xml';
  266. $categoryModel = &JModel::getInstance('category', 'FPSSModel');
  267. $slideModel = &JModel::getInstance('slide', 'FPSSModel');
  268. if(version_compare( JVERSION, '1.6.0', 'ge' )) {
  269. $xml = new JXMLElement(JFile::read(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fpss'.DS.'models'.DS.'category.xml'));
  270. $categoryParams = new JParameter('');
  271. foreach ($xml->params as $paramGroup) {
  272. foreach ($paramGroup->param as $param) {
  273. if ($param->getAttribute('type') != 'spacer') {
  274. $categoryParams->set($param->getAttribute('name'), $param->getAttribute('default'));
  275. }
  276. }
  277. }
  278. $categoryParams = $categoryParams->toString();
  279. $xml = new JXMLElement(JFile::read(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fpss'.DS.'models'.DS.'slide.xml'));
  280. $slideParams = new JParameter('');
  281. foreach ($xml->params as $paramGroup) {
  282. foreach ($paramGroup->param as $param) {
  283. if ($param->getAttribute('type') != 'spacer') {
  284. $slideParams->set($param->getAttribute('name'), $param->getAttribute('default'));
  285. }
  286. }
  287. }
  288. $slideParams = $slideParams->toString();
  289. $xml = new JXMLElement(JFile::read($manifest));
  290. }
  291. else {
  292. $xml = new JSimpleXML;
  293. $xml->loadFile(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fpss'.DS.'models'.DS.'category.xml');
  294. $categoryParams = new JParameter('');
  295. foreach ($xml->document->params as $paramGroup) {
  296. foreach ($paramGroup->param as $param) {
  297. if ($param->attributes('type') != 'spacer') {
  298. $categoryParams->set($param->attributes('name'), $param->attributes('default'));
  299. }
  300. }
  301. }
  302. // Enforce new sample data for text overriding the live parameter
  303. $categoryParams->set('liveData', '0');
  304. $categoryParams = $categoryParams->toString();
  305. $xml = new JSimpleXML;
  306. $xml->loadFile(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_fpss'.DS.'models'.DS.'slide.xml');
  307. $slideParams = new JParameter('');
  308. foreach ($xml->document->params as $paramGroup) {
  309. foreach ($paramGroup->param as $param) {
  310. if ($param->attributes('type') != 'spacer') {
  311. $slideParams->set($param->attributes('name'), $param->attributes('default'));
  312. }
  313. }
  314. }
  315. $slideParams = $slideParams->toString();
  316. $xml = new JSimpleXML();
  317. $xml->loadFile($manifest);
  318. $xml = $xml->document->category;
  319. }
  320. foreach($xml as $category) {
  321. $attributes = $category->attributes();
  322. if(version_compare( JVERSION, '1.6.0', 'ge' )) {
  323. $data = array();
  324. foreach($attributes as $key=>$value) {
  325. $data[$key] = $value->data();
  326. }
  327. $data['language'] = '*';
  328. }
  329. else {
  330. $data = $attributes;
  331. }
  332. $data['params'] = $categoryParams;
  333. $categoryModel->setState('data', $data);
  334. $categoryModel->save();
  335. $slides = $category->children();
  336. foreach($slides as $slide) {
  337. $attributes = $slide->attributes();
  338. if(version_compare( JVERSION, '1.6.0', 'ge' )) {
  339. $data = array();
  340. foreach($attributes as $key=>$value) {
  341. $data[$key] = $value->data();
  342. }
  343. $data['language'] = '*';
  344. }
  345. else {
  346. $data = $attributes;
  347. }
  348. $images = array();
  349. foreach($slide->children() as $child) {
  350. if($child->name() != 'image') {
  351. $data[$child->name()] = $child->data();
  352. }
  353. else {
  354. $images[] = $child;
  355. }
  356. }
  357. $data['params'] = $slideParams;
  358. $data['dummy'] = false;
  359. $data['thumb'] = true;
  360. $data['catid'] = $categoryModel->getState('id');
  361. $slideModel->setState('data', $data);
  362. $slideModel->save();
  363. foreach($images as $image) {
  364. $id = $slideModel->getState('id');
  365. $attributes = $image->attributes();
  366. if(version_compare( JVERSION, '1.6.0', 'ge' )) {
  367. $data = array();
  368. foreach($attributes as $key=>$value) {
  369. $data[$key] = $value->data();
  370. }
  371. }
  372. else {
  373. $data = $attributes;
  374. }
  375. $src = JPATH_SITE.JPath::clean($data['src']);
  376. $folder = ($data['type'] == 'original')? 'src' : 'cache';
  377. $file = $id.'_'.md5('Image'.$id);
  378. $file .= '_'.JString::substr($data['type'], 0, 1);
  379. $file .= '.'.JFile::getExt($src);
  380. $destination = JPATH_SITE.DS.'media'.DS.'com_fpss'.DS.$folder.DS.$file;
  381. JFile::copy($src, $destination);
  382. }
  383. }
  384. }
  385. }
  386. }