Source for file DimensionsPart.class.php

Documentation is available at DimensionsPart.class.php

  1. <?php
  2. /**
  3. * @package harmoni.osid_v2.repository
  4. *
  5. * @copyright Copyright &copy;2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  7. *
  8. * @version $Id: DimensionsPart.class.php,v 1.12 2007/09/04 20:25:44 adamfranco Exp $
  9. */
  10. require_once(dirname(__FILE__)."/../getid3.getimagesize.php");
  11.  
  12. /**
  13. * The DimensionsPart attempts to extract height, width, and mime type info from
  14. * a file, in an array similar to that returned from GetImageSize() method.
  15. * If the file is not an image and/or such information can not be determined,
  16. * this part has a boolean value of false.
  17. *
  18. * @package harmoni.osid_v2.repository
  19. *
  20. * @copyright Copyright &copy;2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  22. *
  23. * @version $Id: DimensionsPart.class.php,v 1.12 2007/09/04 20:25:44 adamfranco Exp $
  24. */
  25. class DimensionsPart
  26. extends Part
  27. {
  28.  
  29. var $_recordId;
  30. var $_partStructure;
  31. var $_dimensions;
  32. /**
  33. * Constructor
  34. *
  35. * @param object PartStructure $partStructure
  36. * @param object Id $recordId
  37. * @param object Properties $configuration
  38. * @param object Record $record
  39. * @return object
  40. * @access public
  41. * @since 10/17/05
  42. */
  43. function DimensionsPart( $partStructure, $recordId, $configuration, $record, $asset ) {
  44. $this->_recordId =$recordId;
  45. $this->_partStructure =$partStructure;
  46. $this->_configuration =$configuration;
  47. $this->_record =$record;
  48. $this->_asset =$asset;
  49. $this->_table = "dr_file";
  50. $this->_idColumn = "id";
  51. $this->_widthColumn = 'width';
  52. $this->_heightColumn = 'height';
  53. $idManager = Services::getService("Id");
  54. $this->_dataPartStructId =$idManager->getId("FILE_DATA");
  55. $this->_mimeTypePartStructId =$idManager->getId("MIME_TYPE");
  56. // Set our dimensions to NULL, so that we can know if it has not been checked
  57. // for yet. If we search for info, but don't have any, or the dimensions is
  58. // an empty string, it will have value FALSE instead of NULL
  59. $this->_dimensions = NULL;
  60. }
  61. /**
  62. * Get the unique Id for this Part.
  63. *
  64. * @return object Id
  65. *
  66. * @throws object RepositoryException An exception with one of
  67. * the following messages defined in
  68. * org.osid.repository.RepositoryException may be thrown: {@link }
  69. * org.osid.repository.RepositoryException#OPERATION_FAILED
  70. * OPERATION_FAILED}, {@link }
  71. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  72. * PERMISSION_DENIED}, {@link }
  73. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  74. * CONFIGURATION_ERROR}, {@link }
  75. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  76. * UNIMPLEMENTED}
  77. *
  78. * @access public
  79. */
  80. function getId() {
  81. $idManager = Services::getService("Id");
  82. $partStructureId =$this->_partStructure->getId();
  83. return $idManager->getId($this->_recordId->getIdString()
  84. ."-".$partStructureId->getIdString());
  85. }
  86.  
  87. /**
  88. * Create a Part. Records are composed of Parts. Parts can also contain
  89. * other Parts. Each Record is associated with a specific RecordStructure
  90. * and each Part is associated with a specific PartStructure.
  91. *
  92. * @param object Id $partStructureId
  93. * @param object mixed $value (original type: java.io.Serializable)
  94. *
  95. * @return object Part
  96. *
  97. * @throws object RepositoryException An exception with one of
  98. * the following messages defined in
  99. * org.osid.repository.RepositoryException may be thrown: {@link }
  100. * org.osid.repository.RepositoryException#OPERATION_FAILED
  101. * OPERATION_FAILED}, {@link }
  102. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  103. * PERMISSION_DENIED}, {@link }
  104. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  105. * CONFIGURATION_ERROR}, {@link }
  106. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  107. * UNIMPLEMENTED}, {@link }
  108. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  109. * NULL_ARGUMENT}, {@link }
  110. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  111. *
  112. * @access public
  113. */
  114. function createPart($partStructuretId, $value) {
  115. throwError(
  116. new Error(UNIMPLEMENTED, "HarmoniPart", true));
  117. }
  118.  
  119. /**
  120. * Delete a Part and all its Parts.
  121. *
  122. * @param object Id $partId
  123. *
  124. * @throws object RepositoryException An exception with one of
  125. * the following messages defined in
  126. * org.osid.repository.RepositoryException may be thrown: {@link }
  127. * org.osid.repository.RepositoryException#OPERATION_FAILED
  128. * OPERATION_FAILED}, {@link }
  129. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  130. * PERMISSION_DENIED}, {@link }
  131. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  132. * CONFIGURATION_ERROR}, {@link }
  133. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  134. * UNIMPLEMENTED}, {@link }
  135. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  136. * NULL_ARGUMENT}, {@link }
  137. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  138. *
  139. * @access public
  140. */
  141. function deletePart($partId) {
  142. throwError(
  143. new Error(RepositoryException::UNIMPLEMENTED(), "HarmoniPart", true));
  144. }
  145.  
  146. /**
  147. * Get all the Parts in this Part. Iterators return a set, one at a time.
  148. *
  149. * @return object PartIterator
  150. *
  151. * @throws object RepositoryException An exception with one of
  152. * the following messages defined in
  153. * org.osid.repository.RepositoryException may be thrown: {@link }
  154. * org.osid.repository.RepositoryException#OPERATION_FAILED
  155. * OPERATION_FAILED}, {@link }
  156. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  157. * PERMISSION_DENIED}, {@link }
  158. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  159. * CONFIGURATION_ERROR}, {@link }
  160. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  161. * UNIMPLEMENTED}
  162. *
  163. * @access public
  164. */
  165. function getParts() {
  166. throwError(
  167. new Error(RepositoryException::UNIMPLEMENTED(), "HarmoniPart", true));
  168. }
  169.  
  170. /**
  171. * Get the value for this Part.
  172. *
  173. * @return object mixed (original type: java.io.Serializable)
  174. *
  175. * @throws object RepositoryException An exception with one of
  176. * the following messages defined in
  177. * org.osid.repository.RepositoryException may be thrown: {@link }
  178. * org.osid.repository.RepositoryException#OPERATION_FAILED
  179. * OPERATION_FAILED}, {@link }
  180. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  181. * PERMISSION_DENIED}, {@link }
  182. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  183. * CONFIGURATION_ERROR}, {@link }
  184. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  185. * UNIMPLEMENTED}
  186. *
  187. * @access public
  188. */
  189. function getValue() {
  190. // If we don't have the dimensions, fetch the mime type and data and try to
  191. // populate the dimensions if appropriate.
  192. if ($this->_dimensions === NULL) {
  193. $dbHandler = Services::getService("DatabaseManager");
  194. $query = new SelectQuery;
  195. $query->addTable($this->_table);
  196. $query->addColumn($this->_widthColumn);
  197. $query->addColumn($this->_heightColumn);
  198. $query->addColumn("(height IS NOT NULL AND width IS NOT NULL)",
  199. "dimensions_exist");
  200. $query->addWhere($this->_idColumn." = '".$this->_recordId->getIdString()."'");
  201. $result =$dbHandler->query($query,
  202. $this->_configuration->getProperty("database_index"));
  203. if ($result->getNumberOfRows() == 0) {
  204. $this->_dimensions = FALSE;
  205. } else if ($result->field("dimensions_exist") == false) {
  206. // Get the MIME type
  207. $mimeTypeParts =$this->_record->getPartsByPartStructure(
  208. $this->_mimeTypePartStructId);
  209. $mimeTypePart =$mimeTypeParts->next();
  210. $mimeType = $mimeTypePart->getValue();
  211. // Only try to get dimensions from image files
  212. if (ereg("^image.*$", $mimeType)) {
  213. $dataParts =$this->_record->getPartsByPartStructure(
  214. $this->_dataPartStructId);
  215. if ($dataParts->hasNext()) {
  216. $dataPart =$dataParts->next();
  217. $this->_dimensions =
  218. GetDataImageSize($dataPart->getValue());
  219. if (isset($this->_dimensions[2]))
  220. unset($this->_dimensions[2]);
  221. $this->updateValue($this->_dimensions);
  222. } else {
  223. $this->_dimensions = FALSE;
  224. }
  225. } else
  226. $this->_dimensions = FALSE;
  227. } else {
  228. $this->_width = $result->field($this->_widthColumn);
  229. $this->_height = $result->field($this->_heightColumn);
  230. $this->_dimensions = array($this->_width, $this->_height);
  231. }
  232. $result->free();
  233. }
  234. return $this->_dimensions;
  235. }
  236.  
  237. /**
  238. * Update the value for this Part.
  239. *
  240. * @param object mixed $value (original type: java.io.Serializable)
  241. *
  242. * @throws object RepositoryException An exception with one of
  243. * the following messages defined in
  244. * org.osid.repository.RepositoryException may be thrown: {@link }
  245. * org.osid.repository.RepositoryException#OPERATION_FAILED
  246. * OPERATION_FAILED}, {@link }
  247. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  248. * PERMISSION_DENIED}, {@link }
  249. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  250. * CONFIGURATION_ERROR}, {@link }
  251. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  252. * UNIMPLEMENTED}, {@link }
  253. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  254. * NULL_ARGUMENT}
  255. *
  256. * @access public
  257. */
  258. function updateValue($value) {
  259. if (is_array($value)) {
  260. $this->_dimensions = $value;
  261. $dbHandler = Services::getService("DatabaseManager");
  262.  
  263. $query = new SelectQuery;
  264. $query->addTable($this->_table);
  265. $query->addColumn("COUNT(*)", "count");
  266. $query->addWhere($this->_idColumn." = '".$this->_recordId->getIdString()."'");
  267. $result =$dbHandler->query($query,
  268. $this->_configuration->getProperty("database_index"));
  269. if ($result->field("count") > 0) {
  270. $query = new UpdateQuery;
  271. $query->setTable($this->_table);
  272. $query->setColumns(array($this->_widthColumn, $this->_heightColumn));
  273. $query->setValues(array("'".$this->_dimensions[0]."'",
  274. "'".$this->_dimensions[1]."'"));
  275. $query->addWhere($this->_idColumn." = '".$this->_recordId->getIdString()."'");
  276. } else {
  277. $query = new InsertQuery;
  278. $query->setTable($this->_table);
  279. $query->setColumns(array($this->_idColumn, $this->_widthColumn, $this->_heightColumn));
  280. $query->setValues(array("'".$this->_recordId->getIdString()."'",
  281. "'".$this->_dimensions[0]."'",
  282. "'".$this->_dimensions[1]."'"));
  283. }
  284. $result->free();
  285. $dbHandler->query($query,
  286. $this->_configuration->getProperty("database_index"));
  287. // This gets done during thumbnail generation, so don't change our
  288. // asset's modification date.
  289. // $this->_asset->updateModificationDate();
  290. }
  291. }
  292.  
  293. /**
  294. * Get the PartStructure associated with this Part.
  295. *
  296. * @return object PartStructure
  297. *
  298. * @throws object RepositoryException An exception with one of
  299. * the following messages defined in
  300. * org.osid.repository.RepositoryException may be thrown: {@link }
  301. * org.osid.repository.RepositoryException#OPERATION_FAILED
  302. * OPERATION_FAILED}, {@link }
  303. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  304. * PERMISSION_DENIED}, {@link }
  305. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  306. * CONFIGURATION_ERROR}, {@link }
  307. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  308. * UNIMPLEMENTED}
  309. *
  310. * @access public
  311. */
  312. function getPartStructure() {
  313. return $this->_partStructure;
  314. }
  315. /**
  316. * Allow the file record to update the fetch from its own queries
  317. *
  318. * @param array $value
  319. * @return void
  320. * @access private
  321. * @since 11/17/05
  322. */
  323. function _updateValue ( $value ) {
  324. if ($value[0] && $value[1]) {
  325. $this->_dimensions = $value;
  326. $this->_width = $value[0];
  327. $this->_height = $value[1];
  328. }
  329. }
  330. }

Documentation generated on Wed, 19 Sep 2007 10:22:44 -0400 by phpDocumentor 1.3.0RC3