PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/joomla/plugins/search/eventsearch/eventsearch.php

https://gitlab.com/ricardosanchez/prueba
PHP | 386 lines | 299 code | 34 blank | 53 comment | 18 complexity | 1728407886b2e8680d8c58c7414cea3a MD5 | raw file
  1. <?php
  2. /**
  3. * Events Calendar Search plugin for Joomla 1.5.x
  4. *
  5. * @version $Id: eventsearch.php 3588 2012-05-02 10:40:19Z geraintedwards $
  6. * @package Events
  7. * @subpackage Mambot Events Calendar
  8. * @copyright Copyright (C) 2008-2016 GWE Systems Ltd
  9. * @copyright Copyright (C) 2006-2007 JEvents Project Group
  10. * @copyright Copyright (C) 2000 - 2003 Eric Lamette, Dave McDonnell
  11. * @licence http://www.gnu.org/copyleft/gpl.html
  12. * @link http://joomlacode.org/gf/project/jevents
  13. */
  14. /** ensure this file is being included by a parent file */
  15. defined('_JEXEC') or die('Restricted access');
  16. // setup for all required function and classes
  17. $file = JPATH_SITE . '/components/com_jevents/mod.defines.php';
  18. if (file_exists($file))
  19. {
  20. include_once($file);
  21. include_once(JEV_LIBS . "/modfunctions.php");
  22. }
  23. else
  24. {
  25. die("JEvents Calendar\n<br />This plugin needs the JEvents component");
  26. }
  27. // Import library dependencies
  28. jimport('joomla.event.plugin');
  29. // Check for 1.6
  30. if (!(version_compare(JVERSION, '1.6.0', ">=")))
  31. {
  32. JFactory::getApplication()->registerEvent('onSearchAreas', 'plgSearchEventsSearchAreas');
  33. }
  34. /**
  35. * @return array An array of search areas
  36. */
  37. function plgSearchEventsSearchAreas()
  38. {
  39. $lang = JFactory::getLanguage();
  40. $lang->load("plg_search_eventsearch", JPATH_ADMINISTRATOR);
  41. if (version_compare(JVERSION, '1.6.0', ">="))
  42. {
  43. return array(
  44. 'eventsearch' => JText::_('JEV_EVENTS_SEARCH')
  45. );
  46. }
  47. else
  48. {
  49. return array(
  50. 'events' => JText::_('JEV_EVENTS_SEARCH')
  51. );
  52. }
  53. }
  54. class plgSearchEventsearch extends JPlugin
  55. {
  56. /**
  57. * Constructor
  58. *
  59. * For php4 compatability we must not use the __constructor as a constructor for plugins
  60. * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  61. * This causes problems with cross-referencing necessary for the observer design pattern.
  62. *
  63. * @param object $subject The object to observe
  64. * @param array $config An array that holds the plugin configuration
  65. * @since 1.5
  66. */
  67. function __construct(&$subject, $config = array()) // RSH 10/4/10 added config array to args, needed for plugin parameter registration!
  68. {
  69. parent::__construct($subject, $config); // RSH 10/4/10 added config array to args, needed for plugin parameter registration!
  70. JFactory::getLanguage()->load();
  71. // load plugin parameters
  72. if (!(version_compare(JVERSION, '1.6.0', ">=")))
  73. {
  74. $this->_plugin = JPluginHelper::getPlugin('search', 'eventsearch');
  75. $this->_params = new JRegistry($this->_plugin->params);
  76. }
  77. $this->loadLanguage( 'plg_search_eventsearch' );
  78. }
  79. /**
  80. * @return array An array of search areas
  81. */
  82. function onContentSearchAreas()
  83. {
  84. return array(
  85. 'eventsearch' => JText::_('JEV_EVENTS_SEARCH')
  86. );
  87. }
  88. function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
  89. {
  90. return $this->onSearch($text, $phrase, $ordering, $areas);
  91. }
  92. /**
  93. * Search method
  94. *
  95. * The sql must return the following fields that are used in a common display
  96. * routine: href, title, section, created, text, browsernav
  97. * @param string Target search string
  98. * @param string matching option, exact|any|all
  99. * @param string ordering option, newest|oldest|popular|alpha|category
  100. */
  101. function onSearch($text, $phrase = '', $ordering = '', $areas = null)
  102. {
  103. $db = JFactory::getDBO();
  104. $user = JFactory::getUser();
  105. $groups = (version_compare(JVERSION, '1.6.0', '>=')) ? implode(',', $user->getAuthorisedViewLevels()) : false;
  106. $limit = (version_compare(JVERSION, '1.6.0', '>=')) ? $this->params->get('search_limit', 50) : $this->_params->def('search_limit', 50);
  107. $dateformat = (version_compare(JVERSION, '1.6.0', ">=")) ? $this->params->get('date_format', "%d %B %Y") : $this->_params->def('date_format', "%d %B %Y");
  108. $allLanguages = $this->params->get('all_language_search', true);
  109. $limit = "\n LIMIT $limit";
  110. $text = trim($text);
  111. if ($text == '')
  112. {
  113. return array();
  114. }
  115. if (is_array($areas))
  116. {
  117. $test = array_keys(plgSearchEventsSearchAreas());
  118. if (!array_intersect($areas, array_keys(plgSearchEventsSearchAreas())))
  119. {
  120. return array();
  121. }
  122. }
  123. $params = JComponentHelper::getParams("com_jevents");
  124. // See http://www.php.net/manual/en/timezones.php
  125. $tz = $params->get("icaltimezonelive", "");
  126. if ($tz != "" && is_callable("date_default_timezone_set"))
  127. {
  128. $timezone = date_default_timezone_get();
  129. date_default_timezone_set($tz);
  130. $this->jeventstimezone = $timezone;
  131. }
  132. $search_ical_attributes = array('det.summary', 'det.description', 'det.location', 'det.contact', 'det.extra_info');
  133. // process the new plugins
  134. // get extra data and conditionality from plugins
  135. $extrawhere = array();
  136. $extrajoin = array();
  137. $needsgroup = false;
  138. $filterarray = array("published");
  139. $dataModel = new JEventsDataModel();
  140. $compparams = JComponentHelper::getParams("com_jevents");
  141. if ($compparams->get("multicategory",0)){
  142. $catwhere = "\n AND catmap.catid IN(" . $dataModel->accessibleCategoryList(null,null,null,$allLanguages) . ")";
  143. $catjoin = "\n LEFT JOIN #__jevents_catmap as catmap ON catmap.evid = rpt.eventid";
  144. $catjoin .= "\n LEFT JOIN #__categories AS b ON catmap.catid = b.id";
  145. }
  146. else {
  147. $catwhere = "\n AND ev.catid IN(" . $dataModel->accessibleCategoryList(null,null,null,$allLanguages) . ")";
  148. $catjoin = "\n INNER JOIN #__categories AS b ON b.id = ev.catid";
  149. }
  150. // If there are extra filters from the module then apply them now
  151. $reg = JFactory::getConfig();
  152. $modparams = $reg->get("jev.modparams", false);
  153. if ($modparams && $modparams->get("extrafilters", false))
  154. {
  155. $filterarray = array_merge($filterarray, explode(",", $modparams->get("extrafilters", false)));
  156. }
  157. $filters = jevFilterProcessing::getInstance($filterarray);
  158. $filters->setWhereJoin($extrawhere, $extrajoin);
  159. $needsgroup = $filters->needsGroupBy();
  160. JPluginHelper::importPlugin('jevents');
  161. $dispatcher = JEventDispatcher::getInstance();
  162. $dispatcher->trigger('onListIcalEvents', array(& $extrafields, & $extratables, & $extrawhere, & $extrajoin, & $needsgroup));
  163. $extrajoin = ( count($extrajoin) ? " \n LEFT JOIN " . implode(" \n LEFT JOIN ", $extrajoin) : '' );
  164. $extrawhere = ( count($extrawhere) ? ' AND ' . implode(' AND ', $extrawhere) : '' );
  165. $extrasearchfields = array();
  166. // NB extrajoin is a string from now on
  167. $dispatcher->trigger('onSearchEvents', array(& $extrasearchfields, & $extrajoin, & $needsgroup));
  168. $wheres = array();
  169. $wheres_ical = array();
  170. switch ($phrase) {
  171. case 'exact':
  172. $text = $db->Quote('%' . $db->escape($text, true) . '%', false);
  173. // ical
  174. $wheres2 = array();
  175. foreach ($search_ical_attributes as $search_item)
  176. {
  177. $wheres2[] = "LOWER($search_item) LIKE " . $text;
  178. }
  179. $where_ical = '(' . implode(') OR (', $wheres2) . ')';
  180. break;
  181. case 'all':
  182. case 'any':
  183. default:
  184. $words = explode(' ', $text);
  185. $text = $db->Quote('%' . $db->escape($text, true) . '%', false);
  186. // ical
  187. $wheres = array();
  188. foreach ($words as $word)
  189. {
  190. $word = $db->Quote('%' . $db->escape($word) . '%', false);
  191. $wheres2 = array();
  192. foreach ($search_ical_attributes as $search_item)
  193. {
  194. $wheres2[] = "LOWER($search_item) LIKE " . $word;
  195. }
  196. $wheres[] = implode(' OR ', $wheres2);
  197. }
  198. $where_ical = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
  199. break;
  200. }
  201. if (count($extrasearchfields) > 0)
  202. {
  203. $extraor = implode(" OR ", $extrasearchfields);
  204. $extraor = " OR " . $extraor;
  205. // replace the ### placeholder with the keyword
  206. // $text is already exscaped above
  207. $extraor = str_replace("###", $text, $extraor);
  208. $where_ical .= $extraor;
  209. }
  210. // some of the where statements may already be escaped
  211. $where_ical = str_replace("%'%'", "%'", $where_ical);
  212. $where_ical = str_replace("''", "'", $where_ical);
  213. $where_ical = str_replace("'%'%", "'%", $where_ical);
  214. $morder = '';
  215. $morder_ical = '';
  216. switch ($ordering) {
  217. case 'oldest':
  218. $order = 'a.created ASC';
  219. $order_ical = 'det.created ASC';
  220. break;
  221. case 'popular':
  222. $order = 'a.hits DESC';
  223. $order_ical = 'det.created ASC'; // no hit field available
  224. break;
  225. case 'alpha':
  226. $order = 'a.title ASC';
  227. $order_ical = 'det.summary ASC';
  228. break;
  229. case 'category':
  230. $order = 'b.title ASC, a.title ASC';
  231. $morder = 'a.title ASC';
  232. $order_ical = 'b.title ASC, det.summary ASC';
  233. $morder_ical = 'det.summary ASC';
  234. break;
  235. case 'newest':
  236. default:
  237. $order = 'a.created DESC';
  238. $order_ical = 'det.created DESC';
  239. break;
  240. }
  241. $eventstitle = JText::_("JEV_EVENT_CALENDAR");
  242. // Now Search Icals
  243. $display2 = array();
  244. foreach ($search_ical_attributes as $search_ical_attribute)
  245. {
  246. $display2[] = "$search_ical_attribute";
  247. }
  248. $display = 'CONCAT(' . implode(", ' ', ", $display2) . ')';
  249. $query = "SELECT det.evdet_id, det.summary as title,"
  250. . "\n ev.created as created,"
  251. . "\n $display as text,"
  252. . "\n CONCAT('$eventstitle','/',det.summary) AS section,"
  253. . "\n CONCAT('index.php?option=com_jevents&task=icalrepeat.detail&evid=',min(rpt.rp_id)) AS href,"
  254. . "\n '2' AS browsernav ,"
  255. . "\n rpt.startrepeat, rpt.rp_id "
  256. . "\n FROM (#__jevents_vevent as ev)"
  257. . "\n LEFT JOIN #__jevents_repetition as rpt ON rpt.eventid = ev.ev_id"
  258. . $catjoin
  259. . "\n LEFT JOIN #__jevents_vevdetail as det ON det.evdet_id = rpt.eventdetail_id"
  260. . "\n LEFT JOIN #__jevents_icsfile as icsf ON icsf.ics_id = ev.icsid"
  261. . $extrajoin
  262. . "\n WHERE ($where_ical)"
  263. . "\n AND icsf.state = 1"
  264. . "\n AND icsf.access " . ((version_compare(JVERSION, '1.6.0', '>=')) ? ' IN (' . $groups . ')' : ' <= ' . $user->gid)
  265. . "\n AND ev.state = 1"
  266. . "\n AND ev.access " . ((version_compare(JVERSION, '1.6.0', '>=')) ? ' IN (' . $groups . ')' : ' <= ' . $user->gid)
  267. . "\n AND b.access " . ((version_compare(JVERSION, '1.6.0', '>=')) ? ' IN (' . $groups . ')' : ' <= ' . $user->gid)
  268. . "\n AND b.published = '1'"
  269. . $extrawhere
  270. . $catwhere
  271. . "\n GROUP BY det.evdet_id"
  272. . "\n ORDER BY " . ($morder_ical ? $morder_ical : $order_ical)
  273. . $limit
  274. ;
  275. $db->setQuery($query);
  276. $list_ical = $db->loadObjectList('evdet_id');
  277. jimport('joomla.utilities.date');
  278. if ($list_ical)
  279. {
  280. foreach ($list_ical as $id => $item)
  281. {
  282. $user = JFactory::getUser();
  283. $query = "SELECT ev.*, ev.state as published, rpt.*, rr.*, det.*, ev.created as created, ex_id, exception_type "
  284. . "\n , YEAR(rpt.startrepeat) as yup, MONTH(rpt.startrepeat ) as mup, DAYOFMONTH(rpt.startrepeat ) as dup"
  285. . "\n , YEAR(rpt.endrepeat ) as ydn, MONTH(rpt.endrepeat ) as mdn, DAYOFMONTH(rpt.endrepeat ) as ddn"
  286. . "\n , HOUR(rpt.startrepeat) as hup, MINUTE(rpt.startrepeat ) as minup, SECOND(rpt.startrepeat ) as sup"
  287. . "\n , HOUR(rpt.endrepeat ) as hdn, MINUTE(rpt.endrepeat ) as mindn, SECOND(rpt.endrepeat ) as sdn"
  288. . "\n FROM #__jevents_vevent as ev"
  289. . "\n LEFT JOIN #__jevents_repetition as rpt ON rpt.eventid = ev.ev_id"
  290. . "\n LEFT JOIN #__jevents_vevdetail as det ON det.evdet_id = rpt.eventdetail_id"
  291. . "\n LEFT JOIN #__jevents_rrule as rr ON rr.eventid = ev.ev_id"
  292. . "\n LEFT JOIN #__jevents_exception as ex ON det.evdet_id = ex.eventdetail_id"
  293. . "\n WHERE ev.access IN ('" . $groups . "')"
  294. . "\n AND det.evdet_id = $id"
  295. . "\n ORDER BY rpt.startrepeat ASC limit 1";
  296. $db->setQuery($query);
  297. $row = $db->loadObject();
  298. if (!$row)
  299. continue;
  300. $event = new jIcalEventRepeat($row);
  301. // only get the next repeat IF its not an exception
  302. if (is_null($row->ex_id)){
  303. $event = $event->getNextRepeat();
  304. }
  305. $startdate = new JevDate(strtotime($event->_startrepeat));
  306. $item->title = $item->title . " (" . $startdate->toFormat($dateformat) . ")";
  307. $item->startrepeat = $event->_startrepeat;
  308. $myitemid = $this->params->get("target_itemid",0);
  309. if($myitemid == 0)
  310. {
  311. // I must find the itemid that allows this event to be shown
  312. $catidsOut = $modcatids = $catidList = $modparams = $showall = "";
  313. // Use the plugin params to ensure menu item is picked up
  314. //$modparams = new JRegistry($this->_plugin->params);
  315. $modparams = new JRegistry(null);
  316. // pretend to have category restriction
  317. $modparams->set("catid0", $row->catid);
  318. $modparams->set("ignorecatfilter", 1);
  319. $myitemid = findAppropriateMenuID($catidsOut, $modcatids, $catidList, $modparams->toObject(), $showall);
  320. }
  321. $item->href = $event->viewDetailLink($event->yup(), $event->mup(), $event->dup(), false, $myitemid);
  322. $link = $item->href;
  323. $list_ical[$id] = $item;
  324. }
  325. }
  326. // Must reset the timezone back!!
  327. if ($tz && is_callable("date_default_timezone_set"))
  328. {
  329. date_default_timezone_set($timezone);
  330. }
  331. return $list_ical;
  332. }
  333. }