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

/kernel/classes/datatypes/ezmedia/ezmedia.php

https://github.com/eeggenberger/ezpublish
PHP | 274 lines | 231 code | 22 blank | 21 comment | 10 complexity | b89020de57886fbd0704aab71a613e6c MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZBinaryFile class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZMedia ezmedia.php
  12. \ingroup eZDatatype
  13. \brief The class eZMedia handles registered media files
  14. */
  15. class eZMedia extends eZPersistentObject
  16. {
  17. function eZMedia( $row )
  18. {
  19. $this->eZPersistentObject( $row );
  20. }
  21. static function definition()
  22. {
  23. static $definition = array( "fields" => array( "contentobject_attribute_id" => array( 'name' => "ContentObjectAttributeID",
  24. 'datatype' => 'integer',
  25. 'default' => 0,
  26. 'required' => true,
  27. 'foreign_class' => 'eZContentObjectAttribute',
  28. 'foreign_attribute' => 'id',
  29. 'multiplicity' => '1..*' ),
  30. "version" => array( 'name' => "Version",
  31. 'datatype' => 'integer',
  32. 'default' => 0,
  33. 'required' => true ),
  34. "filename" => array( 'name' => "Filename",
  35. 'datatype' => 'string',
  36. 'default' => '',
  37. 'required' => true ),
  38. "original_filename" => array( 'name' => "OriginalFilename",
  39. 'datatype' => 'string',
  40. 'default' => '',
  41. 'required' => true ),
  42. "mime_type" => array( 'name' => "MimeType",
  43. 'datatype' => 'string',
  44. 'default' => '',
  45. 'required' => true ),
  46. "width" => array( 'name' => "Width",
  47. 'datatype' => 'integer',
  48. 'default' => 0,
  49. 'required' => true ),
  50. "height" => array( 'name' => "Height",
  51. 'datatype' => 'integer',
  52. 'default' => 0,
  53. 'required' => true ),
  54. "has_controller" => array( 'name' => "HasController",
  55. 'datatype' => 'integer',
  56. 'default' => 0,
  57. 'required' => true ),
  58. "controls" => array( 'name' => "Controls",
  59. 'datatype' => 'string',
  60. 'default' => '',
  61. 'required' => true ),
  62. "is_autoplay" => array( 'name' => "IsAutoplay",
  63. 'datatype' => 'integer',
  64. 'default' => 0,
  65. 'required' => true ),
  66. "pluginspage" => array( 'name' => "Pluginspage",
  67. 'datatype' => 'string',
  68. 'default' => '',
  69. 'required' => true ),
  70. "quality" => array( 'name' => 'Quality',
  71. 'datatype' => 'string',
  72. 'default' => '',
  73. 'required' => true ),
  74. "is_loop" => array( 'name' => "IsLoop",
  75. 'datatype' => 'integer',
  76. 'default' => 0,
  77. 'required' => true ) ),
  78. "keys" => array( "contentobject_attribute_id", "version" ),
  79. 'function_attributes' => array( 'filesize' => 'filesize',
  80. 'filepath' => 'filepath',
  81. 'mime_type_category' => 'mimeTypeCategory',
  82. 'mime_type_part' => 'mimeTypePart' ),
  83. "relations" => array( "contentobject_attribute_id" => array( "class" => "ezcontentobjectattribute",
  84. "field" => "id" ),
  85. "version" => array( "class" => "ezcontentobjectattribute",
  86. "field" => "version" )),
  87. "class_name" => "eZMedia",
  88. "name" => "ezmedia" );
  89. return $definition;
  90. }
  91. function fileSize()
  92. {
  93. $fileInfo = $this->storedFileInfo();
  94. $file = eZClusterFileHandler::instance( $fileInfo['filepath'] );
  95. if ( $file->exists() )
  96. {
  97. return $file->size();
  98. }
  99. return 0;
  100. }
  101. function filePath()
  102. {
  103. $fileInfo = $this->storedFileInfo();
  104. return $fileInfo['filepath'];
  105. }
  106. function mimeTypeCategory()
  107. {
  108. $types = explode( "/", eZPersistentObject::attribute( "mime_type" ) );
  109. return $types[0];
  110. }
  111. function mimeTypePart()
  112. {
  113. $types = explode( "/", eZPersistentObject::attribute( "mime_type" ) );
  114. return $types[1];
  115. }
  116. static function create( $contentObjectAttributeID, $version )
  117. {
  118. $row = array( "contentobject_attribute_id" => $contentObjectAttributeID,
  119. "version" => $version,
  120. "filename" => "",
  121. "original_filename" => "",
  122. "mime_type" => "",
  123. "width" => "0",
  124. "height" => "0",
  125. "controller" => true,
  126. "autoplay" => true,
  127. "pluginspage" => "",
  128. "is_loop" => false,
  129. "quality" => "",
  130. "controls" => ""
  131. );
  132. return new eZMedia( $row );
  133. }
  134. static function fetch( $id, $version, $asObject = true )
  135. {
  136. if( $version == null )
  137. {
  138. return eZPersistentObject::fetchObjectList( eZMedia::definition(),
  139. null,
  140. array( "contentobject_attribute_id" => $id ),
  141. null,
  142. null,
  143. $asObject );
  144. }
  145. else
  146. {
  147. return eZPersistentObject::fetchObject( eZMedia::definition(),
  148. null,
  149. array( "contentobject_attribute_id" => $id,
  150. "version" => $version ),
  151. $asObject );
  152. }
  153. }
  154. static function fetchByFileName( $filename, $version = null, $asObject = true )
  155. {
  156. if ( $version == null )
  157. {
  158. return eZPersistentObject::fetchObjectList( eZMedia::definition(),
  159. null,
  160. array( 'filename' => $filename ),
  161. null,
  162. null,
  163. $asObject );
  164. }
  165. else
  166. {
  167. return eZPersistentObject::fetchObject( eZMedia::definition(),
  168. null,
  169. array( 'filename' => $filename,
  170. 'version' => $version ),
  171. $asObject );
  172. }
  173. }
  174. /**
  175. * Fetch media objects by content object id
  176. * @param int $contentObjectID contentobject id
  177. * @param string $languageCode language code
  178. * @param boolean $asObject if return object
  179. * @return array
  180. */
  181. static function fetchByContentObjectID( $contentObjectID, $languageCode = null, $asObject = true )
  182. {
  183. $condition = array();
  184. $condition['contentobject_id'] = $contentObjectID;
  185. $condition['data_type_string'] = 'ezmedia';
  186. if ( $languageCode != null )
  187. {
  188. $condition['language_code'] = $languageCode;
  189. }
  190. $custom = array( array( 'operation' => 'DISTINCT id',
  191. 'name' => 'id' ) );
  192. $ids = eZPersistentObject::fetchObjectList( eZContentObjectAttribute::definition(),
  193. array(),
  194. $condition,
  195. null,
  196. null,
  197. false,
  198. false,
  199. $custom );
  200. $mediaFiles = array();
  201. foreach ( $ids as $id )
  202. {
  203. $mediaFileObjectAttribute = eZMedia::fetch( $id['id'], null, $asObject );
  204. $mediaFiles = array_merge( $mediaFiles, $mediaFileObjectAttribute );
  205. }
  206. return $mediaFiles;
  207. }
  208. static function removeByID( $id, $version )
  209. {
  210. if( $version == null )
  211. {
  212. eZPersistentObject::removeObject( eZMedia::definition(),
  213. array( "contentobject_attribute_id" => $id ) );
  214. }
  215. else
  216. {
  217. eZPersistentObject::removeObject( eZMedia::definition(),
  218. array( "contentobject_attribute_id" => $id,
  219. "version" => $version ) );
  220. }
  221. }
  222. function storedFileInfo()
  223. {
  224. $fileName = $this->attribute( 'filename' );
  225. $mimeType = $this->attribute( 'mime_type' );
  226. $originalFileName = $this->attribute( 'original_filename' );
  227. $storageDir = eZSys::storageDirectory();
  228. $group = '';
  229. $type = '';
  230. if ( $mimeType )
  231. list( $group, $type ) = explode( '/', $mimeType );
  232. $filePath = $storageDir . '/original/' . $group . '/' . $fileName;
  233. return array( 'filename' => $fileName,
  234. 'original_filename' => $originalFileName,
  235. 'filepath' => $filePath,
  236. 'mime_type' => $mimeType );
  237. }
  238. public $ContentObjectAttributeID;
  239. public $Filename;
  240. public $OriginalFilename;
  241. public $MimeType;
  242. public $Width;
  243. public $Height;
  244. public $HasController;
  245. public $Controls;
  246. public $IsLoop;
  247. public $IsAutoplay;
  248. public $Pluginspage;
  249. public $Quality;
  250. }
  251. ?>