Source for file FileRecord.class.php

Documentation is available at FileRecord.class.php

  1. <?php
  2.  
  3. require_once(dirname(__FILE__)."/Fields/FileDataPart.class.php");
  4. require_once(dirname(__FILE__)."/Fields/FileNamePart.class.php");
  5. require_once(dirname(__FILE__)."/Fields/FileSizePart.class.php");
  6. require_once(dirname(__FILE__)."/Fields/MimeTypePart.class.php");
  7. require_once(dirname(__FILE__)."/Fields/DimensionsPart.class.php");
  8. require_once(dirname(__FILE__)."/Fields/ThumbnailDataPart.class.php");
  9. require_once(dirname(__FILE__)."/Fields/ThumbnailMimeTypePart.class.php");
  10. require_once(dirname(__FILE__)."/Fields/ThumbnailDimensionsPart.class.php");
  11. require_once(HARMONI."/oki2/repository/HarmoniPartIterator.class.php");
  12.  
  13. /**
  14. * Each Asset has one of the AssetType supported by the Repository. There are
  15. * also zero or more RecordStructures required by the Repository for each
  16. * AssetType. RecordStructures provide structural information. The values for
  17. * a given Asset's RecordStructure are stored in a Record. RecordStructures
  18. * can contain sub-elements which are referred to as PartStructures. The
  19. * structure defined in the RecordStructure and its PartStructures is used in
  20. * for any Records for the Asset. Records have Parts which parallel
  21. * PartStructures.
  22. *
  23. * <p>
  24. * OSID Version: 2.0
  25. * </p>
  26. *
  27. * @package harmoni.osid_v2.repository
  28. *
  29. * @copyright Copyright &copy;2005, Middlebury College
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  31. *
  32. * @version $Id: FileRecord.class.php,v 1.27 2007/09/11 17:40:57 adamfranco Exp $
  33. */
  34. class FileRecord
  35. extends RecordInterface
  36. {
  37. var $_id;
  38. var $_recordStructure;
  39. var $_parts;
  40. var $_partsLoaded;
  41. function FileRecord( $recordStructure, $id, $configuration, $asset ) {
  42. $this->_id =$id;
  43. $this->_recordStructure =$recordStructure;
  44. $this->_configuration =$configuration;
  45. $this->_asset =$asset;
  46. $idManager = Services::getService("Id");
  47. $this->_parts = array();
  48. $this->addFileDataPart($recordStructure);
  49. $this->_parts['FILE_NAME'] = new FileNamePart(
  50. $recordStructure->getPartStructure($idManager->getId('FILE_NAME')),
  51. $this->_id,
  52. $this->_configuration,
  53. $this->_asset);
  54. $this->_parts['FILE_SIZE'] = new FileSizePart(
  55. $recordStructure->getPartStructure($idManager->getId('FILE_SIZE')),
  56. $this->_id,
  57. $this->_configuration,
  58. $this->_asset);
  59. $this->_parts['MIME_TYPE'] = new MimeTypePart(
  60. $recordStructure->getPartStructure($idManager->getId('MIME_TYPE')),
  61. $this->_id,
  62. $this->_configuration,
  63. $this->_asset);
  64. $this->_parts['DIMENSIONS'] = new DimensionsPart(
  65. $recordStructure->getPartStructure($idManager->getId('DIMENSIONS')),
  66. $this->_id,
  67. $this->_configuration,
  68. $this,
  69. $this->_asset);
  70. $this->_parts['THUMBNAIL_DATA'] = new ThumbnailDataPart(
  71. $recordStructure->getPartStructure($idManager->getId('THUMBNAIL_DATA')),
  72. $this->_id,
  73. $this->_configuration,
  74. $this->_asset);
  75. $this->_parts['THUMBNAIL_MIME_TYPE'] = new ThumbnailMimeTypePart(
  76. $recordStructure->getPartStructure($idManager->getId('THUMBNAIL_MIME_TYPE')),
  77. $this->_id,
  78. $this->_configuration,
  79. $this->_asset);
  80. $this->_parts['THUMBNAIL_DIMENSIONS'] = new ThumbnailDimensionsPart(
  81. $recordStructure->getPartStructure($idManager->getId('THUMBNAIL_DIMENSIONS')),
  82. $this->_id,
  83. $this->_configuration,
  84. $this,
  85. $this->_asset);
  86. $this->_partsLoaded = false;
  87. }
  88. /**
  89. * Add a fileDataPart
  90. *
  91. * @param $recordStructure
  92. * @return void
  93. * @access public
  94. * @since 12/6/06
  95. */
  96. function addFileDataPart ($recordStructure) {
  97. $idManager = Services::getService("Id");
  98. $this->_parts['FILE_DATA'] = new FileDataPart(
  99. $recordStructure->getPartStructure($idManager->getId('FILE_DATA')),
  100. $this->_id,
  101. $this->_configuration,
  102. $this->_asset);
  103. }
  104.  
  105. /**
  106. * Get the unique Id for this Record.
  107. *
  108. * @return object Id
  109. *
  110. * @throws object RepositoryException An exception with one of
  111. * the following messages defined in
  112. * org.osid.repository.RepositoryException may be thrown: {@link }
  113. * org.osid.repository.RepositoryException#OPERATION_FAILED
  114. * OPERATION_FAILED}, {@link }
  115. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  116. * PERMISSION_DENIED}, {@link }
  117. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  118. * CONFIGURATION_ERROR}, {@link }
  119. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  120. * UNIMPLEMENTED}
  121. *
  122. * @access public
  123. */
  124. function getId() {
  125. return $this->_id;
  126. }
  127.  
  128. /**
  129. * Create a Part. Records are composed of Parts. Parts can also contain
  130. * other Parts. Each Record is associated with a specific RecordStructure
  131. * and each Part is associated with a specific PartStructure.
  132. *
  133. * @param object Id $partStructureId
  134. * @param object mixed $value (original type: java.io.Serializable)
  135. *
  136. * @return object Part
  137. *
  138. * @throws object RepositoryException An exception with one of
  139. * the following messages defined in
  140. * org.osid.repository.RepositoryException may be thrown: {@link }
  141. * org.osid.repository.RepositoryException#OPERATION_FAILED
  142. * OPERATION_FAILED}, {@link }
  143. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  144. * PERMISSION_DENIED}, {@link }
  145. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  146. * CONFIGURATION_ERROR}, {@link }
  147. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  148. * UNIMPLEMENTED}, {@link }
  149. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  150. * NULL_ARGUMENT}, {@link }
  151. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  152. *
  153. * @access public
  154. */
  155. function createPart($partStructureId, $value) {
  156. $found = FALSE;
  157. foreach($this->_parts as $key => $val){
  158. if($partStructureId->isEqual($val->getId()))
  159. break;
  160. $found=TRUE;
  161. /*
  162. while ($parts->hasNext()) {
  163. $part =$parts->next();
  164. if ($partStructureId->isEqual($part->getId())) {
  165. break;
  166. $found = TRUE;
  167. }
  168. */ }
  169. if (!$found)
  170. throwError(new Error(RepositoryException::UNKNOWN_ID(), "FileRecord", true));
  171. $partIdString = $partStructureId->getIdString();
  172. // if (is_object($this->_parts[$partIdString]))
  173. // throwError(new Error(PERMISSION_DENIED.": Can't add another field to a
  174. // non-multi-valued part.", "FileRecord", true));
  175. // } else {
  176. //
  177. // switch ($partIdString) {
  178. // case "FILE_DATA":
  179. // $className = "FileDataPart";
  180. // break;
  181. // case "FILE_NAME":
  182. // $className = "FileNamePart";
  183. // break;
  184. // case "FILE_SIZE":
  185. // $className = "FileSizePart";
  186. // break;
  187. // case "MIME_TYPE":
  188. // $className = "MimeTypePart";
  189. // break;
  190. // default:
  191. // throwError(new Error(OPERATION_FAILED, "FileRecord", true));
  192. // }
  193. //
  194. // $this->_parts[$partIdString] = new $className(
  195. // $part,
  196. // $this->_id,
  197. // $this->configuration);
  198. // }
  199. $this->_parts[$partIdString]->updateValue($value);
  200. // Make sure that we don't trigger deleting of the whole record by deleting
  201. // and recreating parts
  202. if (isset($this->_toDelete) && in_array($partIdString, $this->_toDelete)) {
  203. $key = array_search($partIdString, $this->_toDelete);
  204. unset ($this->_toDelete[$key]);
  205. }
  206. $this->_asset->updateModificationDate();
  207. return $this->_parts[$partIdString];
  208. }
  209.  
  210. /**
  211. * Delete a Part and all its Parts.
  212. *
  213. * @param object Id $partId
  214. *
  215. * @throws object RepositoryException An exception with one of
  216. * the following messages defined in
  217. * org.osid.repository.RepositoryException may be thrown: {@link }
  218. * org.osid.repository.RepositoryException#OPERATION_FAILED
  219. * OPERATION_FAILED}, {@link }
  220. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  221. * PERMISSION_DENIED}, {@link }
  222. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  223. * CONFIGURATION_ERROR}, {@link }
  224. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  225. * UNIMPLEMENTED}, {@link }
  226. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  227. * NULL_ARGUMENT}, {@link }
  228. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  229. *
  230. * @access public
  231. */
  232. function deletePart($partId) {
  233. $string = $partId->getIdString();
  234. if (ereg("(.*)-(".implode("|", array_keys($this->_parts)).")",$string,$r)) {
  235. $recordId = $r[1];
  236. $field = $r[2];
  237. if ($this->_isLastPart($field)) {
  238. $dbHandler = Services::getService("DatabaseManager");
  239. // Delete the data
  240. $query = new DeleteQuery();
  241. $query->setTable("dr_file_data");
  242. $query->setWhere("fk_file = '".$this->_id->getIdString()."'");
  243. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  244. // Delete the thumbnail
  245. $query = new DeleteQuery();
  246. $query->setTable("dr_thumbnail");
  247. $query->setWhere("fk_file = '".$this->_id->getIdString()."'");
  248. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  249. // delete the file row.
  250. $query = new DeleteQuery();
  251. $query->setTable("dr_file");
  252. $query->setWhere("id = '".$this->_id->getIdString()."'");
  253. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  254. } else if ($field != "FILE_SIZE") {
  255. $this->_parts[$field]->updateValue("NULL");
  256. }
  257. } else {
  258. throwError(new Error(RepositoryException::UNKNOWN_ID().": $string", "FileRecord", true));
  259. }
  260. $this->_asset->updateModificationDate();
  261. }
  262.  
  263. /**
  264. * Get all the Parts in the Record. Iterators return a set, one at a time.
  265. *
  266. * @return object PartIterator
  267. *
  268. * @throws object RepositoryException An exception with one of
  269. * the following messages defined in
  270. * org.osid.repository.RepositoryException may be thrown: {@link }
  271. * org.osid.repository.RepositoryException#OPERATION_FAILED
  272. * OPERATION_FAILED}, {@link }
  273. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  274. * PERMISSION_DENIED}, {@link }
  275. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  276. * CONFIGURATION_ERROR}, {@link }
  277. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  278. * UNIMPLEMENTED}
  279. *
  280. * @access public
  281. */
  282. function getParts() {
  283. $this->_loadParts();
  284. // Create an iterator and return it.
  285. $partIterator = new HarmoniPartIterator($this->_parts);
  286. return $partIterator;
  287. }
  288.  
  289. /**
  290. * Get the part from the record that matches the passed Id
  291. *
  292. * WARNING: NOT IN OSID
  293. *
  294. * @param object HarmoniId
  295. * @return object HarmoniPart
  296. * @access public
  297. * @since 10/10/05
  298. */
  299. function getPart ($id) {
  300. $parts =$this->getParts();
  301. while ($parts->hasNext()) {
  302. $part =$parts->next();
  303. if ($part->getId() == $id)
  304. return $part;
  305. }
  306. $false = false;
  307. return $false;
  308. }
  309.  
  310. /**
  311. * Return true if this Record is multi-valued; false otherwise. This is determined by the implementation.
  312. *
  313. * WARNING: NOT IN OSID - Use at your own risk
  314. *
  315. * @return boolean
  316. * @throws osid.dr.DigitalRepositoryException An exception with one of the following messages defined in osid.dr.DigitalRepositoryException may be thrown: {@link DigitalRepositoryException#OPERATION_FAILED OPERATION_FAILED}, {@link DigitalRepositoryException#PERMISSION_DENIED PERMISSION_DENIED}, {@link DigitalRepositoryException#CONFIGURATION_ERROR CONFIGURATION_ERROR}, {@link DigitalRepositoryException#UNIMPLEMENTED UNIMPLEMENTED}
  317. */
  318. function isMultivalued() {
  319. return true; // we allow as many Records of any RecordStructure as people want.
  320. }
  321.  
  322. /**
  323. * Get the RecordStructure associated with this Record.
  324. *
  325. * @return object RecordStructure
  326. *
  327. * @throws object RepositoryException An exception with one of
  328. * the following messages defined in
  329. * org.osid.repository.RepositoryException may be thrown: {@link }
  330. * org.osid.repository.RepositoryException#OPERATION_FAILED
  331. * OPERATION_FAILED}, {@link }
  332. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  333. * PERMISSION_DENIED}, {@link }
  334. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  335. * CONFIGURATION_ERROR}, {@link }
  336. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  337. * UNIMPLEMENTED}
  338. *
  339. * @access public
  340. */
  341. function getRecordStructure() {
  342. return $this->_recordStructure;
  343. }
  344. /**
  345. * Return TRUE if the Part of the passed Id is the last one, and the whole schebang should be deleted.
  346. *
  347. * WARNING: NOT IN OSID - Use at your own risk
  348. *
  349. * @param string $idString
  350. * @return boolean
  351. * @access private
  352. * @since 10/25/04
  353. */
  354. function _isLastPart ($idString) {
  355. if (!isset($this->_toDelete))
  356. $this->_toDelete = array();
  357. $dbHandler = Services::getService("DatabaseManager");
  358. // Check to see if the data is in the database
  359. $query = new SelectQuery;
  360. $query->addTable("dr_file");
  361. $query->addTable("dr_file_data", LEFT_JOIN, "dr_file.id = dr_file_data.fk_file");
  362. $query->addTable("dr_thumbnail", LEFT_JOIN, "dr_file.id = dr_thumbnail.fk_file");
  363. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_file.fk_mime_type = file_mime_type.id", "file_mime_type");
  364. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_thumbnail.fk_mime_type = thumbnail_mime_type.id", "thumbnail_mime_type");
  365. $query->addColumn("filename", "FILE_NAME");
  366. $query->addColumn("size", "FILE_SIZE");
  367. $query->addColumn("file_mime_type.type", "MIME_TYPE");
  368. $query->addColumn("dr_file_data.data", "FILE_DATA");
  369. $query->addColumn("thumbnail_mime_type.type", "THUMBNAIL_MIME_TYPE");
  370. $query->addColumn("dr_thumbnail.data", "THUMBNAIL_DATA");
  371. $query->addWhere("dr_file.id = '".$this->_id->getIdString()."'");
  372. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  373. if (!$result->getNumberOfRows()) {
  374. $result->free();
  375. return TRUE;
  376. }
  377. $fields = array('FILE_NAME', 'FILE_SIZE', 'MIME_TYPE', 'FILE_DATA', 'THUMBNAIL_MIME_TYPE', 'THUMBNAIL_DATA');
  378. $countValues = 0;
  379. foreach ($fields as $field) {
  380. if ($result->field($field) && !in_array($field, $this->_toDelete))
  381. $countValues++;
  382. }
  383. $result->free();
  384. $this->_toDelete[] = $idString;
  385. if ($countValues <= 1)
  386. return TRUE;
  387. else
  388. return FALSE;
  389. }
  390. /**
  391. * Get the Parts of the Records for this Asset that are based on this
  392. * RecordStructure PartStructure's unique Id.
  393. *
  394. * WARNING: NOT IN OSID (as of July 2005)
  395. *
  396. * @param object Id $partStructureId
  397. *
  398. * @return object PartIterator
  399. *
  400. * @throws object RepositoryException An exception with one of
  401. * the following messages defined in
  402. * org.osid.repository.RepositoryException may be thrown: {@link }
  403. * org.osid.repository.RepositoryException#OPERATION_FAILED
  404. * OPERATION_FAILED}, {@link }
  405. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  406. * PERMISSION_DENIED}, {@link }
  407. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  408. * CONFIGURATION_ERROR}, {@link }
  409. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  410. * UNIMPLEMENTED}, {@link }
  411. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  412. * NULL_ARGUMENT}, {@link }
  413. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  414. *
  415. * @access public
  416. */
  417. function getPartsByPartStructure ( $partStructureId ) {
  418. $this->_loadParts();
  419. $partArray = array();
  420. if (isset($this->_parts[$partStructureId->getIdString()]))
  421. $partArray[] =$this->_parts[$partStructureId->getIdString()];
  422. $partsIterator = new HarmoniIterator($partArray);
  423. return $partsIterator;
  424. }
  425. /**
  426. * Do a single query to load all of the small-valued parts for the record;
  427. * that is, everything but the file and thumb data.
  428. *
  429. * @return void
  430. * @access private
  431. * @since 11/17/05
  432. */
  433. function _loadParts () {
  434. if ($this->_partsLoaded)
  435. return;
  436. $dbHandler = Services::getService("DBHandler");
  437. $query = new SelectQuery;
  438. $query->addTable("dr_file");
  439. $query->addTable("dr_thumbnail", LEFT_JOIN, "dr_file.id = dr_thumbnail.fk_file");
  440. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_file.fk_mime_type = file_mime_type.id", "file_mime_type");
  441. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_thumbnail.fk_mime_type = thumbnail_mime_type.id", "thumbnail_mime_type");
  442. $query->addColumn("filename");
  443. $query->addColumn("size");
  444. $query->addColumn("dr_file.width", "file_width");
  445. $query->addColumn("dr_file.height", "file_height");
  446. $query->addColumn("file_mime_type.type", "file_type");
  447. $query->addColumn("thumbnail_mime_type.type", "thumbnail_type");
  448. $query->addColumn("dr_thumbnail.width", "thumb_width");
  449. $query->addColumn("dr_thumbnail.height", "thumb_height");
  450. $query->addWhere("dr_file.id = '".$this->_id->getIdString()."'");
  451. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  452. if ($result->getNumberOfRows()) {
  453. $this->_parts['FILE_NAME']->_updateValue($result->field('filename'));
  454. $this->_parts['FILE_SIZE']->_updateValue($result->field('size'));
  455. $this->_parts['MIME_TYPE']->_updateValue($result->field('file_type'));
  456. $this->_parts['DIMENSIONS']->_updateValue(array($result->field('file_width'), $result->field('file_height')));
  457. $this->_parts['THUMBNAIL_MIME_TYPE']->_updateValue($result->field('thumbnail_type'));
  458. $this->_parts['THUMBNAIL_DIMENSIONS']->_updateValue(array($result->field('thumb_width'), $result->field('thumb_height')));
  459. }
  460. $this->_partsLoaded = true;
  461. }
  462. }

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