Source for file WAgentBrowser.class.php

Documentation is available at WAgentBrowser.class.php

  1. <?php
  2. /**
  3. * @since Jul 19, 2005
  4. * @package polyphony.wizard.components
  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: WAgentBrowser.class.php,v 1.5 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. require_once(POLYPHONY."/main/library/ResultPrinter/EmbeddedArrayResultPrinter.class.php");
  13.  
  14. /**
  15. * This component allows you to search the agents in the system and select a number of them for some action.
  16. *
  17. * @since Jul 19, 2005
  18. * @package polyphony.wizard.components
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: WAgentBrowser.class.php,v 1.5 2007/09/19 14:04:51 adamfranco Exp $
  24. */
  25. class WAgentBrowser
  26. extends WizardComponent
  27. {
  28. var $_agentsSelected = array();
  29. var $_searchResults = array();
  30. var $_resultPrinter = null;
  31. var $_searchField = null;
  32. var $_searchButton = null;
  33. var $_searchTypeSelector = null;
  34. var $_oneType = false;
  35. var $_options = array();
  36. var $_actionSelected = "nop";
  37. function WAgentBrowser ()
  38. {
  39. $this->_searchField = new WTextField();
  40. $this->_searchField->setSize(20);
  41. $this->_searchButton = WEventButton::withLabel(dgettext("polyphony", "Go"));
  42. $agentManager = Services::getService("Agent");
  43. $searchTypes =$agentManager->getAgentSearchTypes();
  44. $this->_searchTypeSelector = new WSelectList();
  45. $count = 0;
  46. while($searchTypes->hasNext()) {
  47. $type =$searchTypes->next();
  48. $this->_searchTypeSelector->addOption(urlencode(Type::typeToString($type)), Type::typeToString($type));
  49. $this->_searchTypeSelector->setValue(urlencode(Type::typeToString($type)));
  50. $count++;
  51. }
  52. if ($count < 2) $this->_oneType = true;
  53. $this->_searchField->setParent($this);
  54. $this->_searchButton->setParent($this);
  55. $this->_searchTypeSelector->setParent($this);
  56. }
  57. /**
  58. * Resets the search results and the selected agents.
  59. *
  60. * @return void
  61. ***/
  62. function reset()
  63. {
  64. $this->_agentsSelected = array();
  65. $this->_searchResults = array();
  66. $this->_resultPrinter = null;
  67. }
  68. /**
  69. * Adds an action to the list of actions presented to the end user.
  70. *
  71. * @return void
  72. ***/
  73. function addActionOption($action_name, $label)
  74. {
  75. $this->_options[$action_name] = $label;
  76. }
  77. /**
  78. * Tells the wizard component to update itself - this may include getting
  79. * form post data or validation - whatever this particular component wants to
  80. * do every pageload.
  81. * @param string $fieldName The field name to use when outputting form data or
  82. * similar parameters/information.
  83. * @access public
  84. * @return boolean - TRUE if everything is OK
  85. */
  86. function update ($fieldName) {
  87. $this->_searchField->update($fieldName."_query");
  88. $this->_searchButton->update($fieldName."_go");
  89. // check if we have any new checked agents to add to our list.
  90. $values = RequestContext::value($fieldName."_checked");
  91. if (is_array($values)) {
  92. // remove the dummy '' value.
  93. $key = array_search('', $values);
  94. if ($key !== false) unset($values[$key]);
  95. $this->_agentsSelected = array_unique($values);
  96. }
  97. // check if the user selected an action
  98. $action = RequestContext::value($fieldName."_action");
  99. if ($action) $this->_actionSelected = $action;
  100. // perform the search, if necessary.
  101. if ($this->_searchButton->getAllValues()) {
  102. $query = $this->_searchField->getAllValues();
  103. $type = Type::fromString(urldecode($this->_searchTypeSelector->getAllValues()));
  104. $agentManager = Services::getService("Agent");
  105. $list =$agentManager->getAgentsBySearch($query, $type);
  106. $this->_searchResults = array(); // clear the array
  107. $this->_resultPrinter = null;
  108. while ($list->hasNext()) {
  109. $this->_searchResults[] =$list->next();
  110. }
  111. $this->_resultPrinter = new AgentBrowserResultPrinter($this->_searchResults, 1, 10, array("WAgentBrowser", "printAgent"), $fieldName);
  112. $this->_resultPrinter->overridePageNumber(1);
  113. $this->_resultPrinter->setOptions($this->_options);
  114. }
  115. $this->_resultPrinter->_selected = $this->_agentsSelected;
  116. }
  117.  
  118. /**
  119. * Returns the values of wizard-components. Should return an array if children are involved,
  120. * otherwise a whatever type of object is expected.
  121. *
  122. * Returns an array, 0=>action selected, 1=>array(of agents selected)
  123. * @access public
  124. * @return mixed
  125. */
  126. function getAllValues () {
  127. $action = $this->_actionSelected;
  128. $this->_actionSelected = "nop";
  129. return array($action, $this->_agentsSelected);
  130. }
  131.  
  132. /**
  133. * Returns a block of XHTML-valid code that contains markup for this specific
  134. * component.
  135. * @param string $fieldName The field name to use when outputting form data or
  136. * similar parameters/information.
  137. * @access public
  138. * @return string
  139. */
  140. function getMarkup ($fieldName) {
  141. // here we will do a couple things:
  142. // 1) display a search field in which people can enter information and search for users
  143. // 2) take the search results (maybe hundreds of them) and display them in some fashion
  144. // 3) display a list of the currently selected agents (cached)
  145. $srchFieldName = $fieldName . "_query";
  146. $td = textdomain("polyphony");
  147. $m = _("Search agents: ");
  148. $m .= $this->_searchField->getMarkup($srchFieldName);
  149. if (!$this->_oneType) $m .= $this->_searchTypeSelector->getMarkup($fieldName."_type");
  150. $m .= $this->_searchButton->getMarkup($fieldName."_go");
  151. $name = RequestContext::name($fieldName."_checked[]");
  152. $m .= "<input type='hidden' name='$name' value=''/>";
  153. $m .= "<br/>\n";
  154. if (count($this->_searchResults) || count($this->_agentsSelected)) {
  155. $m .= $this->_resultPrinter->getMarkup();
  156. $this->_resultPrinter->overridePageNumber(-1); // reset;
  157. }
  158.  
  159. textdomain($td);
  160. return $m;
  161. }
  162. /**
  163. * Callback function for the HTMLArrayResultPrinter.
  164. *
  165. * @return string
  166. * @access public
  167. * @static
  168. ***/
  169. function printAgent($agent)
  170. {
  171. $m = '';
  172. $m .= $agent->getDisplayName();
  173. return $m;
  174. }
  175. }
  176.  
  177. /**
  178. * A result printer for the {@link WAgentBrowser}.
  179. *
  180. * @package polyphony.wizard.components
  181. * @copyright Copyright &copy; 2005, Middlebury College
  182. * @author Gabriel Schine
  183. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  184. * @version $Id: WAgentBrowser.class.php,v 1.5 2007/09/19 14:04:51 adamfranco Exp $
  185. */
  186. class AgentBrowserResultPrinter
  187. extends EmbeddedArrayResultPrinter
  188. {
  189. var $_colors;
  190. var $_currColor = 0;
  191. var $_fieldName;
  192. var $_selected = array();
  193. var $_options = array();
  194. function AgentBrowserResultPrinter( $array, $numColumns,
  195. $numResultsPerPage, $callbackFunction, $fieldName) {
  196. parent::EmbeddedArrayResultPrinter($array, $numColumns, $numResultsPerPage, $callbackFunction);
  197. $this->_colors = array("#aaa", "#ccc");
  198. $this->setTDStyle("background-color: ".$this->_colors[0]."; padding: 2px;");
  199. $this->_fieldName = $fieldName;
  200. }
  201. function setOptions($options) {
  202. $this->_options = $options;
  203. }
  204. function createTRElement() {
  205. $tr = parent::createTRElement();
  206. $this->_currColor = 1-$this->_currColor;
  207. $this->setTDStyle("background-color: ".$this->_colors[$this->_currColor]."; padding: 2px;");
  208. return $tr;
  209. }
  210. function createItemElement($content, $checked = false) {
  211. $name = RequestContext::name($this->_fieldName . "_checked");
  212. $currItemId =$this->_currentItem->getId();
  213. $id = urlencode($currItemId->getIdString());
  214. $chk = $checked?" checked":"";
  215. $content = "<input type='checkbox' name='".$name."[]' value='$id'$chk />\n" . $content;
  216. return parent::createItemElement($content);
  217. }
  218. function createHeaderRow() {
  219. $resultsText = dgettext("polyphony", "Search results");
  220. return $this->createTRElement().$this->createTDElement(
  221. "<input type='hidden' name='".RequestContext::name("starting_number")."' id='starting_number' value='".RequestContext::value("starting_number")."' /><b>$resultsText:</b>", $this->_numColumns) .
  222. "</tr>";
  223. }
  224. function createFooterRow() {
  225. $m = '';
  226. if (count($this->_selected)) {
  227. $agents = Services::getService("Agent");
  228. $ids = Services::getService("Id");
  229. // print out the existing agents.
  230. $m .= $this->createTRElement().$this->createTDElement("<b>".dgettext("polyphony", "Already selected").":</b>")."</tr>\n";
  231. foreach($this->_selected as $id) {
  232. $agent = $agents->getAgent($ids->getId($id));
  233. $this->_currentItem =$agent;
  234. $itemArray = array ($agent);
  235. $params = array_merge($itemArray, $this->_callbackParams);
  236. $itemMarkup = call_user_func_array(
  237. $this->_callbackFunction, $params);
  238. $m .= $this->createTRElement().$this->createItemElement($itemMarkup, true)."</tr>\n";
  239. }
  240. }
  241. $fieldID = $this->_fieldName."_action";
  242. $name = RequestContext::name($fieldID);
  243. $tdContent = "
  244. With selected: <select name='$name' id='$fieldID' onchange='if (this.value != \"nop\") submitWizard(this.form)'>\n";
  245. $tdContent .= "<option value='nop'>".dgettext("polyphony", "choose action...")."</option>\n";
  246. foreach ($this->_options as $name=>$label) {
  247. $tdContent .= "<option value='$name'>$label</option>\n";
  248. }
  249. $tdContent .= "</select>";
  250. $m .= $this->createTRElement() . $this->createTDElement($tdContent)."</tr>";
  251. return $m;
  252. }
  253. function createPageLinks ($numItems, $startingNumber)
  254. {
  255. $harmoni = Harmoni::instance();
  256. ob_start();
  257. $numPages = ceil($numItems/$this->_pageSize);
  258. $currentPage = floor($startingNumber/$this->_pageSize)+1; // add one for 1-based counting
  259. for ($i=1; $i<=$numPages; $i++) {
  260. if ($i > 0 && ($i+1) % 10 == 0)
  261. print "<br />";
  262. print " ";
  263. if ($i != $currentPage) {
  264. $url =$harmoni->request->mkURLWithPassthrough();
  265. $url->setValue("starting_number", (($i-1)*$this->_pageSize+1));
  266. print "<a href='#' onclick='getWizardElement(\"starting_number\").value = \"".(($i-1)*$this->_pageSize+1)."\"; submitWizard(getWizardElement(\"starting_number\").form);'>";
  267. }
  268. print $i;
  269. if ($i != $currentPage)
  270. print "</a>";
  271. }
  272.  
  273. // Add the links to the page
  274. $pageLinks = ob_get_contents();
  275. ob_end_clean();
  276.  
  277. return $pageLinks;
  278. }
  279. ###
  280.  
  281. } // END class AgentBrowserResultPrinter
  282.  
  283.  
  284. ?>

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