options.php 33 KB

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