PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/model/doctrine/PluginmmMedia.class.php

https://github.com/xavierlacot/mediatorMediaLibraryPlugin
PHP | 474 lines | 382 code | 72 blank | 20 comment | 53 complexity | 428395831b730c32320b6aa77908f22b MD5 | raw file
  1. <?php
  2. /**
  3. * PluginmmMedia
  4. *
  5. * This class has been auto-generated by the Doctrine ORM Framework
  6. *
  7. * @package ##PACKAGE##
  8. * @subpackage ##SUBPACKAGE##
  9. * @author ##NAME## <##EMAIL##>
  10. * @version SVN: $Id: Builder.php 6716 2009-11-12 19:26:28Z jwage $
  11. */
  12. abstract class PluginmmMedia extends BasemmMedia
  13. {
  14. protected $mediatorMedia = null;
  15. protected
  16. $_metadatas_unsaved,
  17. $_metadatas = array();
  18. public function getMediatorMedia()
  19. {
  20. if (!isset($this->mediatorMedia))
  21. {
  22. try
  23. {
  24. $this->mediatorMedia = new mediatorMedia($this->getAbsoluteFilename());
  25. }
  26. catch (Exception $e)
  27. {
  28. //throw new sfException($e->getMessage());
  29. }
  30. }
  31. return $this->mediatorMedia;
  32. }
  33. public function __destruct()
  34. {
  35. if (isset($this->mediatorMedia))
  36. {
  37. $this->mediatorMedia->__destruct();
  38. unset($this->mediatorMedia);
  39. }
  40. if (isset($this->handler))
  41. {
  42. $this->handler->__destruct();
  43. unset($this->handler);
  44. }
  45. }
  46. public function __toString()
  47. {
  48. return $this->getTitle();
  49. }
  50. public function clearMetadatas()
  51. {
  52. if ($this->isNew())
  53. {
  54. $this->_metadatas_unsaved[] = array();
  55. return false;
  56. }
  57. $q = Doctrine_Query::create()
  58. ->delete('mmMediaMetadata m')
  59. ->where('m.mm_media_id = ?', $this->getId());
  60. $q->execute();
  61. }
  62. public function delete(Doctrine_Connection $conn = null)
  63. {
  64. $mediator_media = $this->getMediatorMedia();
  65. if ($mediator_media)
  66. {
  67. $mediator_media->delete();
  68. }
  69. return parent::delete($conn);
  70. }
  71. public function getAbsoluteFilename($mm_media_folder = null)
  72. {
  73. if (is_null($mm_media_folder))
  74. {
  75. $mm_media_folder = $this->getMmMediaFolder();
  76. }
  77. $path = ('' != $mm_media_folder->getAbsolutePath()) ? $mm_media_folder->getAbsolutePath().'/' : '';
  78. $path .= $this->getFilename();
  79. return $path;
  80. }
  81. /**
  82. * returns the raw content of the document
  83. *
  84. * @return string the content of the file
  85. */
  86. public function getContent()
  87. {
  88. $mediator_media = $this->getMediatorMedia();
  89. if ($mediator_media)
  90. {
  91. return $mediator_media->getContent();
  92. }
  93. else
  94. {
  95. return null;
  96. }
  97. }
  98. public function getDisplay($options = array())
  99. {
  100. $display_options = array();
  101. if (isset($options['size']))
  102. {
  103. $display_options['size'] = $options['size'];
  104. unset($options['size']);
  105. }
  106. else
  107. {
  108. $display_options['size'] = 'original';
  109. }
  110. if (!isset($options['alt']))
  111. {
  112. $options['alt'] = $this->getTitle() ? $this->getTitle() : $this->getFilename();
  113. }
  114. if (!isset($options['class']))
  115. {
  116. $options['class'] = 'mediator-media-preview mediator-media-size-'.$display_options['size'];
  117. }
  118. else
  119. {
  120. $options['class'] .= ' mediator-media-preview mediator-media-size-'.$display_options['size'];
  121. }
  122. $display_options['mm_media'] = $this;
  123. $display_options['html_options'] = $options;
  124. $partial = 'mediatorMediaLibrary/media_types/view'.(('' != $this->getType()) ? '_'.$this->getType() : '');
  125. sfContext::getInstance()->getConfiguration()->loadHelpers(array('Partial'));
  126. try
  127. {
  128. $result = get_partial($partial, $display_options);
  129. }
  130. catch (Exception $e)
  131. {
  132. $result = get_partial('mediatorMediaLibrary/media_types/view', $display_options);
  133. }
  134. return $result;
  135. }
  136. public function getMetadata($name)
  137. {
  138. if ($this->isNew())
  139. {
  140. return null;
  141. }
  142. if (!isset($this->_metadatas[$name]))
  143. {
  144. $metadata = Doctrine_Query::create()
  145. ->from('mmMediaMetadata m')
  146. ->where('m.name = ? AND mm_media_id = ?', array($name, $this->id))
  147. ->fetchOne();
  148. if ($metadata)
  149. {
  150. $this->_metadatas[$name] = $metadata;
  151. }
  152. }
  153. else
  154. {
  155. $metadata = $this->_metadatas[$name];
  156. }
  157. return $metadata;
  158. }
  159. public function getMetadatas()
  160. {
  161. $metadatas = Doctrine_Query::create()
  162. ->from('mmMediaMetadata m')
  163. ->where('mm_media_id = ?', array($this->id))
  164. ->execute();
  165. foreach ($metadatas as $metadata)
  166. {
  167. $this->_metadatas[$metadata->getName()] = $metadata;
  168. }
  169. return $this->_metadatas;
  170. }
  171. public function getUrl($options = array())
  172. {
  173. if (!isset($options['size']))
  174. {
  175. $options['size'] = 'original';
  176. }
  177. $size_directory = mediatorMediaLibraryToolkit::getDirectoryForSize($options['size']);
  178. $media_path = $this->getAbsoluteFilename();
  179. if (isset($options['extension']))
  180. {
  181. $media_path .= $options['extension'];
  182. }
  183. if (sfConfig::get('app_mediatorMediaLibraryPlugin_php_serve_media', false))
  184. {
  185. // generate a url to the image path
  186. $result = sfContext::getInstance()->getController()->genUrl(
  187. '@mediatorMediaLibrary_medias?size='.$size_directory.'&path='.$media_path
  188. );
  189. }
  190. else
  191. {
  192. $result = sfConfig::get('app_mediatorMediaLibraryPlugin_media_root', '/media')
  193. .'/'.$size_directory
  194. .'/'.$media_path;
  195. }
  196. $result = str_replace(' ', '%20', $result);
  197. if (isset($options['with_time']) && (true === $options['with_time']))
  198. {
  199. $result .= '?time='.strtotime($this->getUpdatedAt());
  200. }
  201. $pattern = '~^
  202. (%s):// # protocol
  203. (
  204. ([a-z0-9-]+\.)+[a-z]{2,6} # a domain name
  205. | # or
  206. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
  207. )
  208. (:[0-9]+)? # a port (optional)
  209. (/?|/\S+) # a /, nothing or a / with something
  210. $~ix';
  211. if (isset($options['absolute'])
  212. && (true === $options['absolute'])
  213. && !preg_match($pattern, $result))
  214. {
  215. // prepend with the current domain name
  216. $result = sfContext::getInstance()->getRequest()->getUriPrefix().'/'.$result;
  217. }
  218. return $result;
  219. }
  220. protected function log($message)
  221. {
  222. try
  223. {
  224. sfContext::getInstance()->getLogger()->log('{mmMedia}'.$message);
  225. }
  226. catch (Exception $e)
  227. {
  228. // nothing
  229. }
  230. }
  231. public function hasMetadata($name)
  232. {
  233. return (null !== $this->getMetadata($name));
  234. }
  235. public function moveTo($array = array())
  236. {
  237. if (!isset($array['mm_media_folder']) && !isset($array['filename']))
  238. {
  239. throw new sfException('Missing informations in order to move the file.');
  240. }
  241. if (isset($array['mm_media_folder']))
  242. {
  243. $mm_media_folder = $array['mm_media_folder'];
  244. }
  245. else
  246. {
  247. $mm_media_folder = $this->getMmMediaFolder();
  248. }
  249. $mm_media_folder_path = ('' != $mm_media_folder->getAbsolutePath()) ? $mm_media_folder->getAbsolutePath().DIRECTORY_SEPARATOR : '';
  250. if (isset($array['filename']))
  251. {
  252. $this->getMediatorMedia()->moveTo($mm_media_folder_path, $array['filename']);
  253. $this->setMmMediaFolder($mm_media_folder);
  254. $this->setFilename($array['filename']);
  255. $this->setTitle($array['filename']);
  256. }
  257. else
  258. {
  259. $this->getMediatorMedia()->moveTo($mm_media_folder_path);
  260. $this->setMmMediaFolder($mm_media_folder);
  261. }
  262. $this->save();
  263. }
  264. public function save(Doctrine_Connection $conn = null)
  265. {
  266. if ($this->isNew())
  267. {
  268. $this->setUuid(uniqid('', true));
  269. }
  270. parent::save($conn);
  271. if (isset($this->_metadatas_unsaved))
  272. {
  273. $this->setMetadatas($this->_metadatas_unsaved);
  274. }
  275. }
  276. public function setMetadata($name, $value)
  277. {
  278. if ($this->isNew())
  279. {
  280. $this->_metadatas_unsaved[$name] = $value;
  281. return false;
  282. }
  283. $metadata = $this->getMetadata($name);
  284. if (false === $metadata)
  285. {
  286. $metadata = new mmMediaMetadata();
  287. $metadata->setName($name);
  288. $metadata->setmmMediaId($this->getPrimaryKey());
  289. }
  290. $metadata->setValue(''.((string)$value));
  291. $result = $metadata->save();
  292. $this->_metadatas[$name] = $metadata;
  293. return $result;
  294. }
  295. public function setMetadatas($metadatas)
  296. {
  297. if (is_array($metadatas))
  298. {
  299. foreach ($metadatas as $name => $value)
  300. {
  301. $this->setMetadata($name, $value);
  302. }
  303. }
  304. }
  305. public function setSource($source)
  306. {
  307. $filename = !is_null($this->getFilename()) ? $this->getFilename() : basename($source);
  308. $this->update(array(
  309. 'source' => $source,
  310. 'mm_media_folder' => $this->getMmMediaFolder(),
  311. 'filename' => $filename
  312. ));
  313. }
  314. public function update($array = array())
  315. {
  316. if (is_null($this->getFilename())
  317. && ((!isset($array['source'])
  318. || !isset($array['mm_media_folder'])
  319. || !isset($array['filename']))
  320. && (!isset($array['filesystem_existing'])
  321. || (isset($array['filesystem_existing']) && (true !== $array['filesystem_existing'])))
  322. )
  323. )
  324. {
  325. throw new sfException('Missing informations in order to update the file.');
  326. }
  327. $mm_media_folder = $array['mm_media_folder'];
  328. unset($array['mm_media_folder']);
  329. if (!is_null($mm_media_folder) && is_object($mm_media_folder))
  330. {
  331. if ($mm_media_folder->isNew())
  332. {
  333. $mm_media_folder->save();
  334. }
  335. $absolute_path = $mm_media_folder->getAbsolutePath();
  336. $mm_media_folder_path = ('' != $absolute_path) ? $absolute_path.DIRECTORY_SEPARATOR : '';
  337. $this->setMmMediaFolderId($mm_media_folder->getPrimaryKey());
  338. }
  339. else
  340. {
  341. $mm_media_folder_path = $this->getmmMediaFolder()->getAbsolutePath();
  342. }
  343. if (isset($array['filename']))
  344. {
  345. $filename = mediatorMediaLibraryInflector::cleanFilename($array['filename']);
  346. unset($array['filename']);
  347. $this->log(sprintf('The media will be created or updated in "%s"', $mm_media_folder_path.$filename));
  348. $this->mediatorMedia = new mediatorMedia($mm_media_folder_path.$filename);
  349. if (isset($array['source']))
  350. {
  351. $source = $array['source'];
  352. unset($array['source']);
  353. if (!isset($array['filesystem_existing']) || (true !== $array['filesystem_existing']))
  354. {
  355. $this->log(sprintf('Writing the media from "%s"', $source));
  356. $this->mediatorMedia->write($source);
  357. }
  358. }
  359. $this->setMimeType($this->mediatorMedia->getMimeType());
  360. $type = $this->mediatorMedia->getType();
  361. $this->setType($type);
  362. $this->setMd5sum(md5_file($this->mediatorMedia->cache()));
  363. $this->setFilesize(filesize($this->mediatorMedia->cache()));
  364. // save the media metadatas
  365. $metadatas = $this->mediatorMedia->getMetadatas();
  366. if (isset($metadatas['width']))
  367. {
  368. $this->setWidth($metadatas['width']);
  369. unset($metadatas['width']);
  370. }
  371. if (isset($metadatas['height']))
  372. {
  373. $this->setHeight($metadatas['height']);
  374. unset($metadatas['height']);
  375. }
  376. $this->setMetadatas($metadatas);
  377. if (false === sfConfig::get('app_mediatorMediaLibraryPlugin_asynchronous', false))
  378. {
  379. $thumbnail_name = $this->mediatorMedia->process();
  380. if (false !== $thumbnail_name)
  381. {
  382. $this->setThumbnailFilename($thumbnail_name);
  383. }
  384. }
  385. $this->setFilename($filename);
  386. $this->setTitle($filename);
  387. }
  388. foreach ($array as $key => $value)
  389. {
  390. $fieldNames = Doctrine::getTable('mmMedia')->getColumnNames();
  391. if (in_array($key, $fieldNames))
  392. {
  393. $this->$key = $value;
  394. }
  395. else
  396. {
  397. $this->setMetadata($key, $value);
  398. }
  399. }
  400. $this->save();
  401. }
  402. }