options.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  423. }
  424. /**
  425. * Create selection box or radio button group
  426. *
  427. * When $this->htmlencoded is TRUE, the keys and values in
  428. * $this->possible_values are assumed to be display-safe.
  429. * Use with care!
  430. *
  431. * Note that when building radio buttons instead of a select
  432. * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
  433. * radio buttons will be output one after another without
  434. * linebreaks between them. Otherwise, each radio button
  435. * goes on a line of its own.
  436. *
  437. * @param boolean $multiple_select When TRUE, the select widget
  438. * will allow multiple selections
  439. * (OPTIONAL; default is FALSE
  440. * (single select list))
  441. * @param boolean $radio_buttons When TRUE, the widget will
  442. * instead be built as a group
  443. * of radio buttons (and
  444. * $multiple_select will be
  445. * forced to FALSE) (OPTIONAL;
  446. * default is FALSE (select widget))
  447. *
  448. * @return string html formated selection box or radio buttons
  449. *
  450. */
  451. function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
  452. //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
  453. // radio buttons instead of select widget?
  454. //
  455. if ($radio_buttons) {
  456. global $br, $nbsp;
  457. $result = '';
  458. foreach ($this->possible_values as $real_value => $disp_value) {
  459. $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);
  460. if ($this->size != SMOPT_SIZE_TINY)
  461. $result .= $br;
  462. }
  463. return $result;
  464. }
  465. // everything below applies to select widgets
  466. //
  467. switch ($this->size) {
  468. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  469. case SMOPT_SIZE_TINY:
  470. $height = 3;
  471. break;
  472. case SMOPT_SIZE_SMALL:
  473. $height = 8;
  474. break;
  475. case SMOPT_SIZE_LARGE:
  476. $height = 15;
  477. break;
  478. case SMOPT_SIZE_HUGE:
  479. $height = 25;
  480. break;
  481. case SMOPT_SIZE_NORMAL:
  482. default:
  483. $height = 5;
  484. }
  485. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text);
  486. }
  487. /**
  488. * Create folder selection box
  489. *
  490. * @param boolean $multiple_select When TRUE, the select widget
  491. * will allow multiple selections
  492. * (OPTIONAL; default is FALSE
  493. * (single select list))
  494. *
  495. * @return string html formated selection box
  496. *
  497. */
  498. function createWidget_FolderList($multiple_select=FALSE) {
  499. switch ($this->size) {
  500. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  501. case SMOPT_SIZE_TINY:
  502. $height = 3;
  503. break;
  504. case SMOPT_SIZE_SMALL:
  505. $height = 8;
  506. break;
  507. case SMOPT_SIZE_LARGE:
  508. $height = 15;
  509. break;
  510. case SMOPT_SIZE_HUGE:
  511. $height = 25;
  512. break;
  513. case SMOPT_SIZE_NORMAL:
  514. default:
  515. $height = 5;
  516. }
  517. // possible values might include a nested array of
  518. // possible values (list of folders)
  519. //
  520. $option_list = array();
  521. foreach ($this->possible_values as $value => $text) {
  522. // list of folders (boxes array)
  523. //
  524. if (is_array($text)) {
  525. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
  526. // just one option here
  527. //
  528. } else {
  529. $option_list = array_merge($option_list, array($value => $text));
  530. }
  531. }
  532. if (empty($option_list))
  533. $option_list = array('ignore' => _("unavailable"));
  534. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
  535. }
  536. /**
  537. * Creates textarea
  538. * @return string html formated textarea field
  539. */
  540. function createWidget_TextArea() {
  541. switch ($this->size) {
  542. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  543. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  544. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  545. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  546. case SMOPT_SIZE_NORMAL:
  547. default: $rows = 5; $cols = 50;
  548. }
  549. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  550. }
  551. /**
  552. * Creates field for integer
  553. *
  554. * Difference from createWidget_String is visible only when javascript is enabled
  555. * @return string html formated option field
  556. */
  557. function createWidget_Integer() {
  558. // add onChange javascript handler to a regular string widget
  559. // which will strip out all non-numeric chars
  560. if (checkForJavascript())
  561. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  562. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  563. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  564. . 'this.value=newVal;';
  565. return $this->createWidget_String();
  566. }
  567. /**
  568. * Creates field for floating number
  569. * Difference from createWidget_String is visible only when javascript is enabled
  570. * @return string html formated option field
  571. */
  572. function createWidget_Float() {
  573. // add onChange javascript handler to a regular string widget
  574. // which will strip out all non-numeric (period also OK) chars
  575. if (checkForJavascript())
  576. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  577. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  578. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  579. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  580. return $this->createWidget_String();
  581. }
  582. /**
  583. * Create boolean widget
  584. *
  585. * When creating Yes/No radio buttons, the "yes_text"
  586. * and "no_text" option attributes are used to override
  587. * the typical "Yes" and "No" text.
  588. *
  589. * @param boolean $checkbox When TRUE, the widget will be
  590. * constructed as a checkbox,
  591. * otherwise it will be a set of
  592. * Yes/No radio buttons (OPTIONAL;
  593. * default is TRUE (checkbox)).
  594. *
  595. * @return string html formated boolean widget
  596. *
  597. */
  598. function createWidget_Boolean($checkbox=TRUE) {
  599. global $oTemplate, $nbsp;
  600. // checkbox...
  601. //
  602. if ($checkbox) {
  603. $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);
  604. }
  605. // radio buttons...
  606. //
  607. else {
  608. /* Build the yes choice. */
  609. $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');
  610. /* Build the no choice. */
  611. $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');
  612. /* Build the combined "boolean widget". */
  613. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  614. }
  615. return ($result);
  616. }
  617. /**
  618. * Creates hidden field
  619. * @return string html formated hidden input field
  620. */
  621. function createWidget_Hidden() {
  622. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  623. }
  624. /**
  625. * Creates comment
  626. * @return string comment
  627. */
  628. function createWidget_Comment() {
  629. $result = $this->comment;
  630. return ($result);
  631. }
  632. /**
  633. * Creates an edit list
  634. *
  635. * Note that multiple layout types are supported for this widget.
  636. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  637. * constants.
  638. *
  639. * @return string html formated list of edit fields and
  640. * their associated controls
  641. */
  642. function createWidget_EditList() {
  643. global $oTemplate;
  644. switch ($this->size) {
  645. case SMOPT_SIZE_TINY:
  646. $height = 3;
  647. break;
  648. case SMOPT_SIZE_SMALL:
  649. $height = 8;
  650. break;
  651. case SMOPT_SIZE_MEDIUM:
  652. $height = 15;
  653. break;
  654. case SMOPT_SIZE_LARGE:
  655. $height = 25;
  656. break;
  657. case SMOPT_SIZE_HUGE:
  658. $height = 40;
  659. break;
  660. case SMOPT_SIZE_NORMAL:
  661. default:
  662. $height = 5;
  663. }
  664. if (empty($this->possible_values)) $this->possible_values = array();
  665. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  666. //FIXME: $this->aExtraAttribs probably should only be used in one place
  667. $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
  668. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  669. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  670. $oTemplate->assign('trailing_text', $this->trailing_text);
  671. $oTemplate->assign('possible_values', $this->possible_values);
  672. $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));
  673. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  674. $oTemplate->assign('name', $this->name);
  675. switch ($this->layout_type) {
  676. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  677. return $oTemplate->fetch('edit_list_widget.tpl');
  678. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  679. return $oTemplate->fetch('edit_list_widget_list_style.tpl');
  680. default:
  681. error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $layout_type));
  682. }
  683. }
  684. /**
  685. * Creates a submit button
  686. *
  687. * @return string html formated submit button widget
  688. *
  689. */
  690. function createWidget_Submit() {
  691. return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  692. }
  693. /**
  694. *
  695. */
  696. function save() {
  697. $function = $this->save_function;
  698. $function($this);
  699. }
  700. /**
  701. *
  702. */
  703. function changed() {
  704. // edit lists have a lot going on, so we'll always process them
  705. //
  706. if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
  707. return ($this->value != $this->new_value);
  708. }
  709. } /* End of SquirrelOption class*/
  710. /**
  711. * Saves the option value (this is the default save function
  712. * unless overridden by the user)
  713. *
  714. * @param object $option object that holds option name and new_value
  715. */
  716. function save_option($option) {
  717. // Can't save the pref if we don't have the username
  718. //
  719. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  720. return;
  721. }
  722. global $data_dir;
  723. // edit lists: first add new elements to list, then
  724. // remove any selected ones (note that we must add
  725. // before deleting because the javascript that populates
  726. // the "add" textbox when selecting items in the list
  727. // (for deletion))
  728. //
  729. if ($option->type == SMOPT_TYPE_EDIT_LIST) {
  730. if (empty($option->possible_values)) $option->possible_values = array();
  731. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  732. // add element if given
  733. //
  734. if ((isset($option->use_add_widget) && $option->use_add_widget)
  735. && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
  736. $new_element = trim($new_element);
  737. if (!empty($new_element)
  738. && !in_array($new_element, $option->possible_values))
  739. $option->possible_values[] = $new_element;
  740. }
  741. // delete selected elements if needed
  742. //
  743. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  744. && is_array($option->new_value)
  745. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
  746. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  747. // save full list (stored in "possible_values")
  748. //
  749. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  750. // Certain option types need to be serialized because
  751. // they are not scalar
  752. //
  753. } else if ($option->is_multiple_valued())
  754. setPref($data_dir, $username, $option->name, serialize($option->new_value));
  755. // Checkboxes, when unchecked, don't submit anything in
  756. // the POST, so set to SMPREF_OFF if not found
  757. //
  758. else if (($option->type == SMOPT_TYPE_BOOLEAN
  759. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  760. && empty($option->new_value))
  761. setPref($data_dir, $username, $option->name, SMPREF_OFF);
  762. else
  763. setPref($data_dir, $username, $option->name, $option->new_value);
  764. // if a checkbox or multi select is zeroed/cleared out, it
  765. // needs to have an empty value pushed into its "new_value" slot
  766. //
  767. if (($option->type == SMOPT_TYPE_STRLIST_MULTI
  768. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  769. && is_null($option->new_value))
  770. $option->new_value = '';
  771. }
  772. /**
  773. * save function that does not save
  774. * @param object $option
  775. */
  776. function save_option_noop($option) {
  777. /* Do nothing here... */
  778. }
  779. /**
  780. * Create hidden 'optpage' input field with value set by argument
  781. * @param string $optpage identification of option page
  782. * @return string html formated hidden input field
  783. */
  784. function create_optpage_element($optpage) {
  785. return addHidden('optpage', $optpage);
  786. }
  787. /**
  788. * Create hidden 'optmode' input field with value set by argument
  789. * @param string $optmode
  790. * @return string html formated hidden input field
  791. */
  792. function create_optmode_element($optmode) {
  793. return addHidden('optmode', $optmode);
  794. }
  795. /**
  796. * @param array $optgrps
  797. * @param array $optvals
  798. * @return array
  799. */
  800. function create_option_groups($optgrps, $optvals) {
  801. /* Build a simple array with which to start. */
  802. $result = array();
  803. /* Create option group for each option group name. */
  804. foreach ($optgrps as $grpkey => $grpname) {
  805. $result[$grpkey] = array();
  806. $result[$grpkey]['name'] = $grpname;
  807. $result[$grpkey]['options'] = array();
  808. }
  809. /* Create a new SquirrelOption for each set of option values. */
  810. foreach ($optvals as $grpkey => $grpopts) {
  811. foreach ($grpopts as $optset) {
  812. /* Create a new option with all values given. */
  813. $next_option = new SquirrelOption(
  814. $optset,
  815. $optset['name'],
  816. $optset['caption'],
  817. $optset['type'],
  818. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  819. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  820. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  821. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  822. );
  823. /* If provided, set if the caption is allowed to wrap for this option. */
  824. if (isset($optset['caption_wrap'])) {
  825. $next_option->setCaptionWrap($optset['caption_wrap']);
  826. }
  827. /* If provided, set the size for this option. */
  828. if (isset($optset['size'])) {
  829. $next_option->setSize($optset['size']);
  830. }
  831. /* If provided, set the trailing_text for this option. */
  832. if (isset($optset['trailing_text'])) {
  833. $next_option->setTrailingText($optset['trailing_text']);
  834. }
  835. /* If provided, set the yes_text for this option. */
  836. if (isset($optset['yes_text'])) {
  837. $next_option->setYesText($optset['yes_text']);
  838. }
  839. /* If provided, set the no_text for this option. */
  840. if (isset($optset['no_text'])) {
  841. $next_option->setNoText($optset['no_text']);
  842. }
  843. /* If provided, set the layout type for this option. */
  844. if (isset($optset['layout_type'])) {
  845. $next_option->setLayoutType($optset['layout_type']);
  846. }
  847. /* If provided, set the use_add_widget value for this option. */
  848. if (isset($optset['use_add_widget'])) {
  849. $next_option->setUseAddWidget($optset['use_add_widget']);
  850. }
  851. /* If provided, set the use_delete_widget value for this option. */
  852. if (isset($optset['use_delete_widget'])) {
  853. $next_option->setUseDeleteWidget($optset['use_delete_widget']);
  854. }
  855. /* If provided, set the comment for this option. */
  856. if (isset($optset['comment'])) {
  857. $next_option->setComment($optset['comment']);
  858. }
  859. /* If provided, set the save function for this option. */
  860. if (isset($optset['save'])) {
  861. $next_option->setSaveFunction($optset['save']);
  862. }
  863. /* If provided, set the extra attributes for this option. */
  864. if (isset($optset['extra_attributes'])) {
  865. $next_option->setExtraAttributes($optset['extra_attributes']);
  866. }
  867. /* If provided, set the "post script" for this option. */
  868. if (isset($optset['post_script'])) {
  869. $next_option->setPostScript($optset['post_script']);
  870. }
  871. /* If provided, set the folder_filter for this option. */
  872. if (isset($optset['folder_filter'])) {
  873. $next_option->setFolderFilter($optset['folder_filter']);
  874. }
  875. /* Add this option to the option array. */
  876. $result[$grpkey]['options'][] = $next_option;
  877. }
  878. }
  879. /* Return our resulting array. */
  880. return ($result);
  881. }