PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/iCagenda/admin/models/event.php

https://gitlab.com/Alzakath/icagenda
PHP | 617 lines | 335 code | 87 blank | 195 comment | 61 complexity | 1950903b31d721a04288eabcd5a82158 MD5 | raw file
  1. <?php
  2. /**
  3. *------------------------------------------------------------------------------
  4. * iCagenda v3 by Jooml!C - Events Management Extension for Joomla! 2.5 / 3.x
  5. *------------------------------------------------------------------------------
  6. * @package com_icagenda
  7. * @copyright Copyright (c)2012-2015 Cyril Rezé, Jooml!C - All rights reserved
  8. *
  9. * @license GNU General Public License version 3 or later; see LICENSE.txt
  10. * @author Cyril Rezé (Lyr!C)
  11. * @link http://www.joomlic.com
  12. *
  13. * @version 3.5.6 2015-05-19
  14. * @since 1.0
  15. *------------------------------------------------------------------------------
  16. */
  17. // No direct access to this file
  18. defined('_JEXEC') or die();
  19. jimport('joomla.application.component.modeladmin');
  20. /**
  21. * iCagenda model.
  22. */
  23. class iCagendaModelEvent extends JModelAdmin
  24. {
  25. /**
  26. * @var string The prefix to use with controller messages.
  27. * @since 1.0
  28. */
  29. protected $text_prefix = 'COM_ICAGENDA';
  30. /**
  31. * Method to test whether a record can be deleted.
  32. *
  33. * @param object $record A record object.
  34. *
  35. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  36. *
  37. * @since 3.5.6
  38. */
  39. protected function canDelete($record)
  40. {
  41. if ( ! empty($record->id))
  42. {
  43. if ($record->state != -2)
  44. {
  45. return false;
  46. }
  47. $user = JFactory::getUser();
  48. if ($user->authorise('core.delete'))
  49. {
  50. icagendaCustomfields::deleteData($record->id, 2);
  51. icagendaCustomfields::cleanData(2);
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. /**
  58. * Prepare and sanitise the table prior to saving.
  59. *
  60. * @param JTable $table A JTable object.
  61. *
  62. * @return void
  63. *
  64. * @since 1.0
  65. */
  66. protected function prepareTable( $table )
  67. {
  68. $date = JFactory::getDate();
  69. $user = JFactory::getUser();
  70. $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
  71. if (empty($table->id))
  72. {
  73. // Set the values
  74. $table->created = $date->toSql();
  75. // Set ordering to the last item if not set
  76. if (empty($table->ordering))
  77. {
  78. $db = JFactory::getDbo();
  79. $query = $db->getQuery(true)
  80. ->select('MAX(ordering)')
  81. ->from($db->quoteName('#__icagenda_events'));
  82. $db->setQuery($query);
  83. $max = $db->loadResult();
  84. $table->ordering = $max + 1;
  85. }
  86. }
  87. else
  88. {
  89. // Set the values
  90. $table->modified = $date->toSql();
  91. $table->modified_by = $user->get('id');
  92. }
  93. }
  94. /**
  95. * Returns a Table object, always creating it.
  96. *
  97. * @param string $type The table type to instantiate
  98. * @param string $prefix A prefix for the table class name. Optional.
  99. * @param array $config Configuration array for model. Optional.
  100. *
  101. * @return JTable A database object
  102. *
  103. * @since 1.0
  104. */
  105. public function getTable($type = 'Event', $prefix = 'iCagendaTable', $config = array())
  106. {
  107. return JTable::getInstance($type, $prefix, $config);
  108. }
  109. /**
  110. * Method to get a single record.
  111. *
  112. * @param integer $pk The id of the primary key.
  113. *
  114. * @return mixed Object on success, false on failure.
  115. *
  116. * @since 1.0
  117. */
  118. public function getItem($pk = null)
  119. {
  120. if ($item = parent::getItem($pk))
  121. {
  122. // Do any procesing on fields here if needed
  123. }
  124. return $item;
  125. }
  126. /**
  127. * Method to get the record form.
  128. *
  129. * @param array $data Data for the form.
  130. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  131. *
  132. * @return mixed A JForm object on success, false on failure
  133. *
  134. * @since 1.0
  135. */
  136. public function getForm($data = array(), $loadData = true)
  137. {
  138. // Get the form.
  139. $form = $this->loadForm('com_icagenda.event', 'event',
  140. array('control' => 'jform', 'load_data' => $loadData));
  141. if (empty($form))
  142. {
  143. return false;
  144. }
  145. return $form;
  146. }
  147. /**
  148. * Method to get the data that should be injected in the form.
  149. *
  150. * @return mixed The data for the form.
  151. *
  152. * @since 1.0
  153. */
  154. protected function loadFormData()
  155. {
  156. // Check the session for previously entered form data.
  157. $app = JFactory::getApplication();
  158. $data_array = $app->getUserState('com_icagenda.edit.event.data', array());
  159. if (empty($data_array))
  160. {
  161. $data = $this->getItem();
  162. }
  163. else
  164. {
  165. $data = new JObject;
  166. $data->setProperties($data_array);
  167. }
  168. // If not array, creates array with week days data
  169. if ( ! is_array($data->weekdays))
  170. {
  171. $data->weekdays = explode(',', $data->weekdays);
  172. }
  173. // Retrieves data, to display selected week days
  174. $arrayWeekDays = $data->weekdays;
  175. foreach ($arrayWeekDays as $allTest)
  176. {
  177. if ($allTest == '')
  178. {
  179. $data->weekdays = '0,1,2,3,4,5,6';
  180. }
  181. }
  182. // Set Features
  183. $data->features = $this->getFeatures($data->id);
  184. // Convert features into an array so that the form control can be set
  185. if ( ! isset($data->features))
  186. {
  187. $data->features = array();
  188. }
  189. if ( ! is_array($data->features))
  190. {
  191. $data->features = explode(',', $data->features);
  192. }
  193. return $data;
  194. }
  195. /**
  196. * Method to save the form data.
  197. *
  198. * @param array $data The form data.
  199. *
  200. * @return boolean True on success.
  201. *
  202. * @since 3.4.0
  203. */
  204. public function save($data)
  205. {
  206. $input = JFactory::getApplication()->input;
  207. $date = JFactory::getDate();
  208. $user = JFactory::getUser();
  209. // Fix version before 3.4.0 to set a created date (will use last modified date if exists, or current date)
  210. if (empty($data['created']))
  211. {
  212. $data['created'] = ( ! empty($data['modified'])) ? $data['modified'] : $date->toSql();
  213. }
  214. // Alter the title for save as copy
  215. if ($input->get('task') == 'save2copy')
  216. {
  217. $origTable = clone $this->getTable();
  218. $origTable->load($input->getInt('id'));
  219. if ($data['title'] == $origTable->title)
  220. {
  221. list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
  222. $data['title'] = $title;
  223. $data['alias'] = $alias;
  224. }
  225. else
  226. {
  227. if ($data['alias'] == $origTable->alias)
  228. {
  229. $data['alias'] = '';
  230. }
  231. }
  232. $data['state'] = 0;
  233. }
  234. // Automatic handling of alias for empty fields
  235. if (in_array($input->get('task'), array('apply', 'save', 'save2new')) && (int) $input->get('id') == 0)
  236. {
  237. if ($data['alias'] == null)
  238. {
  239. if (JFactory::getConfig()->get('unicodeslugs') == 1)
  240. {
  241. $data['alias'] = JFilterOutput::stringURLUnicodeSlug($data['title']);
  242. }
  243. else
  244. {
  245. $data['alias'] = JFilterOutput::stringURLSafe($data['title']);
  246. }
  247. $table = JTable::getInstance('Event', 'iCagendaTable');
  248. if ($table->load(array('alias' => $data['alias'], 'catid' => $data['catid'])))
  249. {
  250. $msg = JText::_('COM_ICAGENDA_ALERT_EVENT_SAVE_WARNING');
  251. }
  252. list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']);
  253. $data['alias'] = $alias;
  254. if (isset($msg))
  255. {
  256. JFactory::getApplication()->enqueueMessage($msg, 'warning');
  257. }
  258. }
  259. }
  260. // Generates Alias if empty
  261. if ($data['alias'] == null || empty($data['alias']))
  262. {
  263. $data['alias'] = JFilterOutput::stringURLSafe($data['title']);
  264. if ($data['alias'] == null || empty($data['alias']))
  265. {
  266. if (JFactory::getConfig()->get('unicodeslugs') == 1)
  267. {
  268. $data['alias'] = JFilterOutput::stringURLUnicodeSlug($data['title']);
  269. }
  270. else
  271. {
  272. $data['alias'] = JFilterOutput::stringURLSafe($data['created']);
  273. }
  274. }
  275. }
  276. // Set File Uploaded
  277. if ( ! isset($data['file']))
  278. {
  279. $file = JRequest::getVar('jform', null, 'files', 'array');
  280. $fileUrl = $this->upload($file);
  281. $data['file'] = $fileUrl;
  282. }
  283. // Set Creator infos
  284. $userId = $user->get('id');
  285. $userName = $user->get('name');
  286. if (empty($data['created_by']))
  287. {
  288. $data['created_by'] = (int) $userId;
  289. }
  290. $data['username'] = $userName;
  291. // Set Params
  292. if (isset($data['params']) && is_array($data['params']))
  293. {
  294. // Convert the params field to a string.
  295. $parameter = new JRegistry;
  296. $parameter->loadArray($data['params']);
  297. $data['params'] = (string)$parameter;
  298. }
  299. // Get Event ID from the result back to the Table after saving.
  300. $table = $this->getTable();
  301. if ($table->save($data) === true)
  302. {
  303. $data['id'] = $table->id;
  304. }
  305. else
  306. {
  307. $data['id'] = null;
  308. }
  309. if (parent::save($data))
  310. {
  311. // Save Features to database
  312. $this->maintainFeatures($data);
  313. // Save Custom Fields to database
  314. if (isset($data['custom_fields']) && is_array($data['custom_fields']))
  315. {
  316. icagendaCustomfields::saveToData($data['custom_fields'], $data['id'], 2);
  317. }
  318. return true;
  319. }
  320. return false;
  321. }
  322. /**
  323. * Upload
  324. *
  325. * @since 3.5.3
  326. */
  327. function upload($file)
  328. {
  329. jimport('joomla.filesystem.file');
  330. jimport('joomla.filesystem.folder');
  331. $filename = JFile::makeSafe($file['name']['file']);
  332. // Get media path
  333. $params_media = JComponentHelper::getParams('com_media');
  334. $image_path = $params_media->get('image_path', 'images');
  335. // Paths to thumbs folder
  336. $thumbsPath = $image_path . '/icagenda/thumbs';
  337. if ($filename != '')
  338. {
  339. $src = $file['tmp_name']['file'];
  340. $dest = JPATH_SITE . '/' . $image_path . '/icagenda/files/' . $filename;
  341. if ( ! is_dir($dest))
  342. {
  343. mkdir($intDir, 0755);
  344. }
  345. if (JFile::upload($src, $dest, false))
  346. {
  347. echo 'upload';
  348. return $image_path . '/icagenda/files/' . $filename;
  349. }
  350. return $image_path . '/icagenda/files/' . $filename;
  351. }
  352. }
  353. /**
  354. * Maintain features to data
  355. *
  356. * @since 3.4.0
  357. */
  358. protected function maintainFeatures($data)
  359. {
  360. // Get the list of feature ids to be linked to the event
  361. $features = isset($data['features']) && is_array($data['features']) ? implode(',', $data['features']) : '';
  362. $db = JFactory::getDbo();
  363. // Write any new feature records to the icagenda_feature_xref table
  364. if ( ! empty($features))
  365. {
  366. // Get a list of the valid features already present for this event
  367. $query = $db->getQuery(true);
  368. $query->select('feature_id')
  369. ->from($db->qn('#__icagenda_feature_xref'));
  370. $query->where('event_id = ' . (int) $data['id']);
  371. $query->where('feature_id IN (' . $features . ')');
  372. $db->setQuery($query);
  373. $existing_features = $db->loadColumn(0);
  374. // Identify the insert list
  375. if (empty($existing_features))
  376. {
  377. $new_features = $data['features'];
  378. }
  379. else
  380. {
  381. $new_features = array();
  382. foreach ($data['features'] as $feature)
  383. {
  384. if ( ! in_array($feature, $existing_features))
  385. {
  386. $new_features[] = $feature;
  387. }
  388. }
  389. }
  390. // Write the needed xref records
  391. if ( ! empty($new_features))
  392. {
  393. $xref = new JObject;
  394. $xref->set('event_id', $data['id']);
  395. foreach ($new_features as $feature)
  396. {
  397. $xref->set('feature_id', $feature);
  398. $db->insertObject('#__icagenda_feature_xref', $xref);
  399. $db->setQuery($query);
  400. if ( ! $db->execute())
  401. {
  402. return false;
  403. }
  404. }
  405. }
  406. }
  407. // Delete any unwanted feature records from the icagenda_feature_xref table
  408. $query = $db->getQuery(true);
  409. $query->delete($db->qn('#__icagenda_feature_xref'));
  410. $query->where('event_id = ' . (int) $data['id']);
  411. if ( ! empty($features))
  412. {
  413. // Delete only unwanted features
  414. $query->where('feature_id NOT IN (' . $features . ')');
  415. }
  416. $db->setQuery($query);
  417. $db->execute($query);
  418. if ( ! $db->execute())
  419. {
  420. return false;
  421. }
  422. return true;
  423. }
  424. /**
  425. * Extracts the list of Feature IDs linked to the event and returns an array
  426. *
  427. * @param integer $event_id
  428. *
  429. * @return array/integer Set of Feature IDs
  430. *
  431. * @since 3.5.3
  432. */
  433. protected function getFeatures($event_id)
  434. {
  435. // Write any new feature records to the icagenda_feature_xref table
  436. if (empty($event_id))
  437. {
  438. return '';
  439. }
  440. else
  441. {
  442. $db = JFactory::getDbo();
  443. // Get a comma separated list of the ids of features present for this event
  444. // Note: Direct extraction of a comma separated list is avoided because each db type uses proprietary syntax
  445. $query = $db->getQuery(true);
  446. $query->select('fx.feature_id')
  447. ->from($db->qn('#__icagenda_events', 'e'))
  448. ->innerJoin('#__icagenda_feature_xref AS fx ON e.id=fx.event_id')
  449. ->innerJoin('#__icagenda_feature AS f ON fx.feature_id=f.id AND f.state=1');
  450. $query->where('e.id = ' . (int) $event_id);
  451. $db->setQuery($query);
  452. $features = $db->loadColumn(0);
  453. // Return a comma separated list
  454. return implode(',', $features);
  455. }
  456. }
  457. /**
  458. * Approve Function.
  459. *
  460. * @since 3.2.0
  461. */
  462. function approve($cid, $publish)
  463. {
  464. if (count($cid))
  465. {
  466. JArrayHelper::toInteger($cid);
  467. $cids = implode( ',', $cid );
  468. $query = 'UPDATE #__icagenda_events'
  469. . ' SET approval = '.(int) $publish
  470. . ' WHERE id IN ( '.$cids.' )';
  471. $this->_db->setQuery( $query );
  472. if ( ! $this->_db->query())
  473. {
  474. $this->setError($this->_db->getErrorMsg());
  475. return false;
  476. }
  477. }
  478. return true;
  479. }
  480. /**
  481. * Method to test whether a record can be deleted.
  482. *
  483. * @param object $record A record object.
  484. *
  485. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  486. *
  487. * @since 3.6.0
  488. */
  489. // protected function canDelete($record)
  490. // {
  491. // if ( ! empty($record->id))
  492. // {
  493. // if ($record->state != -2)
  494. // {
  495. // return false;
  496. // }
  497. // $user = JFactory::getUser();
  498. // return $user->authorise('core.delete', 'com_icagenda.event.' . (int) $record->id);
  499. // }
  500. // return false;
  501. // }
  502. /**
  503. * Method to test whether a record can have its state edited.
  504. *
  505. * @param object $record A record object.
  506. *
  507. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  508. *
  509. * @since 3.6.0
  510. */
  511. // protected function canEditState($record)
  512. // {
  513. // $user = JFactory::getUser();
  514. // Check for existing event.
  515. // if (!empty($record->id))
  516. // {
  517. // return $user->authorise('core.edit.state', 'com_icagenda.event.' . (int) $record->id);
  518. // }
  519. // New event, so check against the category.
  520. // elseif (!empty($record->catid))
  521. // {
  522. // return $user->authorise('core.edit.state', 'com_icagenda.event.' . (int) $record->catid);
  523. // }
  524. // Default to component settings if neither event nor category known.
  525. // else
  526. // {
  527. // return parent::canEditState('com_icagenda');
  528. // }
  529. // }
  530. }