Source for file PartAndValuesModule.class.php

Documentation is available at PartAndValuesModule.class.php

  1. <?php
  2. /**
  3. * @package polyphony.repository.search
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: PartAndValuesModule.class.php,v 1.7 2007/09/19 14:04:49 adamfranco Exp $
  9. */
  10.  
  11. /**
  12. * Search Modules generate forms for and collect/format the subitions of said forms
  13. * for various Digital Repository search types.
  14. *
  15. * @package polyphony.repository.search
  16. *
  17. * @copyright Copyright &copy; 2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  19. *
  20. * @version $Id: PartAndValuesModule.class.php,v 1.7 2007/09/19 14:04:49 adamfranco Exp $
  21. */
  22.  
  23. class PartAndValuesModule {
  24. /**
  25. * Constructor
  26. *
  27. * @param string $fieldName
  28. * @return object
  29. * @access public
  30. * @since 04/25/06
  31. */
  32. function PartAndValuesModule ( $partStructFieldName, $valueFieldName ) {
  33. $this->_partStructFieldName = $partStructFieldName;
  34. $this->_valueFieldName = $valueFieldName;
  35. $this->_initilaized = false;
  36. }
  37. /**
  38. * Initialize this object
  39. *
  40. * @return void
  41. * @access public
  42. * @since 5/15/06
  43. */
  44. function init () {
  45. if (!$this->_initilaized) {
  46. $harmoni = Harmoni::instance();
  47. $harmoni->request->startNamespace('PartAndValuesModule');
  48. $this->_contextPartStructFieldName = RequestContext::name($this->_partStructFieldName);
  49. $this->_contextValueFieldName = RequestContext::name($this->_valueFieldName);
  50. if (RequestContext::value($this->_partStructFieldName)) {
  51. $this->_partStruct = RequestContext::value($this->_partStructFieldName);
  52. } else {
  53. $this->_partStruct = null;
  54. }
  55. if (RequestContext::value($this->_valueFieldName))
  56. $this->_value = RequestContext::value($this->_valueFieldName);
  57. else
  58. $this->_value = null;
  59. $harmoni->request->endNamespace();
  60. $this->_initilaized = true;
  61. }
  62. }
  63. /**
  64. * Create a form for searching.
  65. *
  66. * @param string $action The destination on form submit.
  67. * @return string
  68. * @access public
  69. * @since 04/25/06
  70. */
  71. function createSearchForm ($repository, $action ) {
  72. ob_start();
  73. print "<form action='$action' method='post'>\n<div>\n";
  74. $this->createSearchFields($repository);
  75. print "\t<input type='submit' />\n";
  76. print "</div>\n</form>";
  77. return ob_get_clean();
  78. }
  79. /**
  80. * Create the fields (without form tags) for searching
  81. *
  82. * @param object Repository $repository
  83. * @return string
  84. * @access public
  85. * @since 4/26/06
  86. */
  87. function createSearchFields ($repository) {
  88. $this->init();
  89. ob_start();
  90. print "\n\t\t<select name='".$this->_contextPartStructFieldName."'";
  91. print " onchange='this.form.submit()'>";
  92. $idManager = Services::getService("Id");
  93. if ($this->_partStruct) {
  94. $idStrings = explode("_____", $this->_partStruct);
  95. $selectedRecordStructId =$idManager->getId($idStrings[0]);
  96. $selectedPartStructId =$idManager->getId($idStrings[1]);
  97. }
  98. $setManager = Services::getService("Sets");
  99. $recordStructSet =$setManager->getPersistentSet($repository->getId());
  100. $recordStructSet->reset();
  101. $recordStructHeadingPrinted = FALSE;
  102. while ($recordStructSet->hasNext()) {
  103. // Close the record structure group if needed
  104. if ($recordStructHeadingPrinted)
  105. print "\n\t\t\t</optgroup>";
  106. $recordStructHeadingPrinted = FALSE;
  107. $recordStruct =$repository->getRecordStructure($recordStructSet->next());
  108. $recordStructId =$recordStruct->getId();
  109. $partStructSet =$setManager->getPersistentSet($recordStruct->getId());
  110. while ($partStructSet->hasNext()) {
  111. $partStruct =$recordStruct->getPartStructure($partStructSet->next());
  112. // If the $partStruct has Authoritative values, add them to our menu
  113. $authoritativeValues =$partStruct->getAuthoritativeValues();
  114. if ($authoritativeValues->hasNext()) {
  115. // Print a heading for the record structure if it isn't printed
  116. // yet.
  117. if (!$recordStructHeadingPrinted) {
  118. print "\n\t\t\t<optgroup label='";
  119. print $recordStruct->getDisplayName();
  120. print "'>";
  121. $recordStructHeadingPrinted = TRUE;
  122. }
  123. $partStructId =$partStruct->getId();
  124. print "\n\t\t\t\t<option ";
  125. print " value='";
  126. print $recordStructId->getIdString();
  127. print "_____";
  128. print $partStructId->getIdString();
  129. print "'";
  130. // Make sure that we keep either the selected part Structure
  131. // or the first one around for populating the Authority List
  132. if (isset($selectedPartStructId)) {
  133. if ($selectedPartStructId->isEqual($partStructId)) {
  134. $selectedPartStruct =$partStruct;
  135. print " selected='selected'";
  136. }
  137. } else if (!isset($selectedPartStruct)) {
  138. $selectedPartStruct =$partStruct;
  139. print " selected='selected'";
  140. }
  141. print ">";
  142. print $partStruct->getDisplayName();
  143. print "</option>";
  144. }
  145. }
  146. }
  147. if (!isset($selectedPartStruct)) {
  148. print "\n\t\t\t<option value=''>"._("No authoritative values are defined.")."</option>";
  149. }
  150. print "\n\t\t</select>";
  151. if (isset($selectedPartStruct)) {
  152. print "\n\t\t<select name='".$this->_contextValueFieldName."'>";
  153. print "\n\t\t\t<option value=''>"._("Please select a value...")."</option>";
  154. $authoritativeValues =$selectedPartStruct->getAuthoritativeValues();
  155. while ($authoritativeValues->hasNext()) {
  156. $value =$authoritativeValues->next();
  157. print "\n\t\t\t<option";
  158. print " value='".urlencode($value->asString())."'";
  159. if ($this->_value == urlencode($value->asString()))
  160. print " selected='selected'";
  161. print ">".$value->asString()."</option>";
  162. }
  163. print "\n\t\t\t<option value='__NonMatching__'";
  164. if ($this->_value == '__NonMatching__')
  165. print " selected='selected'";
  166. print ">"._("* Non-Matching *")."</option>";
  167. print "\n\t\t</select>";
  168. }
  169. return ob_get_clean();
  170. }
  171. /**
  172. * Get the formatted search terms based on the submissions of the form
  173. *
  174. * @param object Repository $repository
  175. * @return mixed
  176. * @access public
  177. * @since 04/25/06
  178. */
  179. function getSearchCriteria ( $repository ) {
  180. $this->init();
  181. if ($this->_partStruct
  182. && $this->_value)
  183. {
  184. $idManager = Services::getService("Id");
  185. $idStrings = explode("_____", $this->_partStruct);
  186. $selectedRecordStructId =$idManager->getId($idStrings[0]);
  187. $selectedPartStructId =$idManager->getId($idStrings[1]);
  188. $recordStruct =$repository->getRecordStructure($selectedRecordStructId);
  189. $partStruct =$recordStruct->getPartStructure($selectedPartStructId);
  190. $value =$partStruct->createValueObjectFromString(
  191. urldecode($this->_value));
  192. return array (
  193. 'RecordStructureId' => $selectedRecordStructId,
  194. 'PartStructureId' => $selectedPartStructId,
  195. 'AuthoritativeValue' => $value);
  196. } else {
  197. return false;
  198. }
  199. }
  200. /**
  201. * Get an array of the current values to be added to a url. The keys of the
  202. * arrays are the field-names in the appropriate context.
  203. *
  204. * @return array
  205. * @access public
  206. * @since 04/25/06
  207. */
  208. function getCurrentValues () {
  209. $this->init();
  210. if ($this->_partStruct) {
  211. $values = array(
  212. $this->_contextPartStructFieldName => $this->_partStruct,
  213. $this->_contextValueFieldName => $this->_value);
  214. } else
  215. $values = array();
  216. return $values;
  217. }
  218. /**
  219. * Update the current values with data (maybe stored in the session for instance.
  220. * The keys of the arrays are the field-names in the appropriate context.
  221. * This could have been originally fetched via getCurrentValues
  222. *
  223. * @param array $values
  224. * @return void
  225. * @access public
  226. * @since 04/25/06
  227. */
  228. function setCurrentValues ($values) {
  229. $this->init();
  230. if (isset($values[$this->_contextPartStructFieldName])) {
  231. $this->_partStruct = $values[$this->_contextPartStructFieldName];
  232. $this->_value = $values[$this->_contextValueFieldName];
  233. }
  234. }
  235. }
  236.  
  237. ?>

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