Source for file HarmoniFileModule.class.php

Documentation is available at HarmoniFileModule.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: HarmoniFileModule.class.php,v 1.20 2007/09/19 14:04:48 adamfranco Exp $
  10. */
  11.  
  12. /**
  13. * Require the class that we are extending.
  14. *
  15. */
  16. require_once(dirname(__FILE__)."/../RepositoryInputOutputModule.interface.php");
  17. require_once(HARMONI."Primitives/Numbers/ByteSize.class.php");
  18.  
  19. /**
  20. * InputOutput modules are classes which generate HTML for the display or editing
  21. * of Records. Which InputOutput module to use is determined by the Format
  22. * of the RecordStructure corresponding to that Record. For example, a Structure
  23. * using the "DataManagerPrimitive" Format would use the DataManagerPrimative
  24. * InputOutput module for displaying generating forms for editing its data.
  25. *
  26. * @package polyphony.repository.inputoutput
  27. * @version $Id: HarmoniFileModule.class.php,v 1.20 2007/09/19 14:04:48 adamfranco Exp $
  28. * @since $Date: 2007/09/19 14:04:48 $
  29. * @copyright 2004 Middlebury College
  30. */
  31.  
  32. class HarmoniFileModule
  33. extends RepositoryInputOutputModuleInterface {
  34. /**
  35. * Constructor
  36. *
  37. * @return obj
  38. * @access public
  39. * @since 10/19/04
  40. */
  41. function HarmoniFileModule () {
  42. }
  43. /**
  44. * Create wizard steps for editing the values of the specified Record and
  45. * add them to the wizard.
  46. *
  47. * @param object $record
  48. * @param object $wizard The wizard to add the steps to.
  49. * @param array $partStructures An ordered array of the partStructures to include.
  50. * @return void
  51. * @access public
  52. * @since 10/19/04
  53. */
  54. function createWizardStepsForPartStructures ( $record, $wizard, $partStructures ) {
  55. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  56. ArgumentValidator::validate($wizard, new ExtendsValidatorRule("Wizard"));
  57. ArgumentValidator::validate($partStructures, new ArrayValidatorRuleWithRule(new ExtendsValidatorRule("PartStructure")));
  58. $this->createWizardSteps($record, $wizard);
  59. }
  60. /**
  61. * Create wizard steps for editing the values of the specified Record and
  62. * add them to the wizard.
  63. *
  64. * @param object $record
  65. * @param object $wizard The wizard to add the steps to.
  66. * @return void
  67. * @access public
  68. * @since 10/19/04
  69. */
  70. function createWizardSteps ( $record, $wizard ) {
  71. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  72. ArgumentValidator::validate($wizard, new ExtendsValidatorRule("Wizard"));
  73. $recordStructure =$record->getRecordStructure();
  74. // Get all the parts
  75. $partIterator =$record->getParts();
  76. $parts = array();
  77. while($partIterator->hasNext()) {
  78. $part =$partIterator->next();
  79. $partStructure =$part->getPartStructure();
  80. $partStructureId =$partStructure->getId();
  81. $parts[$partStructureId->getIdString()] =$part;
  82. }
  83. $step =$wizard->addStep("record", new WizardStep());
  84. $step->setDisplayName($recordStructure->getDisplayName());
  85. ob_start();
  86. $component =$step->addComponent("file_upload", new WFileUploadField());
  87. print "\n<em>"._("Upload a new file or change file properties.")."</em>\n<hr />";
  88. print "\n<br /><strong>";
  89. if ($parts['FILE_NAME']->getValue()) {
  90. print _("New file (optional)");
  91. } else {
  92. print _("File");
  93. }
  94. print ":</strong>";
  95. print "\n[[file_upload]]";
  96. $component =$step->addComponent("file_name", new WTextField());
  97. $component->setValue($parts['FILE_NAME']->getValue());
  98. $component =$step->addComponent("use_custom_filename", new WCheckBox());
  99. $component->setValue(false);
  100. $component =$step->addComponent("file_size", new WTextField());
  101. $size = ByteSize::withValue($parts['FILE_SIZE']->getValue());
  102. $component->setValue($size->asString());
  103. $component->setEnabled(FALSE, TRUE);
  104. //
  105. // $component =$step->addComponent("size_from_file", new WCheckBox());
  106. // $component->setValue(false);
  107. $component =$step->addComponent("mime_type", new WTextField());
  108. $component->setValue($parts['MIME_TYPE']->getValue());
  109. $component =$step->addComponent("use_custom_type", new WCheckBox());
  110. $component->setValue(false);
  111. // Dimensions
  112. $dimensionComponent = new WTextField();
  113. $dimensionComponent->setSize(8);
  114. $dimensionComponent->setStyle("text-align: right");
  115. $dimensionComponent->setErrorRule(new WECRegex("^([0-9]+px)?$"));
  116. $dimensionComponent->setErrorText(_("Must be a positive integer followed by 'px'."));
  117. $dimensionComponent->addOnChange("validateWizard(this.form);");
  118. $dim = $parts['DIMENSIONS']->getValue();
  119. $component =$step->addComponent("height", $dimensionComponent->shallowCopy());
  120. if ($dim[1])
  121. $component->setValue($dim[1].'px');
  122.  
  123. $component =$step->addComponent("use_custom_height", new WCheckBox());
  124. $component->setValue(false);
  125. $component =$step->addComponent("width", $dimensionComponent->shallowCopy());
  126. if ($dim[0])
  127. $component->setValue($dim[0].'px');
  128. $component =$step->addComponent("use_custom_width", new WCheckBox());
  129. $component->setValue(false);
  130. // Thumnail Upload
  131. $component =$step->addComponent("thumbnail_upload", new WFileUploadField());
  132. $component =$step->addComponent("thumbnail_mime_type", new WTextField());
  133. $component->setValue($parts['THUMBNAIL_MIME_TYPE']->getValue());
  134. $component =$step->addComponent("use_custom_thumbnail_type", new WCheckBox());
  135. $component->setValue(false);
  136. // Thumbnail dimensions
  137. $thumDim = $parts['THUMBNAIL_DIMENSIONS']->getValue();
  138. $component =$step->addComponent("thumbnail_height", $dimensionComponent->shallowCopy());
  139. if ($thumDim[1])
  140. $component->setValue($thumDim[1].'px');
  141. $component =$step->addComponent("use_custom_thumbnail_height", new WCheckBox());
  142. $component->setValue(false);
  143. $component =$step->addComponent("thumbnail_width", $dimensionComponent->shallowCopy());
  144. if ($thumDim[0])
  145. $component->setValue($thumDim[0].'px');
  146. $component =$step->addComponent("use_custom_thumbnail_width", new WCheckBox());
  147. $component->setValue(false);
  148. print "\n<p>"._("Change properties of the uploaded file to custom values:");
  149. print "\n<table border='1'>";
  150. print "\n<tr>";
  151. print "\n\t<th>";
  152. print "\n\t\t"._("Property")."";
  153. print "\n\t</th>";
  154. print "\n\t<th>";
  155. print "\n\t\t"._("Use Custom Value")."";
  156. print "\n\t</th>";
  157. print "\n\t<th>";
  158. print "\n\t\t"._("Custom Value")."";
  159. print "\n\t</th>";
  160. print "\n</tr>";
  161. print "\n<tr>";
  162. print "\n\t<td>";
  163. print "\n\t\t"._("File Name")."";
  164. print "\n\t</td>";
  165. print "\n\t<td align='center'>";
  166. print "\n\t\t[[use_custom_filename]]";
  167. print "\n\t</td>";
  168. print "\n\t<td>";
  169. print "\n\t\t[[file_name]]";
  170. print "\n\t</td>";
  171. print "\n</tr>";
  172. print "\n<tr>";
  173. print "\n\t<td>";
  174. print "\n\t\t"._("File Size")."";
  175. print "\n\t</td>";
  176. print "\n\t<td align='center'>";
  177. // print "\n\t\t[[size_from_file]]";
  178. print "\n\t</td>";
  179. print "\n\t<td>";
  180. print "\n\t\t[[file_size]]";
  181. print "\n\t</td>";
  182. print "\n</tr>";
  183. print "\n<tr>";
  184. print "\n\t<td>";
  185. print "\n\t\t"._("Mime Type")."";
  186. print "\n\t</td>";
  187. print "\n\t<td align='center'>";
  188. print "\n\t\t[[use_custom_type]]";
  189. print "\n\t</td>";
  190. print "\n\t<td>";
  191. print "\n\t\t[[mime_type]]";
  192. print "\n\t</td>";
  193. print "\n</tr>";
  194. print "\n<tr>";
  195. print "\n\t<td>";
  196. print "\n\t\t"._("Width")."";
  197. print "\n\t</td>";
  198. print "\n\t<td align='center'>";
  199. print "\n\t\t[[use_custom_width]]";
  200. print "\n\t</td>";
  201. print "\n\t<td>";
  202. print "\n\t\t[[width]]";
  203. print "\n\t</td>";
  204. print "\n</tr>";
  205. print "\n<tr>";
  206. print "\n\t<td>";
  207. print "\n\t\t"._("Height")."";
  208. print "\n\t</td>";
  209. print "\n\t<td align='center'>";
  210. print "\n\t\t[[use_custom_height]]";
  211. print "\n\t</td>";
  212. print "\n\t<td>";
  213. print "\n\t\t[[height]]";
  214. print "\n\t</td>";
  215. print "\n</tr>";
  216. print "\n<tr>";
  217. print "\n\t<td>";
  218. print "\n\t\t"._("Thumbnail")."";
  219. print "\n\t</td>";
  220. print "\n\t<td align='center'>";
  221. print "\n\t\t &nbsp; ";
  222. print "\n\t</td>";
  223. print "\n\t<td>";
  224. print "\n[[thumbnail_upload]]";
  225. print "\n\t</td>";
  226. print "\n</tr>";
  227. print "\n<tr>";
  228. print "\n\t<td>";
  229. print "\n\t\t"._("Thumbnail Mime Type")."";
  230. print "\n\t</td>";
  231. print "\n\t<td align='center'>";
  232. print "\n\t\t[[use_custom_thumbnail_type]]";
  233. print "\n\t</td>";
  234. print "\n\t<td>";
  235. print "\n\t\t[[thumbnail_mime_type]]";
  236. print "\n\t</td>";
  237. print "\n</tr>";
  238. print "\n<tr>";
  239. print "\n\t<td>";
  240. print "\n\t\t"._("Thumbnail Width")."";
  241. print "\n\t</td>";
  242. print "\n\t<td align='center'>";
  243. print "\n\t\t[[use_custom_thumbnail_width]]";
  244. print "\n\t</td>";
  245. print "\n\t<td>";
  246. print "\n\t\t[[thumbnail_width]]";
  247. print "\n\t</td>";
  248. print "\n</tr>";
  249. print "\n<tr>";
  250. print "\n\t<td>";
  251. print "\n\t\t"._("Thumbnail Height")."";
  252. print "\n\t</td>";
  253. print "\n\t<td align='center'>";
  254. print "\n\t\t[[use_custom_thumbnail_height]]";
  255. print "\n\t</td>";
  256. print "\n\t<td>";
  257. print "\n\t\t[[thumbnail_height]]";
  258. print "\n\t</td>";
  259. print "\n</tr>";
  260. print "\n</table>";
  261. print "\n</p>";
  262. $step->setContent(ob_get_contents());
  263. ob_end_clean();
  264. }
  265. /**
  266. * Get the values submitted in the wizard and update the Record with them.
  267. *
  268. * @param object $record
  269. * @param object $wizard
  270. * @return void
  271. * @access public
  272. * @since 10/19/04
  273. */
  274. function updateFromWizard ( $record, $wizard ) {
  275. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  276. ArgumentValidator::validate($wizard, new ExtendsValidatorRule("Wizard"));
  277. $properties =$wizard->getAllValues();
  278. $values =$properties["record"];
  279. printpre($properties);
  280. // Get all the parts
  281. $partIterator =$record->getParts();
  282. $parts = array();
  283. while($partIterator->hasNext()) {
  284. $part =$partIterator->next();
  285. $partStructure =$part->getPartStructure();
  286. $partStructureId =$partStructure->getId();
  287. $parts[$partStructureId->getIdString()] =$part;
  288. }
  289.  
  290. // if a new File was uploaded, store it.
  291. if ($values['file_upload']['tmp_name']
  292. && $values['file_upload']['name'])
  293. {
  294. $name = $values['file_upload']['name'];
  295. $tmpName = $values['file_upload']['tmp_name'];
  296. $mimeType = $values['file_upload']['type'];
  297. // If we weren't passed a mime type or were passed the generic
  298. // application/octet-stream type, see if we can figure out the
  299. // type.
  300. if (!$mimeType || $mimeType == 'application/octet-stream') {
  301. $mime = Services::getService("MIME");
  302. $mimeType = $mime->getMimeTypeForFileName($name);
  303. }
  304. $parts['FILE_DATA']->updateValue(file_get_contents($tmpName));
  305. $parts['FILE_NAME']->updateValue($name);
  306. $parts['MIME_TYPE']->updateValue($mimeType);
  307. }
  308. // If we've uploaded a thumbnail, safe it.
  309. if ($values['thumbnail_upload']['tmp_name']
  310. && $values['thumbnail_upload']['name'])
  311. {
  312. $name = $values['thumbnail_upload']['name'];
  313. $tmpName = $values['thumbnail_upload']['tmp_name'];
  314. $mimeType = $values['thumbnail_upload']['type'];
  315. // If we weren't passed a mime type or were passed the generic
  316. // application/octet-stream type, see if we can figure out the
  317. // type.
  318. if (!$mimeType || $mimeType == 'application/octet-stream') {
  319. $mime = Services::getService("MIME");
  320. $mimeType = $mime->getMimeTypeForFileName($name);
  321. }
  322. $parts['THUMBNAIL_DATA']->updateValue(file_get_contents($tmpName));
  323. $parts['THUMBNAIL_MIME_TYPE']->updateValue($mimeType);
  324. }
  325. // otherwise, if we've uploaded a new file only, get rid of the
  326. // old one and try to create a new one
  327. else if ($values['file_upload']['tmp_name']
  328. && $values['file_upload']['name'])
  329. {
  330. $imageProcessor = Services::getService("ImageProcessor");
  331. // If our image format is supported by the image processor,
  332. // generate a thumbnail.
  333. if ($imageProcessor->isFormatSupported($mimeType)) {
  334. $parts['THUMBNAIL_DATA']->updateValue(
  335. $imageProcessor->generateThumbnailData($mimeType,
  336. file_get_contents($tmpName)));
  337. $parts['THUMBNAIL_MIME_TYPE']->updateValue($imageProcessor->getThumbnailFormat());
  338. }
  339. // just make our thumbnail values empty. Default icons will display
  340. // instead.
  341. else {
  342. $parts['THUMBNAIL_DATA']->updateValue("");
  343. $parts['THUMBNAIL_MIME_TYPE']->updateValue("NULL");
  344. }
  345. }
  346. // if the "use custom" box was checked store the name.
  347. if ($values['use_custom_filename']) {
  348. $parts['FILE_NAME']->updateValue($values['file_name']);
  349. }
  350. // if the "use custom" box was checked store the mime type.
  351. if ($values['use_custom_type']) {
  352. $parts['MIME_TYPE']->updateValue($values['mime_type']);
  353. }
  354. // if the "use custom" box was checked store the height.
  355. if ($values['use_custom_height']
  356. && ereg("^([0-9]+)px$", $values['height'], $matches))
  357. {
  358. $dimArray = $parts['DIMENSIONS']->getValue();
  359. $dimArray[1] = $matches[1];
  360. print "Setting DIMENSIONS to:"; printpre($dimArray);
  361. $parts['DIMENSIONS']->updateValue($dimArray);
  362. }
  363. unset($dimArray, $matches);
  364. // if the "use custom" box was checked store the width.
  365. if ($values['use_custom_width']
  366. && ereg("^([0-9]+)px$", $values['width'], $matches))
  367. {
  368. $dimArray = $parts['DIMENSIONS']->getValue();
  369. $dimArray[0] = $matches[1];
  370. print "Setting DIMENSIONS to:"; printpre($dimArray);
  371. $parts['DIMENSIONS']->updateValue($dimArray);
  372. }
  373. unset($dimArray, $matches);
  374. // if the "use custom" box was checked store the height.
  375. if ($values['use_custom_thumbnail_height']
  376. && ereg("^([0-9]+)px$", $values['thumbnail_height'], $matches))
  377. {
  378. $dimArray = $parts['THUMBNAIL_DIMENSIONS']->getValue();
  379. $dimArray[1] = $matches[1];
  380. print "Setting THUMBNAIL_DIMENSIONS to:"; printpre($dimArray);
  381. $parts['THUMBNAIL_DIMENSIONS']->updateValue($dimArray);
  382. }
  383. unset($dimArray, $matches);
  384. // if the "use custom" box was checked store the width.
  385. if ($values['use_custom_thumbnail_width']
  386. && ereg("^([0-9]+)px$", $values['thumbnail_width'], $matches))
  387. {
  388. $dimArray = $parts['THUMBNAIL_DIMENSIONS']->getValue();
  389. $dimArray[0] = $matches[1];
  390. print "Setting THUMBNAIL_DIMENSIONS to:"; printpre($dimArray);
  391. $parts['THUMBNAIL_DIMENSIONS']->updateValue($dimArray);
  392. }
  393. unset($dimArray, $matches);
  394. }
  395.  
  396. /**
  397. * Generate HTML for displaying the Record
  398. *
  399. * @param object $record
  400. * @return string
  401. * @access public
  402. * @since 10/19/04
  403. */
  404. function generateDisplay ( $repositoryId, $assetId, $record ) {
  405. ArgumentValidator::validate($assetId, new ExtendsValidatorRule("Id"));
  406. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  407. // Get all the partstructures
  408. $recordStructure =$record->getRecordStructure();
  409. $partStructureIterator =$recordStructure->getPartStructures();
  410. $partStructures = array();
  411. while($partStructureIterator->hasNext()) {
  412. $partStructures[] =$partStructureIterator->next();
  413. }
  414. return $this->generateDisplayForParts($repositoryId, $assetId, $record, $partStructures);
  415. }
  416.  
  417. /**
  418. * Generate HTML for displaying particular parts of the Record
  419. *
  420. * @param object $record The record to print.
  421. * @param array $partStructures An array of particular partStructures to print.
  422. * @return string
  423. * @access public
  424. * @since 10/19/04
  425. */
  426. function generateDisplayForPartStructures ( $repositoryId, $assetId, $record, $partStructures ) {
  427. ArgumentValidator::validate($repositoryId, new ExtendsValidatorRule("Id"));
  428. ArgumentValidator::validate($assetId, new ExtendsValidatorRule("Id"));
  429. ArgumentValidator::validate($record, new ExtendsValidatorRule("RecordInterface"));
  430. ArgumentValidator::validate($partStructures, new ArrayValidatorRuleWithRule(new ExtendsValidatorRule("PartStructure")));
  431. $partIterator =$record->getParts();
  432. $parts = array();
  433. while($partIterator->hasNext()) {
  434. $part =$partIterator->next();
  435. $partStructure =$part->getPartStructure();
  436. $partStructureId =$partStructure->getId();
  437. if (!isset($parts[$partStructureId->getIdString()]) || !is_array($parts[$partStructureId->getIdString()]))
  438. $parts[$partStructureId->getIdString()] = array();
  439. $parts[$partStructureId->getIdString()][] =$part;
  440. }
  441. // print out the parts;
  442. ob_start();
  443. $partStructuresToSkip = array ('FILE_DATA', 'THUMBNAIL_DATA',
  444. 'THUMBNAIL_MIME_TYPE', 'THUMBNAIL_DIMENSIONS');
  445. $printThumbnail = FALSE;
  446. foreach (array_keys($partStructures) as $key) {
  447. $partStructure =$partStructures[$key];
  448. $partStructureId =$partStructure->getId();
  449. if(!in_array($partStructureId->getIdString(), $partStructuresToSkip)){
  450. print "\n<strong>".$partStructure->getDisplayName().":</strong> \n";
  451. switch ($partStructureId->getIdString()) {
  452. case 'FILE_SIZE':
  453. $size = ByteSize::withValue($parts[$partStructureId->getIdString()][0]->getValue());
  454. print $size->asString();
  455. break;
  456. case 'DIMENSIONS':
  457. $dimensionArray = $parts[$partStructureId->getIdString()][0]->getValue();
  458. print "<em>"._('width: ')."</em>".$dimensionArray[0].'px<em>;</em> ';
  459. print "<em>"._('height: ')."</em>".$dimensionArray[1].'px';
  460. break;
  461. default:
  462. print $parts[$partStructureId->getIdString()][0]->getValue();
  463. }
  464. print "\n<br />";
  465. }
  466. // If we've specified that we want the data, or part of the thumb,
  467. // print the tumb.
  468. else {
  469. $printThumbnail = TRUE;
  470. }
  471. }
  472.  
  473. $html = ob_get_clean();
  474. $harmoni = Harmoni::instance();
  475. $harmoni->request->startNamespace("polyphony-repository");
  476. if ($printThumbnail) {
  477. ob_start();
  478. $recordId =$record->getId();
  479. $ns = $harmoni->request->endNamespace();
  480. // ======= VIEWER LINK ======== //
  481. $xmlAssetIdString = $harmoni->request->get("asset_id");
  482. print "<a href='#' onclick='Javascript:window.open(";
  483. print '"'.VIEWER_URL."?&amp;source=";
  484. print urlencode($harmoni->request->quickURL("asset", "browserecordxml",
  485. array("collection_id" => $repositoryId->getIdString(),
  486. "asset_id" => $xmlAssetIdString,
  487. "record_id" => $recordId->getIdString(),
  488. RequestContext::name("limit_by") => RequestContext::value("limit_by"),
  489. RequestContext::name("type") => RequestContext::value("type"),
  490. RequestContext::name("searchtype") => RequestContext::value("searchtype"),
  491. RequestContext::name("searchstring") => RequestContext::value("searchstring"))));
  492. print '&amp;start=0", ';
  493. print '"'.preg_replace("/[^a-z0-9]/i", '_', $assetId->getIdString()).'", ';
  494. print '"toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=500"';
  495. print ")'>";
  496. $harmoni->request->startNamespace($ns);
  497. // If we have a thumbnail with a valid mime type, print a link to that.
  498. $thumbnailName = ereg_replace("\.[^\.]+$", "",
  499. $parts['FILE_NAME'][0]->getValue());
  500. if ($thumbnailMimeType = $parts['THUMBNAIL_MIME_TYPE'][0]->getValue()) {
  501. $mime = Services::getService("MIME");
  502. $thumbnailName .= ".".$mime->getExtensionForMIMEType($thumbnailMimeType);
  503. }
  504. print "\n<img src='";
  505. print RepositoryInputOutputModuleManager::getThumbnailUrlForRecord($assetId, $record);
  506. print "'";
  507. print " style='border: 0px;'";
  508. print " alt='Thumbnail image.'";
  509. print " align='left'";
  510. print " />";
  511. print "</a> <br />";
  512. $html2 = ob_get_clean();
  513.  
  514. ob_start();
  515. print "\n<a href='";
  516. print RepositoryInputOutputModuleManager::getFileUrlForRecord($assetId, $record);
  517. print "' target='_blank'>";
  518.  
  519. print "Download This File</a>\n";
  520. $downloadlink = ob_get_clean();
  521. $html = "<table border=0><tr><td>".$html2."</td><td>".$html.$downloadlink."</td></tr></table>";
  522. }
  523. $harmoni->request->endNamespace();
  524. return $html;
  525. }
  526. }
  527.  
  528. ?>

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