PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/koowa/template/helper/behavior.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 499 lines | 344 code | 59 blank | 96 comment | 22 complexity | e131e37a807513345a6d8e478ebba577 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: behavior.php 4479 2012-02-10 01:53:06Z johanjanssens $
  4. * @category Koowa
  5. * @package Koowa_Template
  6. * @subpackage Helper
  7. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. * @link http://www.nooku.org
  10. */
  11. /**
  12. * Template Behavior Helper
  13. *
  14. * @author Johan Janssens <johan@nooku.org>
  15. * @category Koowa
  16. * @package Koowa_Template
  17. * @subpackage Helper
  18. */
  19. class KTemplateHelperBehavior extends KTemplateHelperAbstract
  20. {
  21. /**
  22. * Array which holds a list of loaded javascript libraries
  23. *
  24. * boolean
  25. */
  26. protected static $_loaded = array();
  27. /**
  28. * Method to load the mootools framework into the document head
  29. *
  30. * - If debugging mode is on an uncompressed version of mootools is included for easier debugging.
  31. *
  32. * @param boolean $debug Is debugging mode on? [optional]
  33. */
  34. public function mootools($config = array())
  35. {
  36. $config = new KConfig($config);
  37. $html ='';
  38. // Only load once
  39. if (!isset(self::$_loaded['mootools']))
  40. {
  41. $html .= '<script src="media://lib_koowa/js/mootools.js" />';
  42. self::$_loaded['mootools'] = true;
  43. }
  44. return $html;
  45. }
  46. /**
  47. * Render a modal box
  48. *
  49. * @return string The html output
  50. */
  51. public function modal($config = array())
  52. {
  53. $config = new KConfig($config);
  54. $config->append(array(
  55. 'selector' => 'a.modal',
  56. 'options' => array('disableFx' => true)
  57. ));
  58. $html = '';
  59. // Load the necessary files if they haven't yet been loaded
  60. if (!isset(self::$_loaded['modal']))
  61. {
  62. $html .= '<script src="media://lib_koowa/js/modal.js" />';
  63. $html .= '<style src="media://lib_koowa/css/modal.css" />';
  64. self::$_loaded['modal'] = true;
  65. }
  66. $signature = md5(serialize(array($config->selector,$config->options)));
  67. if (!isset(self::$_loaded[$signature]))
  68. {
  69. $options = !empty($config->options) ? $config->options->toArray() : array();
  70. $html .= "
  71. <script>
  72. window.addEvent('domready', function() {
  73. SqueezeBox.initialize(".json_encode($options).");
  74. SqueezeBox.assign($$('".$config->selector."'), {
  75. parse: 'rel'
  76. });
  77. });
  78. </script>";
  79. self::$_loaded[$signature] = true;
  80. }
  81. return $html;
  82. }
  83. /**
  84. * Render a tooltip
  85. *
  86. * @return string The html output
  87. */
  88. public function tooltip($config = array())
  89. {
  90. $config = new KConfig($config);
  91. $config->append(array(
  92. 'selector' => '.hasTip',
  93. 'options' => array()
  94. ));
  95. $html = '';
  96. $signature = md5(serialize(array($config->selector,$config->options)));
  97. if (!isset(self::$_loaded[$signature]))
  98. {
  99. //Don't pass an empty array as options
  100. $options = $config->options->toArray() ? ', '.$config->options : '';
  101. $html .= "
  102. <script>
  103. window.addEvent('domready', function(){ new Tips($$('".$config->selector."')".$options."); });
  104. </script>";
  105. self::$_loaded[$signature] = true;
  106. }
  107. return $html;
  108. }
  109. /**
  110. * Render an overlay
  111. *
  112. * @return string The html output
  113. */
  114. public function overlay($config = array())
  115. {
  116. $config = new KConfig($config);
  117. $config->append(array(
  118. 'url' => '',
  119. 'options' => array(),
  120. 'attribs' => array(),
  121. ));
  122. $html = '';
  123. // Load the necessary files if they haven't yet been loaded
  124. if (!isset(self::$_loaded['overlay']))
  125. {
  126. $html .= '<script src="media://lib_koowa/js/koowa.js" />';
  127. $html .= '<style src="media://lib_koowa/css/koowa.css" />';
  128. self::$_loaded['overlay'] = true;
  129. }
  130. $url = $this->getService('koowa:http.url', array('url' => $config->url));
  131. if(!isset($url->query['tmpl'])) {
  132. $url->query['tmpl'] = '';
  133. }
  134. $attribs = KHelperArray::toString($config->attribs);
  135. $id = 'overlay'.rand();
  136. if($url->fragment)
  137. {
  138. //Allows multiple identical ids, legacy should be considered replaced with #$url->fragment instead
  139. $config->append(array(
  140. 'options' => array(
  141. 'selector' => '[id='.$url->fragment.']'
  142. )
  143. ));
  144. }
  145. //Don't pass an empty array as options
  146. $options = $config->options->toArray() ? ', '.$config->options : '';
  147. $html .= "<script>window.addEvent('domready', function(){new Koowa.Overlay('$id'".$options.");});</script>";
  148. $html .= '<div data-url="'.$url.'" class="-koowa-overlay" id="'.$id.'" '.$attribs.'><div class="-koowa-overlay-status">'.JText::_('Loading...').'</div></div>';
  149. return $html;
  150. }
  151. /**
  152. * Keep session alive
  153. *
  154. * This will send an ascynchronous request to the server via AJAX on an interval
  155. * in miliseconds
  156. *
  157. * @return string The html output
  158. */
  159. public function keepalive($config = array())
  160. {
  161. $config = new KConfig($config);
  162. $config->append(array(
  163. 'refresh' => 15 * 60000, //15min
  164. 'url' => KRequest::url()
  165. ));
  166. $refresh = (int) $config->refresh;
  167. // Longest refresh period is one hour to prevent integer overflow.
  168. if ($refresh > 3600000 || $refresh <= 0) {
  169. $refresh = 3600000;
  170. }
  171. // Build the keepalive script.
  172. $html =
  173. "<script>
  174. Koowa.keepalive = function() {
  175. var request = new Request({method: 'get', url: '".$config->url."'}).send();
  176. }
  177. window.addEvent('domready', function() { Koowa.keepalive.periodical('".$refresh."'); });
  178. </script>";
  179. return $html;
  180. }
  181. /**
  182. * Loads the Forms.Validator class and connects it to Koowa.Controller
  183. *
  184. * This allows you to do easy, css class based forms validation-
  185. * Koowa.Controller.Form works with it automatically.
  186. * Requires koowa.js and mootools to be loaded in order to work.
  187. *
  188. * @see http://www.mootools.net/docs/more125/more/Forms/Form.Validator
  189. *
  190. * @return string The html output
  191. */
  192. public function validator($config = array())
  193. {
  194. $config = new KConfig($config);
  195. $config->append(array(
  196. 'selector' => '.-koowa-form',
  197. 'options' => array(
  198. 'scrollToErrorsOnChange' => true,
  199. 'scrollToErrorsOnBlur' => true
  200. )
  201. ));
  202. $html = '';
  203. // Load the necessary files if they haven't yet been loaded
  204. if(!isset(self::$_loaded['validator']))
  205. {
  206. if(version_compare(JVERSION,'1.6.0','ge')) {
  207. $html .= '<script src="media://lib_koowa/js/validator-1.3.js" />';
  208. } else {
  209. $html .= '<script src="media://lib_koowa/js/validator-1.2.js" />';
  210. }
  211. $html .= '<script src="media://lib_koowa/js/patch.validator.js" />';
  212. self::$_loaded['validator'] = true;
  213. }
  214. //Don't pass an empty array as options
  215. $options = $config->options->toArray() ? ', '.$config->options : '';
  216. $html .= "<script>
  217. window.addEvent('domready', function(){
  218. $$('$config->selector').each(function(form){
  219. new Koowa.Validator(form".$options.");
  220. form.addEvent('validate', form.validate.bind(form));
  221. });
  222. });
  223. </script>";
  224. return $html;
  225. }
  226. /**
  227. * Loads the autocomplete behavior and attaches it to a specified element
  228. *
  229. * @see http://mootools.net/forge/p/meio_autocomplete
  230. * @return string The html output
  231. */
  232. public function autocomplete($config = array())
  233. {
  234. $config = new KConfig($config);
  235. $config->append(array(
  236. 'identifier' => null,
  237. 'element' => null,
  238. 'path' => 'name',
  239. 'filter' => array(),
  240. 'validate' => true,
  241. 'selected' => null
  242. ))->append(array(
  243. 'value_element' => $config->element.'-value',
  244. 'attribs' => array(
  245. 'id' => $config->element,
  246. 'type' => 'text',
  247. 'class' => 'inputbox value',
  248. 'size' => 60
  249. ),
  250. ))->append(array(
  251. 'options' => array(
  252. 'valueField' => $config->value_element,
  253. 'filter' => array('path' => $config->path),
  254. 'requestOptions' => array('method' => 'get'),
  255. 'urlOptions' => array(
  256. 'queryVarName' => 'search',
  257. 'extraParams' => KConfig::unbox($config->filter)
  258. )
  259. )
  260. ));
  261. if($config->validate)
  262. {
  263. $config->attribs['data-value'] = $config->element.'-value';
  264. $config->attribs['data-value'] .= ' ma-required';
  265. }
  266. if(!isset($config->url))
  267. {
  268. $identifier = $this->getIdentifier($config->identifier);
  269. $config->url = JRoute::_('index.php?option=com_'.$identifier->package.'&view='.$identifier->name.'&format=json', false);
  270. }
  271. $html = '';
  272. // Load the necessary files if they haven't yet been loaded
  273. if(!isset(self::$_loaded['autocomplete']))
  274. {
  275. $html .= '<script src="media://lib_koowa/js/autocomplete.js" />';
  276. $html .= '<script src="media://lib_koowa/js/patch.autocomplete.js" />';
  277. $html .= '<style src="media://lib_koowa/css/autocomplete.css" />';
  278. }
  279. $html .= "
  280. <script>
  281. window.addEvent('domready', function(){
  282. new Koowa.Autocomplete($('".$config->element."'), ".json_encode($config->url).", ".json_encode(KConfig::unbox($config->options)).");
  283. });
  284. </script>";
  285. $html .= '<input '.KHelperArray::toString($config->attribs).' />';
  286. $html .= '<input '.KHelperArray::toString(array(
  287. 'type' => 'hidden',
  288. 'name' => $config->value,
  289. 'id' => $config->element.'-value',
  290. 'value' => $config->selected
  291. )).' />';
  292. return $html;
  293. }
  294. /**
  295. * Loads the calendar behavior and attaches it to a specified element
  296. *
  297. * @return string The html output
  298. */
  299. public function calendar($config = array())
  300. {
  301. $config = new KConfig($config);
  302. $config->append(array(
  303. 'date' => gmdate("M d Y H:i:s"),
  304. 'name' => '',
  305. 'format' => '%Y-%m-%d %H:%M:%S',
  306. 'attribs' => array('size' => 25, 'maxlenght' => 19),
  307. 'gmt_offset' => JFactory::getConfig()->getValue('config.offset') * 3600
  308. ));
  309. if($config->date && $config->date != '0000-00-00 00:00:00' && $config->date != '0000-00-00') {
  310. $config->date = strftime($config->format, strtotime($config->date) /*+ $config->gmt_offset*/);
  311. }
  312. else $config->date = '';
  313. $html = '';
  314. // Load the necessary files if they haven't yet been loaded
  315. if (!isset(self::$_loaded['calendar']))
  316. {
  317. $html .= '<script src="media://lib_koowa/js/calendar.js" />';
  318. $html .= '<script src="media://lib_koowa/js/calendar-setup.js" />';
  319. $html .= '<style src="media://lib_koowa/css/calendar.css" />';
  320. $html .= '<script>'.$this->_calendarTranslation().'</script>';
  321. self::$_loaded['calendar'] = true;
  322. }
  323. $html .= "<script>
  324. window.addEvent('domready', function() {Calendar.setup({
  325. inputField : '".$config->name."',
  326. ifFormat : '".$config->format."',
  327. button : 'button-".$config->name."',
  328. align : 'Tl',
  329. singleClick : true,
  330. showsTime : false
  331. });});
  332. </script>";
  333. $attribs = KHelperArray::toString($config->attribs);
  334. $html .= '<input type="text" name="'.$config->name.'" id="'.$config->name.'" value="'.$config->date.'" '.$attribs.' />';
  335. $html .= '<img class="calendar" src="media://lib_koowa/images/calendar.png" alt="calendar" id="button-'.$config->name.'" />';
  336. return $html;
  337. }
  338. /**
  339. * Method to get the internationalisation script/settings for the JavaScript Calendar behavior.
  340. *
  341. * @return string The html output
  342. */
  343. protected function _calendarTranslation()
  344. {
  345. // Build the day names array.
  346. $dayNames = array(
  347. '"'.JText::_('Sunday').'"',
  348. '"'.JText::_('Monday').'"',
  349. '"'.JText::_('Tuesday').'"',
  350. '"'.JText::_('Wednesday').'"',
  351. '"'.JText::_('Thursday').'"',
  352. '"'.JText::_('Friday').'"',
  353. '"'.JText::_('Saturday').'"',
  354. '"'.JText::_('Sunday').'"'
  355. );
  356. // Build the short day names array.
  357. $shortDayNames = array(
  358. '"'.JText::_('Sun').'"',
  359. '"'.JText::_('Mon').'"',
  360. '"'.JText::_('Tue').'"',
  361. '"'.JText::_('Wed').'"',
  362. '"'.JText::_('Thu').'"',
  363. '"'.JText::_('Fri').'"',
  364. '"'.JText::_('Sat').'"',
  365. '"'.JText::_('Sun').'"'
  366. );
  367. // Build the month names array.
  368. $monthNames = array(
  369. '"'.JText::_('January').'"',
  370. '"'.JText::_('February').'"',
  371. '"'.JText::_('March').'"',
  372. '"'.JText::_('April').'"',
  373. '"'.JText::_('May').'"',
  374. '"'.JText::_('June').'"',
  375. '"'.JText::_('July').'"',
  376. '"'.JText::_('August').'"',
  377. '"'.JText::_('September').'"',
  378. '"'.JText::_('October').'"',
  379. '"'.JText::_('November').'"',
  380. '"'.JText::_('December').'"'
  381. );
  382. // Build the short month names array.
  383. $shortMonthNames = array(
  384. '"'.JText::_('January_short').'"',
  385. '"'.JText::_('February_short').'"',
  386. '"'.JText::_('March_short').'"',
  387. '"'.JText::_('April_short').'"',
  388. '"'.JText::_('May_short').'"',
  389. '"'.JText::_('June_short').'"',
  390. '"'.JText::_('July_short').'"',
  391. '"'.JText::_('August_short').'"',
  392. '"'.JText::_('September_short').'"',
  393. '"'.JText::_('October_short').'"',
  394. '"'.JText::_('November_short').'"',
  395. '"'.JText::_('December_short').'"'
  396. );
  397. // Build the script.
  398. $i18n = array(
  399. '// Calendar i18n Setup.',
  400. 'Calendar._FD = 0;',
  401. 'Calendar._DN = new Array ('.implode(', ', $dayNames).');',
  402. 'Calendar._SDN = new Array ('.implode(', ', $shortDayNames).');',
  403. 'Calendar._MN = new Array ('.implode(', ', $monthNames).');',
  404. 'Calendar._SMN = new Array ('.implode(', ', $shortMonthNames).');',
  405. '',
  406. 'Calendar._TT = {};',
  407. 'Calendar._TT["INFO"] = "'.JText::_('About the calendar').'";',
  408. 'Calendar._TT["PREV_YEAR"] = "'.JText::_('Prev. year (hold for menu)').'";',
  409. 'Calendar._TT["PREV_MONTH"] = "'.JText::_('Prev. month (hold for menu)').'";',
  410. 'Calendar._TT["GO_TODAY"] = "'.JText::_('Go Today').'";',
  411. 'Calendar._TT["NEXT_MONTH"] = "'.JText::_('Next month (hold for menu)').'";',
  412. 'Calendar._TT["NEXT_YEAR"] = "'.JText::_('Next year (hold for menu)').'";',
  413. 'Calendar._TT["SEL_DATE"] = "'.JText::_('Select date').'";',
  414. 'Calendar._TT["DRAG_TO_MOVE"] = "'.JText::_('Drag to move').'";',
  415. 'Calendar._TT["PART_TODAY"] = "'.JText::_('(Today)').'";',
  416. 'Calendar._TT["DAY_FIRST"] = "'.JText::_('Display %s first').'";',
  417. 'Calendar._TT["WEEKEND"] = "0,6";',
  418. 'Calendar._TT["CLOSE"] = "'.JText::_('Close').'";',
  419. 'Calendar._TT["TODAY"] = "'.JText::_('Today').'";',
  420. 'Calendar._TT["TIME_PART"] = "'.JText::_('(Shift-)Click or drag to change value').'";',
  421. 'Calendar._TT["DEF_DATE_FORMAT"] = "'.JText::_('%Y-%m-%d').'";',
  422. 'Calendar._TT["TT_DATE_FORMAT"] = "'.JText::_('%a, %b %e').'";',
  423. 'Calendar._TT["WK"] = "'.JText::_('wk').'";',
  424. 'Calendar._TT["TIME"] = "'.JText::_('Time:').'";',
  425. '',
  426. '"Date selection:\n" +',
  427. '"- Use the \xab, \xbb buttons to select year\n" +',
  428. '"- Use the " + String.fromCharCode(0x2039) + ", " + String.fromCharCode(0x203a) + " buttons to select month\n" +',
  429. '"- Hold mouse button on any of the above buttons for faster selection.";',
  430. '',
  431. 'Calendar._TT["ABOUT_TIME"] = "\n\n" +',
  432. '"Time selection:\n" +',
  433. '"- Click on any of the time parts to increase it\n" +',
  434. '"- or Shift-click to decrease it\n" +',
  435. '"- or click and drag for faster selection.";',
  436. ''
  437. );
  438. return implode("\n", $i18n);
  439. }
  440. }