/src/system/application/controllers/search.php

https://github.com/jfoucher/joind.in · PHP · 177 lines · 115 code · 20 blank · 42 comment · 11 complexity · 74e8478c34e47e109f8cb76b628872cb MD5 · raw file

  1. <?php
  2. /**
  3. * Search pages controller.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Joind.in
  8. * @package Controllers
  9. * @copyright 2009 - 2010 Joind.in
  10. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  11. * @link http://github.com/joindin/joind.in
  12. */
  13. /**
  14. * Search pages controller.
  15. *
  16. * Responsible for displaying the search page and results.
  17. *
  18. * @category Joind.in
  19. * @package Controllers
  20. * @copyright 2009 - 2010 Joind.in
  21. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  22. * @link http://github.com/joindin/joind.in
  23. *
  24. * @property CI_Config $config
  25. * @property CI_Loader $load
  26. * @property CI_Template $template
  27. * @property CI_Input $input
  28. * @property User_model $user_model
  29. */
  30. class Search extends Controller
  31. {
  32. /**
  33. * Constructor, checks whether the user is logged in and passes this to
  34. * the template.
  35. *
  36. * @return void
  37. */
  38. function Main()
  39. {
  40. parent::Controller();
  41. // check login status and fill the 'logged' parameter in the template
  42. $this->user_model->logStatus();
  43. }
  44. /**
  45. * Displays the search page and results when used.
  46. *
  47. * @return void
  48. */
  49. function index()
  50. {
  51. $this->load->helper('form');
  52. $this->load->library('validation');
  53. $this->load->model('talks_model');
  54. $this->load->model('event_model');
  55. $this->load->helper('reqkey');
  56. $results = array();
  57. $rules = array(
  58. 'search_term' => 'required'
  59. );
  60. $fields = array(
  61. 'search_term' => 'Search Term',
  62. 'start_mo' => 'Start Month',
  63. 'start_day' => 'Start Day',
  64. 'start_yr' => 'Start Year',
  65. 'end_mo' => 'End Month',
  66. 'end_day' => 'End Day',
  67. 'end_yr' => 'End Year',
  68. );
  69. $this->validation->set_rules($rules);
  70. $this->validation->set_fields($fields);
  71. //success! search the talks and events
  72. if ($this->validation->run() == true) {
  73. $query = 'q:' . urlencode($this->input->post('search_term'));
  74. $start = 0;
  75. $end = 0;
  76. $start_mo = $this->input->post('start_mo');
  77. $end_mo = $this->input->post('end_mo');
  78. if (!empty($start_mo)) {
  79. $start = sprintf(
  80. '%04d-%02d-%02d',
  81. $this->input->post('start_yr'),
  82. $this->input->post('start_mo'),
  83. $this->input->post('start_day')
  84. );
  85. $query .= '/start:' . $start;
  86. }
  87. if (!empty($end_mo)) {
  88. $end = sprintf(
  89. '%04d-%02d-%02d',
  90. $this->input->post('end_yr'),
  91. $this->input->post('end_mo'),
  92. $this->input->post('end_day')
  93. );
  94. $query .= '/end:' . $end;
  95. }
  96. redirect('search/' . $query, 'location', 302);
  97. }
  98. $results = null;
  99. $rsegments = $this->uri->rsegments;
  100. array_shift($rsegments); // Remove controller
  101. array_shift($rsegments); // Remove action
  102. if (count($rsegments) > 0) {
  103. $rsegments = array_slice($rsegments, 0, 3);
  104. $search_term = null;
  105. $start = null;
  106. $end = null;
  107. foreach ($rsegments as $val) {
  108. if (false !== ($pos = strpos($val, 'q:'))) {
  109. $search_term = substr($val, 2);
  110. continue;
  111. }
  112. if (false !== ($pos = strpos($val, 'start:'))) {
  113. $start = substr($val, 6);
  114. continue;
  115. }
  116. if (false !== ($pos = strpos($val, 'end:'))) {
  117. $end = substr($val, 4);
  118. continue;
  119. }
  120. }
  121. if (!empty($search_term)) {
  122. $this->validation->search_term = urldecode($search_term);
  123. if (null !== $start) {
  124. $start = max(0, @strtotime($start));
  125. $this->validation->start_mo = date('m', $start);
  126. $this->validation->start_day = date('d', $start);
  127. $this->validation->start_yr = date('Y', $start);
  128. }
  129. if (null !== $end) {
  130. $end = max(0, @strtotime($end));
  131. $this->validation->end_mo = date('m', $end);
  132. $this->validation->end_day = date('d', $end);
  133. $this->validation->end_yr = date('Y', $end);
  134. }
  135. //check to see if they entered a date and set that first
  136. $search_term = urldecode($search_term);
  137. $results = array(
  138. 'talks' => $this->talks_model
  139. ->search($search_term, $start, $end),
  140. 'events' => $this->event_model
  141. ->search($search_term, $start, $end),
  142. 'users' => $this->user_model
  143. ->search($search_term, $start, $end)
  144. );
  145. }
  146. }
  147. $reqkey = buildReqKey();
  148. $arr = array(
  149. 'results' => $results,
  150. 'reqkey' => $reqkey,
  151. 'seckey' => buildSecFile($reqkey)
  152. );
  153. $this->template->write_view('content', 'search/main', $arr, true);
  154. $this->template->render();
  155. }
  156. }
  157. ?>