options.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Functions needed to display the options pages.
  6. *
  7. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage prefs
  12. */
  13. /**
  14. * SquirrelOption: An option for SquirrelMail.
  15. *
  16. * @package squirrelmail
  17. * @subpackage prefs
  18. */
  19. class SquirrelOption {
  20. /**
  21. * The original option configuration array
  22. * @var array
  23. */
  24. var $raw_option_array;
  25. /**
  26. * The name of this setting
  27. * @var string
  28. */
  29. var $name;
  30. /**
  31. * The text that prefaces setting on the preferences page
  32. * @var string
  33. */
  34. var $caption;
  35. /**
  36. * Whether or not the caption text is allowed to wrap
  37. * @var boolean
  38. */
  39. var $caption_wrap;
  40. /**
  41. * The type of INPUT element
  42. *
  43. * See SMOPT_TYPE_* defines
  44. * @var integer
  45. */
  46. var $type;
  47. /**
  48. * Indicates if a link should be shown to refresh part
  49. * or all of the window
  50. *
  51. * See SMOPT_REFRESH_* defines
  52. * @var integer
  53. */
  54. var $refresh_level;
  55. /**
  56. * Specifies the size of certain input items
  57. *
  58. * See SMOPT_SIZE_* defines
  59. * @var integer
  60. */
  61. var $size;
  62. /**
  63. * Text that follows a text input or
  64. * select list input on the preferences page
  65. *
  66. * useful for indicating units, meanings of special values, etc.
  67. * @var string
  68. */
  69. var $trailing_text;
  70. /**
  71. * Text that overrides the "Yes" label for boolean
  72. * radio option widgets
  73. *
  74. * @var string
  75. */
  76. var $yes_text;
  77. /**
  78. * Text that overrides the "No" label for boolean
  79. * radio option widgets
  80. *
  81. * @var string
  82. */
  83. var $no_text;
  84. /**
  85. * Some widgets support more than one layout type
  86. *
  87. * @var int
  88. */
  89. var $layout_type;
  90. /**
  91. * Indicates if the Add widget should be included
  92. * with edit lists.
  93. *
  94. * @var boolean
  95. */
  96. var $use_add_widget;
  97. /**
  98. * Indicates if the Delete widget should be included
  99. * with edit lists.
  100. *
  101. * @var boolean
  102. */
  103. var $use_delete_widget;
  104. /**
  105. * text displayed to the user
  106. *
  107. * Used with SMOPT_TYPE_COMMENT options
  108. * @var string
  109. */
  110. var $comment;
  111. /**
  112. * additional javascript or other widget attributes added to the
  113. * user input; must be an array where keys are attribute names
  114. * ("onclick", etc) and values are the attribute values.
  115. * @var array
  116. */
  117. var $aExtraAttribs;
  118. /**
  119. * script (usually Javascript) that will be placed after (outside of)
  120. * the INPUT tag
  121. * @var string
  122. */
  123. var $post_script;
  124. /**
  125. * The name of the Save Function for this option.
  126. * @var string
  127. */
  128. var $save_function;
  129. /* The various 'values' for this options. */
  130. /**
  131. * default/preselected value for this option
  132. * @var mixed
  133. */
  134. var $value;
  135. /**
  136. * new option value
  137. * @var mixed
  138. */
  139. var $new_value;
  140. /**
  141. * associative array, where each key is an actual input value
  142. * and the corresponding value is what is displayed to the user
  143. * for that list item in the drop-down list
  144. * @var array
  145. */
  146. var $possible_values;
  147. /**
  148. * disables html sanitizing.
  149. *
  150. * WARNING - don't use it, if user input is possible in option
  151. * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
  152. * @var bool
  153. */
  154. var $htmlencoded=false;
  155. /**
  156. * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
  157. * SMOPT_TYPE_FLDRLIST_MULTI widgets.
  158. * See $flag argument in sqimap_mailbox_option_list() function.
  159. * @var string
  160. * @since 1.5.1
  161. */
  162. var $folder_filter='noselect';
  163. /**
  164. * Constructor function
  165. * @param array $raw_option_array
  166. * @param string $name
  167. * @param string $caption
  168. * @param integer $type
  169. * @param integer $refresh_level
  170. * @param mixed $initial_value
  171. * @param array $possible_values
  172. * @param bool $htmlencoded
  173. */
  174. function SquirrelOption
  175. ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  176. /* Set the basic stuff. */
  177. $this->raw_option_array = $raw_option_array;
  178. $this->name = $name;
  179. $this->caption = $caption;
  180. $this->caption_wrap = TRUE;
  181. $this->type = $type;
  182. $this->refresh_level = $refresh_level;
  183. $this->possible_values = $possible_values;
  184. $this->htmlencoded = $htmlencoded;
  185. $this->size = SMOPT_SIZE_NORMAL;
  186. $this->trailing_text = '';
  187. $this->yes_text = '';
  188. $this->no_text = '';
  189. $this->comment = '';
  190. $this->layout_type = 0;
  191. $this->use_add_widget = TRUE;
  192. $this->use_delete_widget = TRUE;
  193. $this->aExtraAttribs = array();
  194. $this->post_script = '';
  195. //Check for a current value.
  196. if (isset($GLOBALS[$name])) {
  197. $this->value = $GLOBALS[$name];
  198. } else if (!empty($initial_value)) {
  199. $this->value = $initial_value;
  200. } else {
  201. $this->value = '';
  202. }
  203. /* Check for a new value. */
  204. if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  205. $this->new_value = NULL;
  206. }
  207. /* Set the default save function. */
  208. if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
  209. $this->save_function = SMOPT_SAVE_DEFAULT;
  210. } else {
  211. $this->save_function = SMOPT_SAVE_NOOP;
  212. }
  213. }
  214. /** Convenience function that identifies which types of
  215. widgets are stored as (serialized) array values. */
  216. function is_multiple_valued() {
  217. return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
  218. || $this->type == SMOPT_TYPE_STRLIST_MULTI
  219. || $this->type == SMOPT_TYPE_EDIT_LIST);
  220. }
  221. /**
  222. * Set the value for this option.
  223. * @param mixed $value
  224. */
  225. function setValue($value) {
  226. $this->value = $value;
  227. }
  228. /**
  229. * Set the new value for this option.
  230. * @param mixed $new_value
  231. */
  232. function setNewValue($new_value) {
  233. $this->new_value = $new_value;
  234. }
  235. /**
  236. * Set whether the caption is allowed to wrap for this option.
  237. * @param boolean $caption_wrap
  238. */
  239. function setCaptionWrap($caption_wrap) {
  240. $this->caption_wrap = $caption_wrap;
  241. }
  242. /**
  243. * Set the size for this option.
  244. * @param integer $size
  245. */
  246. function setSize($size) {
  247. $this->size = $size;
  248. }
  249. /**
  250. * Set the trailing_text for this option.
  251. * @param string $trailing_text
  252. */
  253. function setTrailingText($trailing_text) {
  254. $this->trailing_text = $trailing_text;
  255. }
  256. /**
  257. * Set the yes_text for this option.
  258. * @param string $yes_text
  259. */
  260. function setYesText($yes_text) {
  261. $this->yes_text = $yes_text;
  262. }
  263. /**
  264. * Set the no_text for this option.
  265. * @param string $no_text
  266. */
  267. function setNoText($no_text) {
  268. $this->no_text = $no_text;
  269. }
  270. /* Set the "use add widget" value for this option. */
  271. function setUseAddWidget($use_add_widget) {
  272. $this->use_add_widget = $use_add_widget;
  273. }
  274. /* Set the "use delete widget" value for this option. */
  275. function setUseDeleteWidget($use_delete_widget) {
  276. $this->use_delete_widget = $use_delete_widget;
  277. }
  278. /**
  279. * Set the layout type for this option.
  280. * @param int $layout_type
  281. */
  282. function setLayoutType($layout_type) {
  283. $this->layout_type = $layout_type;
  284. }
  285. /**
  286. * Set the comment for this option.
  287. * @param string $comment
  288. */
  289. function setComment($comment) {
  290. $this->comment = $comment;
  291. }
  292. /**
  293. * Set the extra attributes for this option.
  294. * @param array $aExtraAttribs
  295. */
  296. function setExtraAttributes($aExtraAttribs) {
  297. $this->aExtraAttribs = $aExtraAttribs;
  298. }
  299. /**
  300. * Set the "post script" for this option.
  301. * @param string $post_script
  302. */
  303. function setPostScript($post_script) {
  304. $this->post_script = $post_script;
  305. }
  306. /**
  307. * Set the save function for this option.
  308. * @param string $save_function
  309. */
  310. function setSaveFunction($save_function) {
  311. $this->save_function = $save_function;
  312. }
  313. /**
  314. * Set the folder_filter for this option.
  315. * @param string $folder_filter
  316. * @since 1.5.1
  317. */
  318. function setFolderFilter($folder_filter) {
  319. $this->folder_filter = $folder_filter;
  320. }
  321. /**
  322. * Creates fields on option pages according to option type
  323. *
  324. * This is the function that calls all other createWidget* functions.
  325. *
  326. * @return string The formated option field
  327. *
  328. */
  329. function createWidget() {
  330. global $color;
  331. // Use new value if available
  332. if (!is_null($this->new_value)) {
  333. $tempValue = $this->value;
  334. $this->value = $this->new_value;
  335. }
  336. /* Get the widget for this option type. */
  337. switch ($this->type) {
  338. case SMOPT_TYPE_STRING:
  339. $result = $this->createWidget_String();
  340. break;
  341. case SMOPT_TYPE_STRLIST:
  342. $result = $this->createWidget_StrList();
  343. break;
  344. case SMOPT_TYPE_TEXTAREA:
  345. $result = $this->createWidget_TextArea();
  346. break;
  347. case SMOPT_TYPE_INTEGER:
  348. $result = $this->createWidget_Integer();
  349. break;
  350. case SMOPT_TYPE_FLOAT:
  351. $result = $this->createWidget_Float();
  352. break;
  353. case SMOPT_TYPE_BOOLEAN:
  354. $result = $this->createWidget_Boolean();
  355. break;
  356. case SMOPT_TYPE_BOOLEAN_CHECKBOX:
  357. $result = $this->createWidget_Boolean(TRUE);
  358. break;
  359. case SMOPT_TYPE_BOOLEAN_RADIO:
  360. $result = $this->createWidget_Boolean(FALSE);
  361. break;
  362. case SMOPT_TYPE_HIDDEN:
  363. $result = $this->createWidget_Hidden();
  364. break;
  365. case SMOPT_TYPE_COMMENT:
  366. $result = $this->createWidget_Comment();
  367. break;
  368. case SMOPT_TYPE_FLDRLIST:
  369. $result = $this->createWidget_FolderList();
  370. break;
  371. case SMOPT_TYPE_FLDRLIST_MULTI:
  372. $result = $this->createWidget_FolderList(TRUE);
  373. break;
  374. case SMOPT_TYPE_EDIT_LIST:
  375. $result = $this->createWidget_EditList();
  376. break;
  377. case SMOPT_TYPE_STRLIST_MULTI:
  378. $result = $this->createWidget_StrList(TRUE);
  379. break;
  380. case SMOPT_TYPE_STRLIST_RADIO:
  381. $result = $this->createWidget_StrList(FALSE, TRUE);
  382. break;
  383. case SMOPT_TYPE_SUBMIT:
  384. $result = $this->createWidget_Submit();
  385. break;
  386. default:
  387. error_box (
  388. sprintf(_("Option Type '%s' Not Found"), $this->type)
  389. );
  390. }
  391. /* Add the "post script" for this option. */
  392. $result .= $this->post_script;
  393. // put correct value back if need be
  394. if (!is_null($this->new_value)) {
  395. $this->value = $tempValue;
  396. }
  397. /* Now, return the created widget. */
  398. return $result;
  399. }
  400. /**
  401. * Create string field
  402. * @return string html formated option field
  403. */
  404. function createWidget_String() {
  405. switch ($this->size) {
  406. case SMOPT_SIZE_TINY:
  407. $width = 5;
  408. break;
  409. case SMOPT_SIZE_SMALL:
  410. $width = 12;
  411. break;
  412. case SMOPT_SIZE_LARGE:
  413. $width = 38;
  414. break;
  415. case SMOPT_SIZE_HUGE:
  416. $width = 50;
  417. break;
  418. case SMOPT_SIZE_NORMAL:
  419. default:
  420. $width = 25;
  421. }
  422. //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here
  423. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . htmlspecialchars($this->trailing_text);
  424. }
  425. /**
  426. * Create selection box or radio button group
  427. *
  428. * When $this->htmlencoded is TRUE, the keys and values in
  429. * $this->possible_values are assumed to be display-safe.
  430. * Use with care!
  431. *
  432. * Note that when building radio buttons instead of a select
  433. * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
  434. * radio buttons will be output one after another without
  435. * linebreaks between them. Otherwise, each radio button
  436. * goes on a line of its own.
  437. *
  438. * @param boolean $multiple_select When TRUE, the select widget
  439. * will allow multiple selections
  440. * (OPTIONAL; default is FALSE
  441. * (single select list))
  442. * @param boolean $radio_buttons When TRUE, the widget will
  443. * instead be built as a group
  444. * of radio buttons (and
  445. * $multiple_select will be
  446. * forced to FALSE) (OPTIONAL;
  447. * default is FALSE (select widget))
  448. *
  449. * @return string html formated selection box or radio buttons
  450. *
  451. */
  452. function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
  453. //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
  454. // radio buttons instead of select widget?
  455. //
  456. if ($radio_buttons) {
  457. global $br, $nbsp;
  458. $result = '';
  459. foreach ($this->possible_values as $real_value => $disp_value) {
  460. $result .= addRadioBox('new_' . $this->name, ($this->value == $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name . '_' . $real_value), $this->aExtraAttribs)) . $nbsp . create_label($disp_value, 'new_' . $this->name . '_' . $real_value);
  461. if ($this->size != SMOPT_SIZE_TINY)
  462. $result .= $br;
  463. }
  464. return $result;
  465. }
  466. // everything below applies to select widgets
  467. //
  468. switch ($this->size) {
  469. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  470. case SMOPT_SIZE_TINY:
  471. $height = 3;
  472. break;
  473. case SMOPT_SIZE_SMALL:
  474. $height = 8;
  475. break;
  476. case SMOPT_SIZE_LARGE:
  477. $height = 15;
  478. break;
  479. case SMOPT_SIZE_HUGE:
  480. $height = 25;
  481. break;
  482. case SMOPT_SIZE_NORMAL:
  483. default:
  484. $height = 5;
  485. }
  486. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text);
  487. }
  488. /**
  489. * Create folder selection box
  490. *
  491. * @param boolean $multiple_select When TRUE, the select widget
  492. * will allow multiple selections
  493. * (OPTIONAL; default is FALSE
  494. * (single select list))
  495. *
  496. * @return string html formated selection box
  497. *
  498. */
  499. function createWidget_FolderList($multiple_select=FALSE) {
  500. switch ($this->size) {
  501. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  502. case SMOPT_SIZE_TINY:
  503. $height = 3;
  504. break;
  505. case SMOPT_SIZE_SMALL:
  506. $height = 8;
  507. break;
  508. case SMOPT_SIZE_LARGE:
  509. $height = 15;
  510. break;
  511. case SMOPT_SIZE_HUGE:
  512. $height = 25;
  513. break;
  514. case SMOPT_SIZE_NORMAL:
  515. default:
  516. $height = 5;
  517. }
  518. // possible values might include a nested array of
  519. // possible values (list of folders)
  520. //
  521. $option_list = array();
  522. foreach ($this->possible_values as $value => $text) {
  523. // list of folders (boxes array)
  524. //
  525. if (is_array($text)) {
  526. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
  527. // just one option here
  528. //
  529. } else {
  530. $option_list = array_merge($option_list, array($value => $text));
  531. }
  532. }
  533. if (empty($option_list))
  534. $option_list = array('ignore' => _("unavailable"));
  535. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
  536. }
  537. /**
  538. * Creates textarea
  539. * @return string html formated textarea field
  540. */
  541. function createWidget_TextArea() {
  542. switch ($this->size) {
  543. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  544. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  545. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  546. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  547. case SMOPT_SIZE_NORMAL:
  548. default: $rows = 5; $cols = 50;
  549. }
  550. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  551. }
  552. /**
  553. * Creates field for integer
  554. *
  555. * Difference from createWidget_String is visible only when javascript is enabled
  556. * @return string html formated option field
  557. */
  558. function createWidget_Integer() {
  559. // add onChange javascript handler to a regular string widget
  560. // which will strip out all non-numeric chars
  561. if (checkForJavascript())
  562. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  563. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  564. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  565. . 'this.value=newVal;';
  566. return $this->createWidget_String();
  567. }
  568. /**
  569. * Creates field for floating number
  570. * Difference from createWidget_String is visible only when javascript is enabled
  571. * @return string html formated option field
  572. */
  573. function createWidget_Float() {
  574. // add onChange javascript handler to a regular string widget
  575. // which will strip out all non-numeric (period also OK) chars
  576. if (checkForJavascript())
  577. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  578. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  579. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  580. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  581. return $this->createWidget_String();
  582. }
  583. /**
  584. * Create boolean widget
  585. *
  586. * When creating Yes/No radio buttons, the "yes_text"
  587. * and "no_text" option attributes are used to override
  588. * the typical "Yes" and "No" text.
  589. *
  590. * @param boolean $checkbox When TRUE, the widget will be
  591. * constructed as a checkbox,
  592. * otherwise it will be a set of
  593. * Yes/No radio buttons (OPTIONAL;
  594. * default is TRUE (checkbox)).
  595. *
  596. * @return string html formated boolean widget
  597. *
  598. */
  599. function createWidget_Boolean($checkbox=TRUE) {
  600. global $oTemplate, $nbsp;
  601. // checkbox...
  602. //
  603. if ($checkbox) {
  604. $result = addCheckbox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name), $this->aExtraAttribs)) . $nbsp . create_label($this->trailing_text, 'new_' . $this->name);
  605. }
  606. // radio buttons...
  607. //
  608. else {
  609. /* Build the yes choice. */
  610. $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->yes_text) ? $this->yes_text : _("Yes")), 'new_' . $this->name . '_yes');
  611. /* Build the no choice. */
  612. $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->no_text) ? $this->no_text : _("No")), 'new_' . $this->name . '_no');
  613. /* Build the combined "boolean widget". */
  614. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  615. }
  616. return ($result);
  617. }
  618. /**
  619. * Creates hidden field
  620. * @return string html formated hidden input field
  621. */
  622. function createWidget_Hidden() {
  623. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  624. }
  625. /**
  626. * Creates comment
  627. * @return string comment
  628. */
  629. function createWidget_Comment() {
  630. $result = $this->comment;
  631. return ($result);
  632. }
  633. /**
  634. * Creates an edit list
  635. *
  636. * Note that multiple layout types are supported for this widget.
  637. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  638. * constants.
  639. *
  640. * @return string html formated list of edit fields and
  641. * their associated controls
  642. */
  643. function createWidget_EditList() {
  644. global $oTemplate;
  645. switch ($this->size) {
  646. case SMOPT_SIZE_TINY:
  647. $height = 3;
  648. break;
  649. case SMOPT_SIZE_SMALL:
  650. $height = 8;
  651. break;
  652. case SMOPT_SIZE_MEDIUM:
  653. $height = 15;
  654. break;
  655. case SMOPT_SIZE_LARGE:
  656. $height = 25;
  657. break;
  658. case SMOPT_SIZE_HUGE:
  659. $height = 40;
  660. break;
  661. case SMOPT_SIZE_NORMAL:
  662. default:
  663. $height = 5;
  664. }
  665. if (empty($this->possible_values)) $this->possible_values = array();
  666. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  667. //FIXME: $this->aExtraAttribs probably should only be used in one place
  668. $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
  669. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  670. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  671. $oTemplate->assign('trailing_text', $this->trailing_text);
  672. $oTemplate->assign('possible_values', $this->possible_values);
  673. $oTemplate->assign('select_widget', addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput_' . $this->name . ') == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name . '\' ) { pos = j; } j++; } i++; } if( pos >= 0 ) { window.addinput_' . $this->name . ' = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput_' . $this->name . '.value = this.options[x].value; break; } }'), $this->aExtraAttribs), TRUE, $height));
  674. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  675. $oTemplate->assign('name', $this->name);
  676. switch ($this->layout_type) {
  677. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  678. return $oTemplate->fetch('edit_list_widget.tpl');
  679. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  680. return $oTemplate->fetch('edit_list_widget_list_style.tpl');
  681. default:
  682. error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $layout_type));
  683. }
  684. }
  685. /**
  686. * Creates a submit button
  687. *
  688. * @return string html formated submit button widget
  689. *
  690. */
  691. function createWidget_Submit() {
  692. return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  693. }
  694. /**
  695. *
  696. */
  697. function save() {
  698. $function = $this->save_function;
  699. $function($this);
  700. }
  701. /**
  702. *
  703. */
  704. function changed() {
  705. // edit lists have a lot going on, so we'll always process them
  706. //
  707. if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
  708. return ($this->value != $this->new_value);
  709. }
  710. } /* End of SquirrelOption class*/
  711. /**
  712. * Saves the option value (this is the default save function
  713. * unless overridden by the user)
  714. *
  715. * @param object $option object that holds option name and new_value
  716. */
  717. function save_option($option) {
  718. // Can't save the pref if we don't have the username
  719. //
  720. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  721. return;
  722. }
  723. global $data_dir;
  724. // edit lists: first add new elements to list, then
  725. // remove any selected ones (note that we must add
  726. // before deleting because the javascript that populates
  727. // the "add" textbox when selecting items in the list
  728. // (for deletion))
  729. //
  730. if ($option->type == SMOPT_TYPE_EDIT_LIST) {
  731. if (empty($option->possible_values)) $option->possible_values = array();
  732. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  733. // add element if given
  734. //
  735. if ((isset($option->use_add_widget) && $option->use_add_widget)
  736. && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
  737. $new_element = trim($new_element);
  738. if (!empty($new_element)
  739. && !in_array($new_element, $option->possible_values))
  740. $option->possible_values[] = $new_element;
  741. }
  742. // delete selected elements if needed
  743. //
  744. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  745. && is_array($option->new_value)
  746. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
  747. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  748. // save full list (stored in "possible_values")
  749. //
  750. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  751. // Certain option types need to be serialized because
  752. // they are not scalar
  753. //
  754. } else if ($option->is_multiple_valued())
  755. setPref($data_dir, $username, $option->name, serialize($option->new_value));
  756. // Checkboxes, when unchecked, don't submit anything in
  757. // the POST, so set to SMPREF_OFF if not found
  758. //
  759. else if (($option->type == SMOPT_TYPE_BOOLEAN
  760. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  761. && empty($option->new_value))
  762. setPref($data_dir, $username, $option->name, SMPREF_OFF);
  763. else
  764. setPref($data_dir, $username, $option->name, $option->new_value);
  765. // if a checkbox or multi select is zeroed/cleared out, it
  766. // needs to have an empty value pushed into its "new_value" slot
  767. //
  768. if (($option->type == SMOPT_TYPE_STRLIST_MULTI
  769. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  770. && is_null($option->new_value))
  771. $option->new_value = '';
  772. }
  773. /**
  774. * save function that does not save
  775. * @param object $option
  776. */
  777. function save_option_noop($option) {
  778. /* Do nothing here... */
  779. }
  780. /**
  781. * Create hidden 'optpage' input field with value set by argument
  782. * @param string $optpage identification of option page
  783. * @return string html formated hidden input field
  784. */
  785. function create_optpage_element($optpage) {
  786. return addHidden('optpage', $optpage);
  787. }
  788. /**
  789. * Create hidden 'optmode' input field with value set by argument
  790. * @param string $optmode
  791. * @return string html formated hidden input field
  792. */
  793. function create_optmode_element($optmode) {
  794. return addHidden('optmode', $optmode);
  795. }
  796. /**
  797. * @param array $optgrps
  798. * @param array $optvals
  799. * @return array
  800. */
  801. function create_option_groups($optgrps, $optvals) {
  802. /* Build a simple array with which to start. */
  803. $result = array();
  804. /* Create option group for each option group name. */
  805. foreach ($optgrps as $grpkey => $grpname) {
  806. $result[$grpkey] = array();
  807. $result[$grpkey]['name'] = $grpname;
  808. $result[$grpkey]['options'] = array();
  809. }
  810. /* Create a new SquirrelOption for each set of option values. */
  811. foreach ($optvals as $grpkey => $grpopts) {
  812. foreach ($grpopts as $optset) {
  813. /* Create a new option with all values given. */
  814. $next_option = new SquirrelOption(
  815. $optset,
  816. $optset['name'],
  817. $optset['caption'],
  818. $optset['type'],
  819. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  820. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  821. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  822. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  823. );
  824. /* If provided, set if the caption is allowed to wrap for this option. */
  825. if (isset($optset['caption_wrap'])) {
  826. $next_option->setCaptionWrap($optset['caption_wrap']);
  827. }
  828. /* If provided, set the size for this option. */
  829. if (isset($optset['size'])) {
  830. $next_option->setSize($optset['size']);
  831. }
  832. /* If provided, set the trailing_text for this option. */
  833. if (isset($optset['trailing_text'])) {
  834. $next_option->setTrailingText($optset['trailing_text']);
  835. }
  836. /* If provided, set the yes_text for this option. */
  837. if (isset($optset['yes_text'])) {
  838. $next_option->setYesText($optset['yes_text']);
  839. }
  840. /* If provided, set the no_text for this option. */
  841. if (isset($optset['no_text'])) {
  842. $next_option->setNoText($optset['no_text']);
  843. }
  844. /* If provided, set the layout type for this option. */
  845. if (isset($optset['layout_type'])) {
  846. $next_option->setLayoutType($optset['layout_type']);
  847. }
  848. /* If provided, set the use_add_widget value for this option. */
  849. if (isset($optset['use_add_widget'])) {
  850. $next_option->setUseAddWidget($optset['use_add_widget']);
  851. }
  852. /* If provided, set the use_delete_widget value for this option. */
  853. if (isset($optset['use_delete_widget'])) {
  854. $next_option->setUseDeleteWidget($optset['use_delete_widget']);
  855. }
  856. /* If provided, set the comment for this option. */
  857. if (isset($optset['comment'])) {
  858. $next_option->setComment($optset['comment']);
  859. }
  860. /* If provided, set the save function for this option. */
  861. if (isset($optset['save'])) {
  862. $next_option->setSaveFunction($optset['save']);
  863. }
  864. /* If provided, set the extra attributes for this option. */
  865. if (isset($optset['extra_attributes'])) {
  866. $next_option->setExtraAttributes($optset['extra_attributes']);
  867. }
  868. /* If provided, set the "post script" for this option. */
  869. if (isset($optset['post_script'])) {
  870. $next_option->setPostScript($optset['post_script']);
  871. }
  872. /* If provided, set the folder_filter for this option. */
  873. if (isset($optset['folder_filter'])) {
  874. $next_option->setFolderFilter($optset['folder_filter']);
  875. }
  876. /* Add this option to the option array. */
  877. $result[$grpkey]['options'][] = $next_option;
  878. }
  879. }
  880. /* Return our resulting array. */
  881. return ($result);
  882. }