PageRenderTime 73ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/form_tags.php

https://github.com/Fusion/lenses
PHP | 517 lines | 383 code | 74 blank | 60 comment | 34 complexity | d9e3b94f25883207a6508d444715c988 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Lenses
  4. * @copyright (c) Chris F. Ravenscroft
  5. * @license See 'license.txt'
  6. */
  7. /* Unit tests
  8. class Config
  9. {
  10. static $path = "/toppath";
  11. }
  12. define('WEBROOT', 'http://test/');
  13. unit_test(); exit;
  14. */
  15. function unit_test()
  16. {
  17. print "link_to:\t".link_to('gato','link', array('class'=>'classy'))."\n";
  18. print "form_tag:\t".form_tag('link')."\n";
  19. print "button_to:\t".button_to('gato','link', array('class'=>'classy'))."\n";
  20. print "mail_to:\t".mail_to('Email me', 'test@domain.com', array('class'=>'classy'))."\n";
  21. print "label_for:\t".label_for('myinputtag', 'Awesome Label', array('class'=>'classy'))."\n";
  22. print "input_tag:\t".input_tag('myinputtag', 'defaultvalue', array('class'=>'classy'))."\n";
  23. print "input_password_tag:\t".input_password_tag('mypasswordinputtag', 'defaultvalue', array('class'=>'classy'))."\n";
  24. print "input_hidden_tag:\t".input_hidden_tag('myhiddeninputtag', 'defaultvalue', array('class'=>'classy'))."\n";
  25. print "input_file_tag:\t".input_file_tag('myfileinputtag', array('class'=>'classy'))."\n";
  26. print "textarea_tag:\t".textarea_tag('mytextarea', 'Original Content', array('class'=>'classy'))."\n";
  27. print "rich_textarea_tag:\t".rich_textarea_tag('mytextarea', 'Original Content', array('class'=>'classy'))."\n";
  28. print "click_tag:\t".click_tag('mytext', av_callback, array('class'=>'classy'))."\n";
  29. print "img_tag:\t".img_tag('http://image.gif', array('class'=>'classy'))."\n";
  30. print "click_img_tag:\t".click_img_tag('http://image.gif', av_callback, array('class'=>'classy'))."\n";
  31. print "submit_tag:\t".submit_tag('submittag', array('name'=>'ohdosubmit','class'=>'classy'))."\n";
  32. print "submit_image_tag:\t".submit_image_tag('http://image.gif', array('name'=>'ohdosubmit','class'=>'classy'))."\n";
  33. print "select_tag:\t".select_tag('selectboxid', null, array('name'=>'selectboxname','class'=>'classy'))."\n";
  34. print "select_tag:\t".select_tag('selectboxid',
  35. array('one' => 'One', 'two' => 'Two'),
  36. array('name'=>'selectboxname','class'=>'classy'))."\n";
  37. print "constrained_img_tag:\t".constrained_img_tag("http://image.gif", array('width'=> 50,'height'=> 150,'max_width' =>100,'max_height'=>100))."\n";
  38. # print "flash_upload_form_tag:\t".flash_upload_form_tag('user/controlpaneluploadavatar', array('form_class'=>'uniForm', 'input_label'=>'New Avatar', 'magic_field'=>'submitform', 'submit_class'=>'submitButton', 'submit_caption'=>'Update Avatar','form_wrapper'=>"<fieldset class='blockLabels'>\n*\n</fieldset>", 'input_wrapper'=>"<div class='ctrlHolder'>\n*\n<div id='feedback'></div></div>", 'feedback_id'=>'feedback', 'submit_wrapper'=>"<div class='buttonHolder'>\n*\n</div>", 'callback'=>av_callback))."\n";
  39. }
  40. // ----------------------------------------------------------------------------
  41. // Reserved stuff
  42. // ----------------------------------------------------------------------------
  43. function _options($options = array())
  44. {
  45. $html = '';
  46. foreach ($options as $key => $value)
  47. $html .= ' '.$key.'="'.$value.'"';
  48. return $html;
  49. }
  50. function _get_id_from_name($name, $value = null)
  51. {
  52. // check to see if we have an array variable for a field name
  53. if (strstr($name, '['))
  54. $name = str_replace(
  55. array('[]', '][', '[', ']'),
  56. array((($value != null) ? '_'.$value : ''), '_', '_', ''),
  57. $name);
  58. return $name;
  59. }
  60. // ----------------------------------------------------------------------------
  61. // Links etc.
  62. // ----------------------------------------------------------------------------
  63. /**
  64. * Canonize a url - not a tag _per se_
  65. */
  66. function url_for($url)
  67. {
  68. if(empty($url))
  69. $url = $_SERVER['PHP_SELF'];
  70. return Config::$path . $url;
  71. }
  72. /**
  73. * Create a link to a given uri
  74. */
  75. function link_to($text, $uri, $options = array())
  76. {
  77. $options['href'] = url_for($uri);
  78. return tag('a', $text, $options, TAG_OPEN_CLOSE);
  79. }
  80. /**
  81. * Create a link to a given uri
  82. * But it's a button wrapped in its own small form
  83. */
  84. function button_to($name, $uri, $options = array())
  85. {
  86. $options['value'] = $name;
  87. $options['type'] = 'submit';
  88. return form_tag($target, array('method' => 'post')) . tag('input', null, $options).'</form>';
  89. }
  90. /**
  91. * Create a 'mail to' link
  92. */
  93. function mail_to($name, $uri, $options = array())
  94. {
  95. $options['href'] = 'mailto:'.$uri;
  96. return tag('a', $name, $options, TAG_OPEN_CLOSE);
  97. }
  98. // ----------------------------------------------------------------------------
  99. // tag!
  100. // ----------------------------------------------------------------------------
  101. define('TAG_SELF_CONTAINED', 'tsc');
  102. define('TAG_OPEN_ONLY', 'too');
  103. define('TAG_OPEN_CLOSE', 'toc');
  104. /**
  105. * The barest tag possible - used by everybody else but can also be used directly
  106. */
  107. function tag($name, $content = null, $options = array(), $type = TAG_SELF_CONTAINED)
  108. {
  109. if (!$name)
  110. throw new Exception("Sorry, need a tag name");
  111. $ret = '<' . $name . _options($options);
  112. switch($type)
  113. {
  114. case TAG_SELF_CONTAINED:
  115. $ret .= ' />';
  116. break;
  117. case TAG_OPEN_ONLY:
  118. $ret .= '>';
  119. if(!empty($content)) $ret .= $content;
  120. break;
  121. default:
  122. $ret .= '>';
  123. if(!empty($content)) $ret .= $content;
  124. $ret .= '</' . $name . '>';
  125. }
  126. return $ret;
  127. }
  128. // ----------------------------------------------------------------------------
  129. // Web Forms
  130. // ----------------------------------------------------------------------------
  131. /**
  132. * Create a standard form opening tag
  133. */
  134. function form_tag($url= '', $options = array())
  135. {
  136. if (!isset($options['method']))
  137. $options['method'] = 'post';
  138. if(!empty($options['multipart']))
  139. $options['enctype'] = 'multipart/form-data';
  140. $options['action'] = url_for($url);
  141. return tag('form', null, $options, TAG_OPEN_ONLY);
  142. }
  143. // ----------------------------------------------------------------------------
  144. // Input tags
  145. // ----------------------------------------------------------------------------
  146. function label_for($id, $label, $options = array())
  147. {
  148. if (is_object($label) && method_exists($label, '__toString'))
  149. $label = $label->__toString();
  150. return tag('label', $label, array_merge(array('for' => _get_id_from_name($id, null)), $options), TAG_OPEN_CLOSE);
  151. }
  152. function input_tag($name, $value = null, $options = array())
  153. {
  154. return tag('input', null, array_merge(array('type' => 'text', 'name' => $name, 'id' => _get_id_from_name($name, $value), 'value' => $value), $options));
  155. }
  156. function input_password_tag($name = 'password', $value = null, $options = array())
  157. {
  158. $options['type'] = 'password';
  159. return input_tag($name, $value, $options);
  160. }
  161. function input_hidden_tag($name, $value = null, $options = array())
  162. {
  163. $options['type'] = 'hidden';
  164. return input_tag($name, $value, $options);
  165. }
  166. function input_file_tag($name, $options = array())
  167. {
  168. $options['type'] = 'file';
  169. return input_tag($name, null, $options);
  170. }
  171. function textarea_tag($name, $content = '', $options = array())
  172. {
  173. return tag(
  174. 'textarea', (is_object($content) ? $content->__toString() : $content),
  175. array_merge(array('name' => $name, 'id' => (empty($options['id']) ? $name : $options['id'])),
  176. $options),
  177. TAG_OPEN_CLOSE);
  178. }
  179. function rich_textarea_tag($name, $content = '', $options = array())
  180. {
  181. $id = (empty($options['id']) ? $name : $options['id']);
  182. $ret = textarea_tag($name, $content, $options);
  183. $prefix = Config::$path;
  184. $ret .= <<<EOB
  185. <script type="text/javascript" src="{$prefix}libs/nicEdit.js"></script>
  186. <script type="text/javascript">
  187. new nicEditor({iconsPath:'{$prefix}libs/nicEditorIcons.gif', bbcode:true}).panelInstance('{$id}');
  188. </script>
  189. EOB;
  190. return $ret;
  191. }
  192. // ----------------------------------------------------------------------------
  193. // Submit buttons
  194. // ----------------------------------------------------------------------------
  195. function submit_tag($value = 'Save changes', $options = array())
  196. {
  197. return tag('input', null, array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value), $options));
  198. }
  199. function submit_image_tag($uri, $options = array())
  200. {
  201. return tag('input', null, array_merge(array('type' => 'image', 'name' => 'commit', 'src' => $uri), $options));
  202. }
  203. function reset_tag($value = 'Reset', $options = array())
  204. {
  205. return tag('input', null, array_merge(array('type' => 'reset', 'name' => 'reset', 'value' => $value), $options));
  206. }
  207. /**
  208. * Returns a link that will invoke a js callback
  209. */
  210. function click_tag($text, $callback)
  211. {
  212. return link_to($text, '#', array('onclick' => 'return '.$callback.'();'));
  213. }
  214. function img_tag($uri, $options)
  215. {
  216. return tag('img', null, array_merge(array('src' => $uri), $options), TAG_SELF_CONTAINED);
  217. }
  218. function click_img_tag($uri, $callback)
  219. {
  220. return tag('img', null, array('src' => $uri, 'onclick' => 'return '.$callback.'();'), TAG_SELF_CONTAINED);
  221. }
  222. function constrained_img_tag($uri = '', $options = array())
  223. {
  224. if(empty($options['width'])) $options['width'] = 100;
  225. if(empty($options['height'])) $options['height'] = 100;
  226. if($options['width'] > $options['height'])
  227. {
  228. $desiredWidth = $options['max_width'];
  229. $desiredHeight = intval($options['height'] * ($desiredWidth / $options['width']));
  230. }
  231. else
  232. {
  233. $desiredHeight = $options['max_height'];
  234. $desiredWidth = intval($options['width'] * ($desiredHeight / $options['height']));
  235. }
  236. $options['width'] = $desiredWidth;
  237. $options['height'] = $desiredHeight;
  238. unset($options['max_width']);
  239. unset($options['max_height']);
  240. return tag('img', null, array_merge(array('src' => $uri), $options), TAG_SELF_CONTAINED);
  241. }
  242. function options_for_select($options = array())
  243. {
  244. $html = '';
  245. foreach ($options as $key => $value)
  246. {
  247. $html .= tag('option', $value, array('value' => $key), TAG_OPEN_CLOSE)."\n";
  248. }
  249. return $html;
  250. }
  251. function select_tag($name, $option_tags = null, $options = array())
  252. {
  253. $id = $name;
  254. if (isset($options['multiple']) && $options['multiple'] && substr($name, -2) !== '[]')
  255. $name .= '[]';
  256. if (is_array($option_tags))
  257. $option_tags = options_for_select($option_tags);
  258. return tag('select', $option_tags,
  259. array_merge(array('name' => $name, 'id' => _get_id_from_name($id)), $options), TAG_OPEN_CLOSE);
  260. }
  261. function radiobutton_tag($name, $value, $checked = false, $options = array())
  262. {
  263. $options = array_merge(array('type' => 'radio', 'name' => $name, 'id' => _get_id_from_name($name.'[]', $value), 'value' => $value), $options);
  264. if ($checked)
  265. $options['checked'] = 'checked';
  266. return tag('input', null, $options);
  267. }
  268. function checkbox_tag($name, $value = '1', $checked = false, $options = array())
  269. {
  270. $options = array_merge(array('type' => 'checkbox', 'name' => $name, 'id' => _get_id_from_name($name, $value), 'value' => $value), $options);
  271. if ($checked)
  272. $options['checked'] = 'checked';
  273. return tag('input', null, $options);
  274. }
  275. function util_checkbox_tag($name, $value=1)
  276. {
  277. return checkbox_tag($name, true, false, array('value' => $value, 'class' => $name.' hide'));
  278. }
  279. function enable_select_multi_tag($name)
  280. {
  281. $ret = <<<EOB
  282. <script type="text/javascript">
  283. $(document).ready(
  284. function()
  285. {
  286. $('.$name').selectMulti();
  287. }
  288. );
  289. </script>
  290. EOB;
  291. return $ret;
  292. }
  293. function reveal_tags_tag($name, $label = null, $options = array())
  294. {
  295. $ret = <<<EOB
  296. <script type="text/javascript">
  297. reveal_$name = function()
  298. {
  299. $('.$name').toggle();
  300. $('.{$name}_extra').toggle();
  301. }
  302. </script>
  303. <span>
  304. EOB;
  305. $cbName = 'reveal_'.$name.'_db';
  306. if($label)
  307. $ret .= "<span class=\"{$name}_extra\">" . $label . "</span>\n";
  308. $ret .= checkbox_tag($cbName, true, false,
  309. array('onclick' => 'reveal_ms();') + $options).
  310. "\n</span>\n";
  311. return $ret;
  312. }
  313. static $flash_upload_form_tag_counter = 0;
  314. function flash_upload_form_tag($uri = '', $options = array())
  315. {
  316. $flash_upload_form_tag_counter ++;
  317. $uri = Config::$path . $uri;
  318. $prefix = Config::$path;
  319. $ret = <<<EOB
  320. <script type="text/javascript" src="{$prefix}libs/jquery.asyncUploader.js"></script>
  321. EOB;
  322. $form_id = 'flash_upload_form_' . $flash_upload_form_tag_counter;
  323. $input_id = 'flash_upload_input_' . $flash_upload_form_tag_counter;
  324. $submit_id = 'flash_upload_submit_' . $flash_upload_form_tag_counter;
  325. $form_fragment = '';
  326. $form_tag_options = array(
  327. 'method'=>'post',
  328. 'id'=>$form_id,
  329. 'enctype'=>'multipart/form-data');
  330. if(isset($options['form_class']))
  331. $form_tag_options['class'] = $options['form_class'];
  332. $ret .= form_tag('user/controlpanel', $form_tag_options) . "\n";
  333. $input_fragment = '';
  334. if(isset($options['input_label']))
  335. $input_fragment .= label_for($input_id, $options['input_label']) . "\n";
  336. $input_fragment .= input_file_tag($input_id);
  337. if(isset($options['input_wrapper']))
  338. $input_fragment = str_replace('*', $input_fragment, $options['input_wrapper']) . "\n";
  339. $form_fragment .= $input_fragment;
  340. $submit_fragment = '';
  341. if(isset($options['magic_field']))
  342. {
  343. if(is_array($options['magic_field']))
  344. $submit_fragment .= input_hidden_tag($options['magic_field']) . "\n"; // FIXME
  345. else
  346. $submit_fragment .= input_hidden_tag($options['magic_field']) . "\n";
  347. }
  348. $submit_tag_options = array('id'=>$submit_id);
  349. if(isset($options['submit_class']))
  350. $submit_tag_options['class'] = $options['submit_class'];
  351. $submit_button_caption = isset($options['submit_caption']) ? $options['submit_caption'] : 'Submit';
  352. $submit_fragment .= submit_tag($submit_button_caption, $submit_tag_options);
  353. if(isset($options['submit_wrapper']))
  354. $submit_fragment = str_replace('*', $submit_fragment, $options['submit_wrapper']) . "\n";
  355. $form_fragment .= $submit_fragment;
  356. if(isset($options['feedback_id']))
  357. $feedback_id = $options['feedback_id'];
  358. if(isset($options['form_wrapper']))
  359. $form_fragment = str_replace('*', $form_fragment, $options['form_wrapper']);
  360. if(isset($options['loading_message']))
  361. $loading_msg = $options['loading_message'];
  362. else
  363. $loading_msg = 'Uploading, please wait.';
  364. if(isset($options['complete_message']))
  365. $complete_msg = $options['complete_message'];
  366. else
  367. $complete_msg = 'File Uploaded.';
  368. if(isset($options['callback']))
  369. $callback_name = $options['callback'];
  370. else
  371. $callback_name = 'null';
  372. $ret .= <<<EOB
  373. {$form_fragment}
  374. </form>
  375. <script type="text/javascript">
  376. $('#{$form_id}').handleUpload({
  377. submitButtonId: '{$submit_id}',
  378. fileHandlerUrl: '{$uri}',
  379. printTxtIn: '{$feedback_id}',
  380. loadingMsgs: '{$loading_msg}',
  381. completeMsg: '{$complete_msg}',
  382. callback: {$callback_name}
  383. });
  384. </script>
  385. EOB;
  386. return $ret;
  387. }
  388. /**
  389. * Special Tags
  390. */
  391. function attachments_tag($frame)
  392. {
  393. $url = url_for('file/attachments/');
  394. $ret = <<<EOB
  395. <style type="text/css">
  396. #$frame {
  397. width: 100%;
  398. height: 280px;
  399. border: 0;
  400. }
  401. </style>
  402. <script type="text/javascript">
  403. var revealed_attachments = false;
  404. function reveal_attachments()
  405. {
  406. $('#$frame').toggle();
  407. if(revealed_attachments) return false;
  408. revealed_attachments = true;
  409. window.frames['$frame'].location = "$url";
  410. return false;
  411. }
  412. </script>
  413. EOB;
  414. $ret .= click_tag('Attachments', 'reveal_attachments');
  415. return $ret;
  416. }
  417. function emoticons_tag($frame)
  418. {
  419. $url = url_for('file/emoticons/');
  420. $ret = <<<EOB
  421. <style type="text/css">
  422. #$frame {
  423. width: 100%;
  424. height: 280px;
  425. border: 0;
  426. }
  427. </style>
  428. <script type="text/javascript">
  429. var revealed_emoticons = false;
  430. function reveal_emoticons()
  431. {
  432. $('#$frame').toggle();
  433. if(revealed_emoticons) return false;
  434. revealed_emoticons = true;
  435. window.frames['$frame'].location = "$url";
  436. return false;
  437. }
  438. </script>
  439. EOB;
  440. $ret .= click_tag('Emoticons', 'reveal_emoticons');
  441. return $ret;
  442. }
  443. ?>