options.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Functions needed to display the options pages.
  6. *
  7. * @copyright &copy; 1999-2009 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
  209. && $type != SMOPT_TYPE_INFO
  210. && $type != SMOPT_TYPE_COMMENT) {
  211. $this->save_function = SMOPT_SAVE_DEFAULT;
  212. } else {
  213. $this->save_function = SMOPT_SAVE_NOOP;
  214. }
  215. }
  216. /** Convenience function that identifies which types of
  217. widgets are stored as (serialized) array values. */
  218. function is_multiple_valued() {
  219. return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
  220. || $this->type == SMOPT_TYPE_STRLIST_MULTI
  221. || $this->type == SMOPT_TYPE_EDIT_LIST);
  222. }
  223. /**
  224. * Set the value for this option.
  225. * @param mixed $value
  226. */
  227. function setValue($value) {
  228. $this->value = $value;
  229. }
  230. /**
  231. * Set the new value for this option.
  232. * @param mixed $new_value
  233. */
  234. function setNewValue($new_value) {
  235. $this->new_value = $new_value;
  236. }
  237. /**
  238. * Set whether the caption is allowed to wrap for this option.
  239. * @param boolean $caption_wrap
  240. */
  241. function setCaptionWrap($caption_wrap) {
  242. $this->caption_wrap = $caption_wrap;
  243. }
  244. /**
  245. * Set the size for this option.
  246. * @param integer $size
  247. */
  248. function setSize($size) {
  249. $this->size = $size;
  250. }
  251. /**
  252. * Set the trailing_text for this option.
  253. * @param string $trailing_text
  254. */
  255. function setTrailingText($trailing_text) {
  256. $this->trailing_text = $trailing_text;
  257. }
  258. /**
  259. * Set the yes_text for this option.
  260. * @param string $yes_text
  261. */
  262. function setYesText($yes_text) {
  263. $this->yes_text = $yes_text;
  264. }
  265. /**
  266. * Set the no_text for this option.
  267. * @param string $no_text
  268. */
  269. function setNoText($no_text) {
  270. $this->no_text = $no_text;
  271. }
  272. /* Set the "use add widget" value for this option. */
  273. function setUseAddWidget($use_add_widget) {
  274. $this->use_add_widget = $use_add_widget;
  275. }
  276. /* Set the "use delete widget" value for this option. */
  277. function setUseDeleteWidget($use_delete_widget) {
  278. $this->use_delete_widget = $use_delete_widget;
  279. }
  280. /**
  281. * Set the layout type for this option.
  282. * @param int $layout_type
  283. */
  284. function setLayoutType($layout_type) {
  285. $this->layout_type = $layout_type;
  286. }
  287. /**
  288. * Set the comment for this option.
  289. * @param string $comment
  290. */
  291. function setComment($comment) {
  292. $this->comment = $comment;
  293. }
  294. /**
  295. * Set the extra attributes for this option.
  296. * @param array $aExtraAttribs
  297. */
  298. function setExtraAttributes($aExtraAttribs) {
  299. $this->aExtraAttribs = $aExtraAttribs;
  300. }
  301. /**
  302. * Set the "post script" for this option.
  303. * @param string $post_script
  304. */
  305. function setPostScript($post_script) {
  306. $this->post_script = $post_script;
  307. }
  308. /**
  309. * Set the save function for this option.
  310. * @param string $save_function
  311. */
  312. function setSaveFunction($save_function) {
  313. $this->save_function = $save_function;
  314. }
  315. /**
  316. * Set the folder_filter for this option.
  317. * @param string $folder_filter
  318. * @since 1.5.1
  319. */
  320. function setFolderFilter($folder_filter) {
  321. $this->folder_filter = $folder_filter;
  322. }
  323. /**
  324. * Creates fields on option pages according to option type
  325. *
  326. * This is the function that calls all other createWidget* functions.
  327. *
  328. * @return string The formated option field
  329. *
  330. */
  331. function createWidget() {
  332. global $color;
  333. // Use new value if available
  334. if (!is_null($this->new_value)) {
  335. $tempValue = $this->value;
  336. $this->value = $this->new_value;
  337. }
  338. /* Get the widget for this option type. */
  339. switch ($this->type) {
  340. case SMOPT_TYPE_PASSWORD:
  341. $result = $this->createWidget_String(TRUE);
  342. break;
  343. case SMOPT_TYPE_STRING:
  344. $result = $this->createWidget_String();
  345. break;
  346. case SMOPT_TYPE_STRLIST:
  347. $result = $this->createWidget_StrList();
  348. break;
  349. case SMOPT_TYPE_TEXTAREA:
  350. $result = $this->createWidget_TextArea();
  351. break;
  352. case SMOPT_TYPE_INTEGER:
  353. $result = $this->createWidget_Integer();
  354. break;
  355. case SMOPT_TYPE_FLOAT:
  356. $result = $this->createWidget_Float();
  357. break;
  358. case SMOPT_TYPE_BOOLEAN:
  359. $result = $this->createWidget_Boolean();
  360. break;
  361. case SMOPT_TYPE_BOOLEAN_CHECKBOX:
  362. $result = $this->createWidget_Boolean(TRUE);
  363. break;
  364. case SMOPT_TYPE_BOOLEAN_RADIO:
  365. $result = $this->createWidget_Boolean(FALSE);
  366. break;
  367. case SMOPT_TYPE_HIDDEN:
  368. $result = $this->createWidget_Hidden();
  369. break;
  370. case SMOPT_TYPE_COMMENT:
  371. $result = $this->createWidget_Comment();
  372. break;
  373. case SMOPT_TYPE_FLDRLIST:
  374. $result = $this->createWidget_FolderList();
  375. break;
  376. case SMOPT_TYPE_FLDRLIST_MULTI:
  377. $result = $this->createWidget_FolderList(TRUE);
  378. break;
  379. case SMOPT_TYPE_EDIT_LIST:
  380. $result = $this->createWidget_EditList();
  381. break;
  382. case SMOPT_TYPE_STRLIST_MULTI:
  383. $result = $this->createWidget_StrList(TRUE);
  384. break;
  385. case SMOPT_TYPE_STRLIST_RADIO:
  386. $result = $this->createWidget_StrList(FALSE, TRUE);
  387. break;
  388. case SMOPT_TYPE_SUBMIT:
  389. $result = $this->createWidget_Submit();
  390. break;
  391. case SMOPT_TYPE_INFO:
  392. $result = $this->createWidget_Info();
  393. break;
  394. default:
  395. error_box (
  396. sprintf(_("Option Type '%s' Not Found"), $this->type)
  397. );
  398. }
  399. /* Add the "post script" for this option. */
  400. $result .= $this->post_script;
  401. // put correct value back if need be
  402. if (!is_null($this->new_value)) {
  403. $this->value = $tempValue;
  404. }
  405. /* Now, return the created widget. */
  406. return $result;
  407. }
  408. /**
  409. * Creates info block
  410. * @return string html formated output
  411. */
  412. function createWidget_Info() {
  413. return sq_htmlspecialchars($this->value);
  414. }
  415. /**
  416. * Create string field
  417. *
  418. * @param boolean $password When TRUE, the text in the input
  419. * widget will be obscured (OPTIONAL;
  420. * default = FALSE).
  421. *
  422. * @return string html formated option field
  423. *
  424. */
  425. function createWidget_String($password=FALSE) {
  426. switch ($this->size) {
  427. case SMOPT_SIZE_TINY:
  428. $width = 5;
  429. break;
  430. case SMOPT_SIZE_SMALL:
  431. $width = 12;
  432. break;
  433. case SMOPT_SIZE_LARGE:
  434. $width = 38;
  435. break;
  436. case SMOPT_SIZE_HUGE:
  437. $width = 50;
  438. break;
  439. case SMOPT_SIZE_NORMAL:
  440. default:
  441. $width = 25;
  442. }
  443. //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here
  444. if ($password)
  445. return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . htmlspecialchars($this->trailing_text);
  446. else
  447. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . htmlspecialchars($this->trailing_text);
  448. }
  449. /**
  450. * Create selection box or radio button group
  451. *
  452. * When $this->htmlencoded is TRUE, the keys and values in
  453. * $this->possible_values are assumed to be display-safe.
  454. * Use with care!
  455. *
  456. * Note that when building radio buttons instead of a select
  457. * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
  458. * radio buttons will be output one after another without
  459. * linebreaks between them. Otherwise, each radio button
  460. * goes on a line of its own.
  461. *
  462. * @param boolean $multiple_select When TRUE, the select widget
  463. * will allow multiple selections
  464. * (OPTIONAL; default is FALSE
  465. * (single select list))
  466. * @param boolean $radio_buttons When TRUE, the widget will
  467. * instead be built as a group
  468. * of radio buttons (and
  469. * $multiple_select will be
  470. * forced to FALSE) (OPTIONAL;
  471. * default is FALSE (select widget))
  472. *
  473. * @return string html formated selection box or radio buttons
  474. *
  475. */
  476. function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
  477. //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
  478. // radio buttons instead of select widget?
  479. //
  480. if ($radio_buttons) {
  481. global $br, $nbsp;
  482. $result = '';
  483. foreach ($this->possible_values as $real_value => $disp_value) {
  484. $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);
  485. if ($this->size != SMOPT_SIZE_TINY)
  486. $result .= $br;
  487. }
  488. return $result;
  489. }
  490. // everything below applies to select widgets
  491. //
  492. switch ($this->size) {
  493. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  494. case SMOPT_SIZE_TINY:
  495. $height = 3;
  496. break;
  497. case SMOPT_SIZE_SMALL:
  498. $height = 8;
  499. break;
  500. case SMOPT_SIZE_LARGE:
  501. $height = 15;
  502. break;
  503. case SMOPT_SIZE_HUGE:
  504. $height = 25;
  505. break;
  506. case SMOPT_SIZE_NORMAL:
  507. default:
  508. $height = 5;
  509. }
  510. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text);
  511. }
  512. /**
  513. * Create folder selection box
  514. *
  515. * @param boolean $multiple_select When TRUE, the select widget
  516. * will allow multiple selections
  517. * (OPTIONAL; default is FALSE
  518. * (single select list))
  519. *
  520. * @return string html formated selection box
  521. *
  522. */
  523. function createWidget_FolderList($multiple_select=FALSE) {
  524. switch ($this->size) {
  525. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  526. case SMOPT_SIZE_TINY:
  527. $height = 3;
  528. break;
  529. case SMOPT_SIZE_SMALL:
  530. $height = 8;
  531. break;
  532. case SMOPT_SIZE_LARGE:
  533. $height = 15;
  534. break;
  535. case SMOPT_SIZE_HUGE:
  536. $height = 25;
  537. break;
  538. case SMOPT_SIZE_NORMAL:
  539. default:
  540. $height = 5;
  541. }
  542. // possible values might include a nested array of
  543. // possible values (list of folders)
  544. //
  545. $option_list = array();
  546. foreach ($this->possible_values as $value => $text) {
  547. // list of folders (boxes array)
  548. //
  549. if (is_array($text)) {
  550. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
  551. // just one option here
  552. //
  553. } else {
  554. $option_list = array_merge($option_list, array($value => $text));
  555. }
  556. }
  557. if (empty($option_list))
  558. $option_list = array('ignore' => _("unavailable"));
  559. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text);
  560. }
  561. /**
  562. * Creates textarea
  563. * @return string html formated textarea field
  564. */
  565. function createWidget_TextArea() {
  566. switch ($this->size) {
  567. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  568. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  569. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  570. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  571. case SMOPT_SIZE_NORMAL:
  572. default: $rows = 5; $cols = 50;
  573. }
  574. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  575. }
  576. /**
  577. * Creates field for integer
  578. *
  579. * Difference from createWidget_String is visible only when javascript is enabled
  580. * @return string html formated option field
  581. */
  582. function createWidget_Integer() {
  583. // add onChange javascript handler to a regular string widget
  584. // which will strip out all non-numeric chars
  585. if (checkForJavascript())
  586. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  587. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  588. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  589. . 'this.value=newVal;';
  590. return $this->createWidget_String();
  591. }
  592. /**
  593. * Creates field for floating number
  594. * Difference from createWidget_String is visible only when javascript is enabled
  595. * @return string html formated option field
  596. */
  597. function createWidget_Float() {
  598. // add onChange javascript handler to a regular string widget
  599. // which will strip out all non-numeric (period also OK) chars
  600. if (checkForJavascript())
  601. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  602. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  603. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  604. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  605. return $this->createWidget_String();
  606. }
  607. /**
  608. * Create boolean widget
  609. *
  610. * When creating Yes/No radio buttons, the "yes_text"
  611. * and "no_text" option attributes are used to override
  612. * the typical "Yes" and "No" text.
  613. *
  614. * @param boolean $checkbox When TRUE, the widget will be
  615. * constructed as a checkbox,
  616. * otherwise it will be a set of
  617. * Yes/No radio buttons (OPTIONAL;
  618. * default is TRUE (checkbox)).
  619. *
  620. * @return string html formated boolean widget
  621. *
  622. */
  623. function createWidget_Boolean($checkbox=TRUE) {
  624. global $oTemplate, $nbsp;
  625. // checkbox...
  626. //
  627. if ($checkbox) {
  628. $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);
  629. }
  630. // radio buttons...
  631. //
  632. else {
  633. /* Build the yes choice. */
  634. $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');
  635. /* Build the no choice. */
  636. $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');
  637. /* Build the combined "boolean widget". */
  638. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  639. }
  640. return ($result);
  641. }
  642. /**
  643. * Creates hidden field
  644. * @return string html formated hidden input field
  645. */
  646. function createWidget_Hidden() {
  647. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  648. }
  649. /**
  650. * Creates comment
  651. * @return string comment
  652. */
  653. function createWidget_Comment() {
  654. $result = $this->comment;
  655. return ($result);
  656. }
  657. /**
  658. * Creates an edit list
  659. *
  660. * Note that multiple layout types are supported for this widget.
  661. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  662. * constants.
  663. *
  664. * @return string html formated list of edit fields and
  665. * their associated controls
  666. */
  667. function createWidget_EditList() {
  668. global $oTemplate;
  669. switch ($this->size) {
  670. case SMOPT_SIZE_TINY:
  671. $height = 3;
  672. break;
  673. case SMOPT_SIZE_SMALL:
  674. $height = 8;
  675. break;
  676. case SMOPT_SIZE_MEDIUM:
  677. $height = 15;
  678. break;
  679. case SMOPT_SIZE_LARGE:
  680. $height = 25;
  681. break;
  682. case SMOPT_SIZE_HUGE:
  683. $height = 40;
  684. break;
  685. case SMOPT_SIZE_NORMAL:
  686. default:
  687. $height = 5;
  688. }
  689. if (empty($this->possible_values)) $this->possible_values = array();
  690. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  691. //FIXME: $this->aExtraAttribs probably should only be used in one place
  692. $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
  693. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  694. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  695. $oTemplate->assign('trailing_text', $this->trailing_text);
  696. $oTemplate->assign('possible_values', $this->possible_values);
  697. $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));
  698. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  699. $oTemplate->assign('name', $this->name);
  700. switch ($this->layout_type) {
  701. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  702. return $oTemplate->fetch('edit_list_widget.tpl');
  703. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  704. return $oTemplate->fetch('edit_list_widget_list_style.tpl');
  705. default:
  706. error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $layout_type));
  707. }
  708. }
  709. /**
  710. * Creates a submit button
  711. *
  712. * @return string html formated submit button widget
  713. *
  714. */
  715. function createWidget_Submit() {
  716. return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
  717. }
  718. /**
  719. *
  720. */
  721. function save() {
  722. $function = $this->save_function;
  723. $function($this);
  724. }
  725. /**
  726. *
  727. */
  728. function changed() {
  729. // edit lists have a lot going on, so we'll always process them
  730. //
  731. if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE;
  732. return ($this->value != $this->new_value);
  733. }
  734. } /* End of SquirrelOption class*/
  735. /**
  736. * Saves the option value (this is the default save function
  737. * unless overridden by the user)
  738. *
  739. * @param object $option object that holds option name and new_value
  740. */
  741. function save_option($option) {
  742. // Can't save the pref if we don't have the username
  743. //
  744. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  745. return;
  746. }
  747. global $data_dir;
  748. // edit lists: first add new elements to list, then
  749. // remove any selected ones (note that we must add
  750. // before deleting because the javascript that populates
  751. // the "add" textbox when selecting items in the list
  752. // (for deletion))
  753. //
  754. if ($option->type == SMOPT_TYPE_EDIT_LIST) {
  755. if (empty($option->possible_values)) $option->possible_values = array();
  756. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  757. // add element if given
  758. //
  759. if ((isset($option->use_add_widget) && $option->use_add_widget)
  760. && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
  761. $new_element = trim($new_element);
  762. if (!empty($new_element)
  763. && !in_array($new_element, $option->possible_values))
  764. $option->possible_values[] = $new_element;
  765. }
  766. // delete selected elements if needed
  767. //
  768. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  769. && is_array($option->new_value)
  770. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
  771. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  772. // save full list (stored in "possible_values")
  773. //
  774. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  775. // Certain option types need to be serialized because
  776. // they are not scalar
  777. //
  778. } else if ($option->is_multiple_valued())
  779. setPref($data_dir, $username, $option->name, serialize($option->new_value));
  780. // Checkboxes, when unchecked, don't submit anything in
  781. // the POST, so set to SMPREF_OFF if not found
  782. //
  783. else if (($option->type == SMOPT_TYPE_BOOLEAN
  784. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  785. && empty($option->new_value))
  786. setPref($data_dir, $username, $option->name, SMPREF_OFF);
  787. else
  788. setPref($data_dir, $username, $option->name, $option->new_value);
  789. // if a checkbox or multi select is zeroed/cleared out, it
  790. // needs to have an empty value pushed into its "new_value" slot
  791. //
  792. if (($option->type == SMOPT_TYPE_STRLIST_MULTI
  793. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  794. && is_null($option->new_value))
  795. $option->new_value = '';
  796. }
  797. /**
  798. * save function that does not save
  799. * @param object $option
  800. */
  801. function save_option_noop($option) {
  802. /* Do nothing here... */
  803. }
  804. /**
  805. * Create hidden 'optpage' input field with value set by argument
  806. * @param string $optpage identification of option page
  807. * @return string html formated hidden input field
  808. */
  809. function create_optpage_element($optpage) {
  810. return addHidden('optpage', $optpage);
  811. }
  812. /**
  813. * Create hidden 'optmode' input field with value set by argument
  814. * @param string $optmode
  815. * @return string html formated hidden input field
  816. */
  817. function create_optmode_element($optmode) {
  818. return addHidden('optmode', $optmode);
  819. }
  820. /**
  821. * @param array $optgrps
  822. * @param array $optvals
  823. * @return array
  824. */
  825. function create_option_groups($optgrps, $optvals) {
  826. /* Build a simple array with which to start. */
  827. $result = array();
  828. /* Create option group for each option group name. */
  829. foreach ($optgrps as $grpkey => $grpname) {
  830. $result[$grpkey] = array();
  831. $result[$grpkey]['name'] = $grpname;
  832. $result[$grpkey]['options'] = array();
  833. }
  834. /* Create a new SquirrelOption for each set of option values. */
  835. foreach ($optvals as $grpkey => $grpopts) {
  836. foreach ($grpopts as $optset) {
  837. /* Create a new option with all values given. */
  838. $next_option = new SquirrelOption(
  839. $optset,
  840. $optset['name'],
  841. $optset['caption'],
  842. $optset['type'],
  843. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  844. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  845. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  846. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  847. );
  848. /* If provided, set if the caption is allowed to wrap for this option. */
  849. if (isset($optset['caption_wrap'])) {
  850. $next_option->setCaptionWrap($optset['caption_wrap']);
  851. }
  852. /* If provided, set the size for this option. */
  853. if (isset($optset['size'])) {
  854. $next_option->setSize($optset['size']);
  855. }
  856. /* If provided, set the trailing_text for this option. */
  857. if (isset($optset['trailing_text'])) {
  858. $next_option->setTrailingText($optset['trailing_text']);
  859. }
  860. /* If provided, set the yes_text for this option. */
  861. if (isset($optset['yes_text'])) {
  862. $next_option->setYesText($optset['yes_text']);
  863. }
  864. /* If provided, set the no_text for this option. */
  865. if (isset($optset['no_text'])) {
  866. $next_option->setNoText($optset['no_text']);
  867. }
  868. /* If provided, set the layout type for this option. */
  869. if (isset($optset['layout_type'])) {
  870. $next_option->setLayoutType($optset['layout_type']);
  871. }
  872. /* If provided, set the use_add_widget value for this option. */
  873. if (isset($optset['use_add_widget'])) {
  874. $next_option->setUseAddWidget($optset['use_add_widget']);
  875. }
  876. /* If provided, set the use_delete_widget value for this option. */
  877. if (isset($optset['use_delete_widget'])) {
  878. $next_option->setUseDeleteWidget($optset['use_delete_widget']);
  879. }
  880. /* If provided, set the comment for this option. */
  881. if (isset($optset['comment'])) {
  882. $next_option->setComment($optset['comment']);
  883. }
  884. /* If provided, set the save function for this option. */
  885. if (isset($optset['save'])) {
  886. $next_option->setSaveFunction($optset['save']);
  887. }
  888. /* If provided, set the extra attributes for this option. */
  889. if (isset($optset['extra_attributes'])) {
  890. $next_option->setExtraAttributes($optset['extra_attributes']);
  891. }
  892. /* If provided, set the "post script" for this option. */
  893. if (isset($optset['post_script'])) {
  894. $next_option->setPostScript($optset['post_script']);
  895. }
  896. /* If provided, set the folder_filter for this option. */
  897. if (isset($optset['folder_filter'])) {
  898. $next_option->setFolderFilter($optset['folder_filter']);
  899. }
  900. /* Add this option to the option array. */
  901. $result[$grpkey]['options'][] = $next_option;
  902. }
  903. }
  904. /* Return our resulting array. */
  905. return ($result);
  906. }