options.php 32 KB

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