Source for file RepositoryInputOutputModuleManager.class.php

Documentation is available at RepositoryInputOutputModuleManager.class.php

  1. <?php
  2. /**
  3. *
  4. * @package polyphony.repository.inputoutput
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: RepositoryInputOutputModuleManager.class.php,v 1.18 2007/09/19 14:04:48 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * Require our necessary files
  14. *
  15. */
  16. require_once(dirname(__FILE__)."/modules/DataManagerPrimativesModule.class.php");
  17. require_once(dirname(__FILE__)."/modules/HarmoniFileModule.class.php");
  18. require_once(HARMONI."/oki2/shared/MultiIteratorIterator.class.php");
  19.  
  20. /**
  21. * The RepositoryInputOutModuleManager is responcible for sending records to the
  22. * appropriate RepositoryInputOutputModule based on their Schema Formats.
  23. *
  24. * @package polyphony.repository.inputoutput
  25. * @version $Id: RepositoryInputOutputModuleManager.class.php,v 1.18 2007/09/19 14:04:48 adamfranco Exp $
  26. * @since $Date: 2007/09/19 14:04:48 $
  27. * @copyright 2004 Middlebury College
  28. */
  29.  
  30. class RepositoryInputOutputModuleManager {
  31.  
  32. /**
  33. * Constructor, set up the relations of the Formats to Modules
  34. *
  35. * @return object
  36. * @access public
  37. * @since 10/19/04
  38. */
  39. function RepositoryInputOutputModuleManager () {
  40. $this->_modules = array();
  41. $type = new Type("RecordStructures",
  42. "edu.middlebury.harmoni",
  43. "DataManagerPrimatives",
  44. "RecordStructures stored in the Harmoni DataManager.");
  45. $this->_modules[Type::typeToString($type)] = new DataManagerPrimativesModule;
  46. $type = new Type("RecordStructures",
  47. "edu.middlebury.harmoni",
  48. "File",
  49. "RecordStructures that store files.");
  50. $this->_modules[Type::typeToString($type)] = new HarmoniFileModule;
  51. // $this->_modules['text/plain'] = new PlainTextModule;
  52. }
  53. /**
  54. * Assign the configuration of this Manager. Valid configuration options are as
  55. * follows:
  56. * database_index integer
  57. * database_name string
  58. *
  59. * @param object Properties $configuration (original type: java.util.Properties)
  60. *
  61. * @throws object OsidException An exception with one of the following
  62. * messages defined in org.osid.OsidException: {@link }
  63. * org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
  64. * {@link org.osid.OsidException#PERMISSION_DENIED}
  65. * PERMISSION_DENIED}, {@link }
  66. * org.osid.OsidException#CONFIGURATION_ERROR
  67. * CONFIGURATION_ERROR}, {@link }
  68. * org.osid.OsidException#UNIMPLEMENTED UNIMPLEMENTED}, {@link }
  69. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  70. *
  71. * @access public
  72. */
  73. function assignConfiguration ( $configuration ) {
  74. $this->_configuration =$configuration;
  75. }
  76.  
  77. /**
  78. * Return context of this OsidManager.
  79. *
  80. * @return object OsidContext
  81. *
  82. * @throws object OsidException
  83. *
  84. * @access public
  85. */
  86. function getOsidContext () {
  87. return $this->_osidContext;
  88. }
  89.  
  90. /**
  91. * Assign the context of this OsidManager.
  92. *
  93. * @param object OsidContext $context
  94. *
  95. * @throws object OsidException An exception with one of the following
  96. * messages defined in org.osid.OsidException: {@link }
  97. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  98. *
  99. * @access public
  100. */
  101. function assignOsidContext ( $context ) {
  102. $this->_osidContext =$context;
  103. }
  104. /**
  105. * Create wizard steps for editing the values of the specified Record and
  106. * add them to the wizard.
  107. *
  108. * @param object $record
  109. * @param object $wizard The wizard to add the steps to.
  110. * @param array $partStructures An ordered array of the partStructures to include.
  111. * @return void
  112. * @access public
  113. * @since 10/19/04
  114. */
  115. function createWizardStepsForPartStructures ( $record, $wizard, $partStructures ) {
  116. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  117. ArgumentValidator::validate($wizard, new ExtendsValidatorRule("Wizard"));
  118. ArgumentValidator::validate($partStructures, new ArrayValidatorRuleWithRule(new ExtendsValidatorRule("PartStructure")));
  119. $recordStructure =$record->getRecordStructure();
  120. $format = $recordStructure->getFormat();
  121. if (!is_object($this->_modules[$format]))
  122. throwError(new Error("Unsupported Format, '$format'", "RepositoryInputOutputModuleManager", true));
  123. return $this->_modules[$format]->createWizardStepsForPartStructures($record, $wizard, $partStructures);
  124. }
  125. /**
  126. * Create wizard steps for editing the values of the specified Record and
  127. * add them to the wizard.
  128. *
  129. * @param object $record
  130. * @param object $wizard The wizard to add the steps to.
  131. * @return void
  132. * @access public
  133. * @since 10/19/04
  134. */
  135. function createWizardSteps ( $record, $wizard ) {
  136. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  137. ArgumentValidator::validate($wizard, new ExtendsValidatorRule("Wizard"));
  138. $recordStructure =$record->getRecordStructure();
  139. $format = $recordStructure->getFormat();
  140. if (!is_object($this->_modules[$format]))
  141. throwError(new Error("Unsupported Format, '$format'", "RepositoryInputOutputModuleManager", true));
  142. return $this->_modules[$format]->createWizardSteps($record, $wizard);
  143. }
  144. /**
  145. * Get the values submitted in the wizard and update the Record with them.
  146. *
  147. * @param object $record
  148. * @param object $wizard
  149. * @return void
  150. * @access public
  151. * @since 10/19/04
  152. */
  153. function updateFromWizard ( $record, $wizard ) {
  154. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  155. $recordStructure =$record->getRecordStructure();
  156. $format = $recordStructure->getFormat();
  157. if (!is_object($this->_modules[$format]))
  158. throwError(new Error("Unsupported Format, '$format'", "RepositoryInputOutputModuleManager", true));
  159. return $this->_modules[$format]->updateFromWizard($record, $wizard);
  160. }
  161. /**
  162. * Generate HTML for displaying the Record
  163. *
  164. * @param object $record
  165. * @return string
  166. * @access public
  167. * @since 10/19/04
  168. */
  169. function generateDisplay ( $repositoryId, $assetId, $record ) {
  170. ArgumentValidator::validate($repositoryId, new ExtendsValidatorRule("Id"));
  171. ArgumentValidator::validate($assetId, new ExtendsValidatorRule("Id"));
  172. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  173. $recordStructure =$record->getRecordStructure();
  174. $format = $recordStructure->getFormat();
  175. if (!is_object($this->_modules[$format]))
  176. throwError(new Error("Unsupported Format, '$format'", "RepositoryInputOutputModuleManager", true));
  177. return $this->_modules[$format]->generateDisplay($repositoryId, $assetId, $record);
  178. }
  179. /**
  180. * Generate HTML for displaying particular fields of the Record
  181. *
  182. * @param object $record The record to print.
  183. * @param array $partStructures An array of partStructures to print.
  184. * @return string
  185. * @access public
  186. * @since 10/19/04
  187. */
  188. function generateDisplayForPartStructures ( $repositoryId, $assetId,
  189. $record, $partStructures )
  190. {
  191. ArgumentValidator::validate($repositoryId, new ExtendsValidatorRule("Id"));
  192. ArgumentValidator::validate($assetId, new ExtendsValidatorRule("Id"));
  193. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  194. ArgumentValidator::validate($partStructures, new ArrayValidatorRuleWithRule(new ExtendsValidatorRule("PartStructure")));
  195. $recordStructure =$record->getRecordStructure();
  196. $type =$recordStructure->getType();
  197. if (!is_object($this->_modules[Type::typeToString($type)]))
  198. throwError(new Error("Unsupported Format, '".Type::typeToString($type)."'", "RepositoryInputOutputModuleManager", true));
  199. return $this->_modules[Type::typeToString($type)]->generateDisplayForPartStructures($repositoryId, $assetId, $record, $partStructures);
  200. }
  201. /**
  202. * Return the URL of a thumbnail image for a given Asset.
  203. *
  204. * @param object Id $assetId
  205. * @return string The URL of the thumbnail
  206. * @access public
  207. * @since 7/22/05
  208. */
  209. function getThumbnailUrlForAsset ($assetOrId ) {
  210. ArgumentValidator::validate($assetOrId,
  211. OrValidatorRule::getRule(
  212. ExtendsValidatorRule::getRule("Id"),
  213. ExtendsValidatorRule::getRule("Asset")));
  214. $rule = ExtendsValidatorRule::getRule("Id");
  215. if ($rule->check($assetOrId)) {
  216. $repositoryManager = Services::getService("RepositoryManager");
  217. $asset =$repositoryManager->getAsset($assetOrId);
  218. } else {
  219. $asset =$assetOrId;
  220. }
  221. if (!$asset)
  222. return false;
  223. $fileRecord = RepositoryInputOutputModuleManager::getFirstImageOrFileRecordForAsset(
  224. $asset);
  225. return RepositoryInputOutputModuleManager::getThumbnailUrlForRecord(
  226. $asset, $fileRecord);
  227. }
  228. /**
  229. * Return the URL of a thumbnail image for a given Asset.
  230. *
  231. * @param object Id $assetId
  232. * @return string The URL of the thumbnail
  233. * @access public
  234. * @since 7/22/05
  235. */
  236. function getThumbnailUrlForRecord ($assetOrId, $fileRecord ) {
  237. ArgumentValidator::validate($assetOrId,
  238. OrValidatorRule::getRule(
  239. ExtendsValidatorRule::getRule("Id"),
  240. ExtendsValidatorRule::getRule("Asset")));
  241. $rule = ExtendsValidatorRule::getRule("Id");
  242. if ($rule->check($assetOrId)) {
  243. $repositoryManager = Services::getService("RepositoryManager");
  244. $asset =$repositoryManager->getAsset($assetOrId);
  245. } else {
  246. $asset =$assetOrId;
  247. }
  248. $idManager = Services::getService("IdManager");
  249. $assetId =$asset->getId();
  250. $repository =$asset->getRepository();
  251. $repositoryId =$repository->getId();
  252. if ($fileRecord === FALSE)
  253. return FALSE;
  254. $fileRecordId =$fileRecord->getId();
  255. $filenameParts =$fileRecord->getPartsByPartStructure(
  256. $idManager->getId("FILE_NAME"));
  257. $filenamePart =$filenameParts->next();
  258. $filename = $filenamePart->getValue();
  259. $mimeTypeParts =$fileRecord->getPartsByPartStructure(
  260. $idManager->getId("THUMBNAIL_MIME_TYPE"));
  261. $mimeTypePart =$mimeTypeParts->next();
  262. $mimeType = $mimeTypePart->getValue();
  263. $harmoni = Harmoni::instance();
  264. $harmoni->request->startNamespace("polyphony-repository");
  265. $url = $harmoni->request->quickURL("repository", "viewthumbnail",
  266. array(
  267. "repository_id" => $repositoryId->getIdString(),
  268. "asset_id" => $assetId->getIdString(),
  269. "record_id" => $fileRecordId->getIdString()));
  270. $harmoni->request->endNamespace();
  271. return $url;
  272. }
  273. /**
  274. * Return the URL of a file for a given Asset. If the Asset has multiple
  275. * files, only one will be returned.
  276. *
  277. * @param object Id $assetId
  278. * @return string The URL of the thumbnail
  279. * @access public
  280. * @since 7/22/05
  281. */
  282. function getFileUrlForAsset ($assetOrId ) {
  283. ArgumentValidator::validate($assetOrId,
  284. OrValidatorRule::getRule(
  285. ExtendsValidatorRule::getRule("Id"),
  286. ExtendsValidatorRule::getRule("Asset")));
  287. $rule = ExtendsValidatorRule::getRule("Id");
  288. if ($rule->check($assetOrId)) {
  289. $repositoryManager = Services::getService("RepositoryManager");
  290. $asset =$repositoryManager->getAsset($assetOrId);
  291. } else {
  292. $asset =$assetOrId;
  293. }
  294. if (!$asset)
  295. return false;
  296. $fileRecord = RepositoryInputOutputModuleManager::getFirstImageOrFileRecordForAsset(
  297. $asset);
  298. return RepositoryInputOutputModuleManager::getFileUrlForRecord(
  299. $asset, $fileRecord);
  300. }
  301. /**
  302. * Return the URL of a file for a given Asset. If the Asset has multiple
  303. * files, only one will be returned.
  304. *
  305. * @param object Id $assetId
  306. * @return string The URL of the thumbnail
  307. * @access public
  308. * @since 7/22/05
  309. */
  310. function getFileUrlForRecord($assetOrId, $fileRecord ) {
  311. $idManager = Services::getService("IdManager");
  312. ArgumentValidator::validate($assetOrId,
  313. OrValidatorRule::getRule(
  314. ExtendsValidatorRule::getRule("Id"),
  315. ExtendsValidatorRule::getRule("Asset")));
  316. // Remote Files
  317. $recStruct =$fileRecord->getRecordStructure();
  318. $remoteFileId =$idManager->getId("REMOTE_FILE");
  319. if ($remoteFileId->isEqual($recStruct->getId())) {
  320. $urlParts =$fileRecord->getPartsByPartStructure($idManager->getId("FILE_URL"));
  321. $urlPart =$urlParts->next();
  322. return $urlPart->getValue();
  323. }
  324. // Local files
  325. $rule = ExtendsValidatorRule::getRule("Id");
  326. if ($rule->check($assetOrId)) {
  327. $repositoryManager = Services::getService("RepositoryManager");
  328. $asset =$repositoryManager->getAsset($assetOrId);
  329. } else {
  330. $asset =$assetOrId;
  331. }
  332. $assetId =$asset->getId();
  333. $repository =$asset->getRepository();
  334. $repositoryId =$repository->getId();
  335. if ($fileRecord === FALSE)
  336. return FALSE;
  337. $fileRecordId =$fileRecord->getId();
  338. $harmoni = Harmoni::instance();
  339. $harmoni->request->startNamespace("polyphony-repository");
  340. $url = $harmoni->request->quickURL("repository", "viewfile",
  341. array(
  342. "repository_id" => $repositoryId->getIdString(),
  343. "asset_id" => $assetId->getIdString(),
  344. "record_id" => $fileRecordId->getIdString()));
  345. $harmoni->request->endNamespace();
  346. return $url;
  347. }
  348. /**
  349. * Answer the first image Record of the Asset, if none is availible, answer
  350. * the first file of any time. If none are availible, answer FALSE.
  351. *
  352. * @param object Id $assetId
  353. * @return mixed
  354. * @access public
  355. * @since 8/19/05
  356. */
  357. function getFirstImageOrFileRecordForAsset ( $assetOrId ) {
  358. ArgumentValidator::validate($assetOrId,
  359. OrValidatorRule::getRule(
  360. ExtendsValidatorRule::getRule("Id"),
  361. ExtendsValidatorRule::getRule("Asset")));
  362. $rule = ExtendsValidatorRule::getRule("Id");
  363. if ($rule->check($assetOrId)) {
  364. $repositoryManager = Services::getService("RepositoryManager");
  365. $asset =$repositoryManager->getAsset($assetOrId);
  366. } else {
  367. $asset =$assetOrId;
  368. }
  369. $idManager = Services::getService("IdManager");
  370. $assetId =$asset->getId();
  371. // Check the cache
  372. if (!isset($GLOBALS['__RepositoryThumbRecordCache']))
  373. $GLOBALS['__RepositoryThumbRecordCache'] = array();
  374. if (!isset($GLOBALS['__RepositoryThumbRecordCache'][$assetId->getIdString()])) {
  375. $imageProcessor = Services::getService("ImageProcessor");
  376. $fileRecords = new MultiIteratorIterator();
  377. $fileRecords->addIterator($asset->getRecordsByRecordStructure($idManager->getId("FILE")));
  378. $fileRecords->addIterator($asset->getRecordsByRecordStructure($idManager->getId("REMOTE_FILE")));
  379. while ($fileRecords->hasNext()) {
  380. $record =$fileRecords->next();
  381. if (!isset($fileRecord)) {
  382. $fileRecord =$record;
  383. }
  384. $mimeTypeParts =$record->getPartsByPartStructure(
  385. $idManager->getId("MIME_TYPE"));
  386. $mimeTypePart =$mimeTypeParts->next();
  387. $mimeType = $mimeTypePart->getValue();
  388. // If this record is supported by the image processor, then use it
  389. // to generate a thumbnail instead of the default icons.
  390. if ($imageProcessor->isFormatSupported($mimeType)) {
  391. $fileRecord =$record;
  392. break;
  393. }
  394. }
  395. if (!isset($fileRecord))
  396. $GLOBALS['__RepositoryThumbRecordCache'][$assetId->getIdString()] = FALSE;
  397. else
  398. $GLOBALS['__RepositoryThumbRecordCache'][$assetId->getIdString()] =$fileRecord;
  399. }
  400. return $GLOBALS['__RepositoryThumbRecordCache'][$assetId->getIdString()];
  401. }
  402. /**
  403. * Answer true if the Asset or AssetId has a thumbnail rather than a default Icon
  404. *
  405. * @param mixed object Asset Id $assetOrID
  406. * @return boolean
  407. * @access public
  408. * @since 12/4/06
  409. */
  410. function hasThumbnailNotIcon ( $assetOrId ) {
  411. $idManager = Services::getService("IdManager");
  412. $record = RepositoryInputOutputModuleManager::getFirstImageOrFileRecordForAsset($assetOrId);
  413. if (!$record)
  414. return FALSE;
  415. // Make sure that the structure is the right one.
  416. $structure =$record->getRecordStructure();
  417. $fileId =$idManager->getId('FILE');
  418. $remoteFileId =$idManager->getId('REMOTE_FILE');
  419. if (!$fileId->isEqual($structure->getId()) && !$remoteFileId->isEqual($structure->getId())) {
  420. return FALSE;
  421. } else {
  422. // Get the parts for the record.
  423. $partIterator =$record->getParts();
  424. $parts = array();
  425. while($partIterator->hasNext()) {
  426. $part =$partIterator->next();
  427. $partStructure =$part->getPartStructure();
  428. $partStructureId =$partStructure->getId();
  429. $parts[$partStructureId->getIdString()] =$part;
  430. }
  431. // If we have a thumbnail, print that.
  432. if ($parts['THUMBNAIL_MIME_TYPE']->getValue())
  433. return TRUE;
  434. else
  435. return FALSE;
  436. }
  437. }
  438. }
  439.  
  440. ?>

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