options.php 29 KB

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