PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_hwdmediashare/models/file.php

https://gitlab.com/ppapadatis/Videolearn
PHP | 302 lines | 176 code | 40 blank | 86 comment | 17 complexity | 3a6756037b60a864d97eaefdf302c4b7 MD5 | raw file
  1. <?php
  2. /**
  3. * @version SVN $Id: file.php 751 2012-10-31 17:13:13Z dhorsfall $
  4. * @package hwdMediaShare
  5. * @copyright Copyright (C) 2012 Highwood Design Limited. All rights reserved.
  6. * @license GNU General Public License http://www.gnu.org/copyleft/gpl.html
  7. * @author Dave Horsfall
  8. * @since 16-Mar-2012 18:17:41
  9. */
  10. // No direct access to this file
  11. defined('_JEXEC') or die('Restricted access');
  12. // import Joomla modelform library
  13. jimport('joomla.application.component.modeladmin');
  14. /**
  15. * hwdMediaShare Model
  16. */
  17. class hwdMediaShareModelFile extends JModelAdmin
  18. {
  19. /**
  20. * Method to get a single record.
  21. *
  22. * @param integer The id of the primary key.
  23. *
  24. * @return mixed Object on success, false on failure.
  25. */
  26. public function getItem($pk = null)
  27. {
  28. if ($item = parent::getItem($pk))
  29. {
  30. }
  31. return $item;
  32. }
  33. /**
  34. * Returns a reference to the a Table object, always creating it.
  35. *
  36. * @param type The table type to instantiate
  37. * @param string A prefix for the table class name. Optional.
  38. * @param array Configuration array for model. Optional.
  39. * @return JTable A database object
  40. * @since 0.1
  41. */
  42. public function getTable($type = 'File', $prefix = 'hwdMediaShareTable', $config = array())
  43. {
  44. return JTable::getInstance($type, $prefix, $config);
  45. }
  46. /**
  47. * Method to get the record form.
  48. *
  49. * @param array $data Data for the form.
  50. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  51. * @return mixed A JForm object on success, false on failure
  52. * @since 0.1
  53. */
  54. public function getForm($data = array(), $loadData = true)
  55. {
  56. // Get the form.
  57. $form = $this->loadForm('com_hwdmediashare.file', 'file', array('control' => 'jform', 'load_data' => $loadData));
  58. if (empty($form))
  59. {
  60. return false;
  61. }
  62. return $form;
  63. }
  64. /**
  65. * Method to get the script that have to be included on the form
  66. *
  67. * @return string Script files
  68. */
  69. public function getScript()
  70. {
  71. return 'administrator/components/com_hwdmediashare/models/forms/file.js';
  72. }
  73. /**
  74. * Method to get the data that should be injected in the form.
  75. *
  76. * @return mixed The data for the form.
  77. * @since 0.1
  78. */
  79. protected function loadFormData()
  80. {
  81. // Check the session for previously entered form data.
  82. $data = JFactory::getApplication()->getUserState('com_hwdmediashare.edit.file.data', array());
  83. if (empty($data))
  84. {
  85. $data = $this->getItem();
  86. }
  87. return $data;
  88. }
  89. /**
  90. * Method to save the form data.
  91. *
  92. * @param array The form data.
  93. *
  94. * @return boolean True on success.
  95. * @since 0.1
  96. */
  97. public function save($data)
  98. {
  99. $app = JFactory::getApplication();
  100. $date =& JFactory::getDate();
  101. $user = JFactory::getUser();
  102. $isNew = false;
  103. $hwdms = hwdMediaShareFactory::getInstance();
  104. $config = $hwdms->getConfig();
  105. empty($data['id']) ? $data['key'] = hwdMediaShareFactory::generateKey() : null;
  106. // Alter the title for save as copy
  107. $data['modified'] = $date->format('Y-m-d H:i:s');
  108. $data['modified_user_id'] = $user->id;
  109. empty($data['created_user_id']) ? $data['created_user_id'] = $user->id : null;
  110. empty($data['created']) ? $data['created'] = $date->format('Y-m-d H:i:s') : null;
  111. empty($data['publish_up']) ? $data['publish_up'] = $date->format('Y-m-d H:i:s') : null;
  112. empty($data['publish_down']) ? $data['publish_down'] = "0000-00-00 00:00:00" : null;
  113. empty($data['alias']) ? $data['alias'] = JFilterOutput::stringURLSafe($_REQUEST['jform']['title']) : $data['alias'] = JFilterOutput::stringURLSafe($data['alias']);
  114. if (!$app->isAdmin() && $config->get('approve_new_albums') == 1)
  115. {
  116. $data['status'] = 2;
  117. }
  118. else
  119. {
  120. $data['status'] = 1;
  121. }
  122. $form = parent::save($data);
  123. if ($form)
  124. {
  125. if (empty($data['id']))
  126. {
  127. $isNew = true;
  128. }
  129. // Set data to current database object
  130. !$app->isAdmin() ? $data['id'] = $this->getState('playlistform.id') : $data['id'] = $this->getState('playlist.id');
  131. $params = new StdClass;
  132. $params->elementType = 4;
  133. $params->elementId = $data['id'];
  134. $params->tags = $data['tags'];
  135. $params->key = $data['key'];
  136. $params->remove = (isset($data['remove_thumbnail']) ? true : false);
  137. hwdMediaShareFactory::load('tags');
  138. hwdMediaShareTags::save($params);
  139. hwdMediaShareFactory::load('customfields');
  140. hwdMediaShareCustomFields::save($params);
  141. hwdMediaShareFactory::load('upload');
  142. hwdMediaShareUpload::processThumbnail($params);
  143. if ($isNew)
  144. {
  145. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_hwdmediashare/tables');
  146. $table =& JTable::getInstance('Playlist', 'hwdMediaShareTable');
  147. $table->load( $data['id'] );
  148. $properties = $table->getProperties(1);
  149. $row = JArrayHelper::toObject($properties, 'JObject');
  150. hwdMediaShareFactory::load('events');
  151. $events = hwdMediaShareEvents::getInstance();
  152. $events->triggerEvent('onAfterPlaylistAdd', $row);
  153. }
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Method to assign user to a single record
  160. *
  161. * @param integer The id of the primary key.
  162. *
  163. * @return mixed Object on success, false on failure.
  164. */
  165. public function batch($pks, $value = array())
  166. {
  167. // Initialise variables.
  168. $user = JFactory::getUser();
  169. $table = $this->getTable();
  170. $pks = (array) $pks;
  171. if (!isset($value['user']) || !isset($value['access']) || !isset($value['language']))
  172. {
  173. $this->setError(JText::_('JGLOBAL_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
  174. return false;
  175. }
  176. // Access checks.
  177. foreach ($pks as $i => $id)
  178. {
  179. $data = array();
  180. $data['id'] = $id;
  181. !empty($value['user']) ? $data['created_user_id'] = $value['user'] : null;
  182. !empty($value['access']) ? $data['access'] = $value['access'] : null;
  183. !empty($value['language']) ? $data['language'] = $value['language'] : null;
  184. if (!parent::save($data))
  185. {
  186. $this->setError(JText::_('COM_HWDMS_SAVE_FAILED'));
  187. return false;
  188. }
  189. }
  190. // Clear the component's cache
  191. $this->cleanCache();
  192. return true;
  193. }
  194. /**
  195. * Method to assign user to a single record
  196. *
  197. * @param integer The id of the primary key.
  198. *
  199. * @return mixed Object on success, false on failure.
  200. */
  201. public function delete(&$pks)
  202. {
  203. // Delete test file is already exists
  204. jimport( 'joomla.filesystem.file' );
  205. // Iterate the items to delete each one.
  206. foreach ($pks as $i => $pk)
  207. {
  208. $table =& JTable::getInstance('File', 'hwdMediaShareTable');
  209. $table->load( $pk );
  210. $properties = $table->getProperties(1);
  211. $file = JArrayHelper::toObject($properties, 'JObject');
  212. switch ($file->element_type)
  213. {
  214. case 1:
  215. // Media
  216. $table =& JTable::getInstance('Media', 'hwdMediaShareTable');
  217. break;
  218. case 2:
  219. // Album
  220. $table =& JTable::getInstance('Album', 'hwdMediaShareTable');
  221. break;
  222. case 3:
  223. // Group
  224. $table =& JTable::getInstance('Group', 'hwdMediaShareTable');
  225. break;
  226. case 4:
  227. // Playlist
  228. $table =& JTable::getInstance('Playlist', 'hwdMediaShareTable');
  229. break;
  230. case 5:
  231. // Channel
  232. $table =& JTable::getInstance('UserChannel', 'hwdMediaShareTable');
  233. break;
  234. case 6:
  235. // Category
  236. $table =& JTable::getInstance('Category', 'hwdMediaShareTable');
  237. break;
  238. }
  239. if (!is_object($table)) {
  240. continue;
  241. }
  242. $table->load( $file->element_id );
  243. $properties = $table->getProperties(1);
  244. $item = JArrayHelper::toObject($properties, 'JObject');
  245. hwdMediaShareFactory::load('files');
  246. hwdMediaShareFiles::getLocalStoragePath();
  247. $foldersSource = hwdMediaShareFiles::getFolders($item->key);
  248. $filenameSource = hwdMediaShareFiles::getFilename($item->key, $file->file_type);
  249. $extSource = hwdMediaShareFiles::getExtension($item, $file->file_type);
  250. $pathSource = hwdMediaShareFiles::getPath($foldersSource, $filenameSource, $extSource);
  251. jimport( 'joomla.filesystem.file' );
  252. if (JFile::exists($pathSource)) JFile::delete($pathSource);
  253. }
  254. if (!parent::delete($pks))
  255. {
  256. return false;
  257. }
  258. // Clear the component's cache
  259. $this->cleanCache();
  260. return true;
  261. }
  262. }