/gadgets/Search/Actions/Search.php

https://github.com/jaws-project/jaws · PHP · 171 lines · 118 code · 20 blank · 33 comment · 13 complexity · 1329abecf270e97c7d02c5c8bdf69815 MD5 · raw file

  1. <?php
  2. /**
  3. * Search boxes actions
  4. *
  5. * @category GadgetLayout
  6. * @package Search
  7. * @author Jonathan Hernandez <ion@suavizado.com>
  8. * @author Ali Fazelzadeh <afz@php.net>
  9. * @copyright 2004-2022 Jaws Development Group
  10. * @license http://www.gnu.org/copyleft/lesser.html
  11. */
  12. class Search_Actions_Search extends Jaws_Gadget_Action
  13. {
  14. /**
  15. * Builds the search box
  16. *
  17. * @access public
  18. * @param bool $gadgets_combo Display gadgets combo (optional, default true)
  19. * @return string XHTML search box
  20. */
  21. function Box($gadgets_combo = true)
  22. {
  23. $post = $this->gadget->request->fetch(
  24. array('all', 'exact', 'least', 'exclude', 'gadgets', 'date'),
  25. 'get',
  26. false
  27. );
  28. if ($this->app->requestedActionMode === 'normal') {
  29. $tFilename = 'Search.html';
  30. } else {
  31. $tFilename = 'Search0.html';
  32. }
  33. $tpl = $this->gadget->template->load($tFilename);
  34. if ($gadgets_combo) {
  35. $block = 'Box';
  36. } else {
  37. $block = 'SimpleBox';
  38. }
  39. $tpl->SetBlock("$block");
  40. $tpl->SetVariable('base_script', BASE_SCRIPT);
  41. $tpl->SetVariable('title', $this->gadget->title);
  42. $tpl->SetVariable('lbl_all', $this::t('WORD_FILTER_ALL'));
  43. $tpl->SetVariable('ttl_all', $this::t('WORD_FILTER_ALL'));
  44. $model = $this->gadget->model->load('Search');
  45. $tpl->SetVariable('all', $model->implodeSearch($post));
  46. // gadgets select box
  47. if ($gadgets_combo) {
  48. $tpl->SetVariable('lbl_search_in', $this::t('SEARCH_IN'));
  49. $gadgetList = $model->GetSearchableGadgets();
  50. $gSearchable = $this->gadget->registry->fetch('searchable_gadgets');
  51. $searchableGadgets = ($gSearchable=='*')? array_keys($gadgetList) : explode(', ', $gSearchable);
  52. array_unshift($searchableGadgets, '*');
  53. foreach ($searchableGadgets as $gadget) {
  54. if ($gadget == '*') {
  55. $title = Jaws::t('ALL');
  56. } else {
  57. $gInfo = Jaws_Gadget::getInstance($gadget);
  58. if (Jaws_Error::IsError($gInfo)) {
  59. continue;
  60. }
  61. $title = $gInfo->title;
  62. }
  63. $tpl->SetBlock("$block/gadget");
  64. $tpl->SetVariable('gadget', $gadget);
  65. $tpl->SetVariable('title', $title);
  66. $tpl->SetVariable('selected', ($post['gadgets'] == $gadget)? 'selected="selected"' : '');
  67. $tpl->ParseBlock("$block/gadget");
  68. }
  69. }
  70. $tpl->SetVariable('search', $this::t('BUTTON'));
  71. $tpl->ParseBlock("$block");
  72. return $tpl->Get();
  73. }
  74. /**
  75. * Builds the simple search box
  76. *
  77. * @access public
  78. * @return string XHTML search box
  79. */
  80. function SimpleBox()
  81. {
  82. return $this->Box(false);
  83. }
  84. /**
  85. * Builds the advanced search box
  86. *
  87. * @access public
  88. * @return string XHTML search box
  89. */
  90. function AdvancedBox()
  91. {
  92. $post = $this->gadget->request->fetch(
  93. array('all', 'exact', 'least', 'exclude', 'gadgets', 'date'),
  94. 'get',
  95. false
  96. );
  97. if ($this->app->requestedActionMode === 'normal') {
  98. $tFilename = 'Search.html';
  99. } else {
  100. $tFilename = 'Search0.html';
  101. }
  102. $tpl = $this->gadget->template->load($tFilename);
  103. $tpl->SetBlock('AdvancedBox');
  104. $tpl->SetVariable('base_script', BASE_SCRIPT);
  105. $tpl->SetVariable('title', $this->gadget->title);
  106. $tpl->SetVariable('lbl_word_filter', $this::t('WORD_FILTER'));
  107. $tpl->SetVariable('lbl_all', $this::t('WORD_FILTER_ALL'));
  108. $tpl->SetVariable('lbl_exact', $this::t('WORD_FILTER_EXACT'));
  109. $tpl->SetVariable('lbl_least', $this::t('WORD_FILTER_LEAST'));
  110. $tpl->SetVariable('lbl_exclude', $this::t('WORD_FILTER_EXCLUDE'));
  111. $tpl->SetVariable('lbl_data_filter', $this::t('DATA_FILTER'));
  112. $tpl->SetVariable('lbl_search_in', $this::t('SEARCH_IN'));
  113. $model = $this->gadget->model->load('Search');
  114. $options = $model->parseSearch($post, $searchable);
  115. //$options = array_map('Jaws_XSS::filter', $options);
  116. array_walk_recursive($options, function (&$value) {
  117. $value = Jaws_XSS::filter($value);
  118. });
  119. $wordAll =& Piwi::CreateWidget('Entry', 'all', implode(' ', $options['all']));
  120. $wordExact =& Piwi::CreateWidget('Entry', 'exact', implode(' ', $options['exact']));
  121. $wordLeast =& Piwi::CreateWidget('Entry', 'least', implode(' ', $options['least']));
  122. $wordExclude =& Piwi::CreateWidget('Entry', 'exclude', implode(' ', $options['exclude']));
  123. $tpl->SetVariable('all', $wordAll->Get());
  124. $tpl->SetVariable('exclude', $wordExclude->Get());
  125. $tpl->SetVariable('least', $wordLeast->Get());
  126. $tpl->SetVariable('exact', $wordExact->Get());
  127. //Gadgets filter combo
  128. $gadgetList = $model->GetSearchableGadgets();
  129. $gSearchable = $this->gadget->registry->fetch('searchable_gadgets');
  130. $searchableGadgets = ($gSearchable=='*')? array_keys($gadgetList) : explode(', ', $gSearchable);
  131. $gchk =& Piwi::CreateWidget('Combo', 'gadgets');
  132. $gchk->addOption(Jaws::t('ALL'), '');
  133. foreach ($searchableGadgets as $gadget) {
  134. $info = Jaws_Gadget::getInstance($gadget);
  135. if (Jaws_Error::IsError($info)) {
  136. continue;
  137. }
  138. $gchk->AddOption($info->title, $gadget);
  139. }
  140. $default = !is_null($post['gadgets']) ? $post['gadgets'] : '';
  141. $gchk->SetDefault($default);
  142. $tpl->SetVariable('gadgets_combo', $gchk->Get());
  143. //Search button
  144. $btnSearch =& Piwi::CreateWidget('Button', '', $this::t('BUTTON'));
  145. $btnSearch->SetID('btn_search');
  146. $btnSearch->SetSubmit(true);
  147. $tpl->SetVariable('btn_search', $btnSearch->Get());
  148. $tpl->ParseBlock('AdvancedBox');
  149. return $tpl->Get();
  150. }
  151. }