/fmake/libs/phpObjectForms/lib/elements/extra/FPSplitSelect.class.php

https://github.com/fmake/adv · PHP · 341 lines · 271 code · 46 blank · 24 comment · 29 complexity · 7ce1b3a059f3542885d11757a0e19a7c MD5 · raw file

  1. <?php
  2. /**
  3. * @author Ilya Boyandin <ilyabo@gmail.com>
  4. */
  5. class FPSplitSelect extends FPElement {
  6. var $_size;
  7. //var $_isMultiple = false;
  8. var $_options = array();
  9. var $_leftIDs;
  10. var $_rightIDs;
  11. var $_leftTitle;
  12. var $_rightTitle;
  13. var $_tblPadding = 2;
  14. var $_tblSpacing = 2;
  15. function FPSplitSelect($params)
  16. {
  17. FPElement::FPElement($params);
  18. /*if (isset($params["multiple"]))
  19. $this->_isMultiple = $params["multiple"] ? true : false
  20. ;*/
  21. if (isset($params["size"]))
  22. $this->_size = $params["size"];
  23. /*if (!isset($this->_size))
  24. $this->_size = $this->_isMultiple ? 5 : 1
  25. ;*/
  26. if (isset($params["options"])) $this->_options = $params["options"];
  27. if (isset($params["left_ids"])) {
  28. $this->_leftIDs = $params["left_ids"];
  29. if (!isset($params["right_ids"])) {
  30. $this->_rightIDs = array();
  31. foreach ($this->_options as $key => $val) {
  32. if (!in_array($key, $this->_leftIDs))
  33. $this->_rightIDs[] = $key;
  34. }
  35. }
  36. }
  37. if (isset($params["right_ids"])) {
  38. $this->_rightIDs = $params["right_ids"];
  39. if (!isset($params["left_ids"])) {
  40. $this->_leftIDs = array();
  41. foreach ($this->_options as $key => $val) {
  42. if (!in_array($key, $this->_rightIDs))
  43. $this->_leftIDs[] = $key;
  44. }
  45. }
  46. }
  47. if (isset($params["left_title"]))
  48. $this->_leftTitle = $params["left_title"];
  49. if (isset($params["right_title"]))
  50. $this->_rightTitle = $params["right_title"];
  51. if (isset($params["min_right_options_selection"]))
  52. $this->_minRightOptsSelection = $params["min_right_options_selection"]
  53. ;
  54. if (isset($params["max_right_options_selection"]))
  55. $this->_maxRightOptsSelection = $params["max_right_options_selection"]
  56. ;
  57. if (isset($params["exact_right_options_selection"]))
  58. $this->_exactRightOptsSelection = $params["exact_right_options_selection"]
  59. ;
  60. /*
  61. $this->_required =
  62. ($this->_minRightOptsSelection > 0 ||
  63. $this->_exactRightOptsSelection > 0)
  64. ;*/
  65. // $this->_title = $this->_rightTitle;
  66. // $this->_required = true;
  67. if (isset($params["table_padding"])) $this->_tblPadding = $params["table_padding"];
  68. if (isset($params["table_spacing"])) $this->_tblSpacing = $params["table_spacing"];
  69. }
  70. function _packValues($vals) {
  71. return implode("||", $vals);
  72. }
  73. function _unpackValues($packedVals) {
  74. return ($packedVals != "" ? explode("||", $packedVals) : array());
  75. }
  76. function setValue($semicolonSeparatedRightIDs)
  77. {
  78. $this->_rightIDs = $this->_unpackValues($semicolonSeparatedRightIDs);
  79. $this->_leftIDs = array();
  80. foreach ($this->_options as $key => $val) {
  81. if (!in_array($key, $this->_rightIDs))
  82. $this->_leftIDs[] = $key;
  83. }
  84. }
  85. function getValue()
  86. {
  87. return $this->_rightIDs; //$this->_packValues($this->_rightIDs);
  88. }
  89. function validate()
  90. {
  91. $cnt = 0;
  92. for ($i=0; $i<count($this->_rightIDs); $i++)
  93. {
  94. if (!isset($this->_options[$this->_rightIDs[$i]]))
  95. {
  96. $this->_errCode = FP_ERR_CODE__INVALID_USER_DATA;
  97. return false;
  98. }
  99. $cnt++;
  100. }
  101. if (isset($this->_minRightOptsSelection) && $cnt < $this->_minRightOptsSelection)
  102. {
  103. $this->_errCode = FP_ERR_CODE__TOO_FEW_OPTS_SELECTED;
  104. return false;
  105. }
  106. if (isset($this->_maxRightOptsSelection) && $cnt > $this->_maxRightOptsSelection)
  107. {
  108. $this->_errCode = FP_ERR_CODE__TOO_MANY_OPTS_SELECTED;
  109. return false;
  110. }
  111. if (isset($this->_exactRightOptsSelection) && $cnt != $this->_exactRightOptsSelection)
  112. {
  113. $this->_errCode =
  114. $cnt < $this->_exactOptsSelection ?
  115. FP_ERR_CODE__TOO_FEW_OPTS_SELECTED :
  116. FP_ERR_CODE__TOO_MANY_OPTS_SELECTED
  117. ;
  118. return false;
  119. }
  120. return true;
  121. }
  122. function echoSource()
  123. {
  124. $cssPrefix = $this->_owner->getCssClassPrefix();
  125. $this->_append(
  126. '<input type="hidden" name="'.$this->_name.'"'.
  127. ' value="'.$this->_packValues($this->_rightIDs).'">'."\n".
  128. '<table'.
  129. ' cellpadding="'.$this->_tblPadding.'"'.
  130. ' cellspacing="'.$this->_tblSpacing.'"'.
  131. ' border="0"'.
  132. '>'."\n".
  133. (isset($this->_title) ?
  134. '<tr><td colspan="3" align="center">'.
  135. '<span class="'.$cssPrefix.'ReqTitle">'.
  136. $this->_title.
  137. '</span>'.
  138. '</td></tr>'."\n"
  139. : ""
  140. ).
  141. '<tr>'.
  142. '<td>'.
  143. '<span class="'.$cssPrefix.'ReqTitle">'.
  144. $this->_leftTitle.
  145. '</span>'.
  146. '</td>'."\n".
  147. '<td>&nbsp;</td>'.
  148. '<td>'.
  149. '<span class="'.$cssPrefix.'ReqTitle">'.
  150. $this->_rightTitle.
  151. '</span>'.
  152. '</td>'.
  153. '</tr>'."\n".
  154. '<tr>'
  155. );
  156. // left select box
  157. $this->_append(
  158. '<td>'."\n".
  159. '<select'.
  160. ' name="'.$this->_name.'_leftBox"'.
  161. ' size="'.$this->_size.'"'.
  162. //($this->_isMultiple ? ' multiple' : '').
  163. (isset($this->_cssStyle) ?
  164. ' style="'.$this->_cssStyle.'"' : ''
  165. ).
  166. ' onchange="'.$this->_name.'_splitSelectOnChangeLeft()"'.
  167. '>'."\n"
  168. );
  169. foreach ($this->_leftIDs as $key)
  170. $this->_append(
  171. '<option'.
  172. ' value="'.$key.'"'.
  173. '>'.
  174. $this->_options[$key].
  175. '</option>'."\n"
  176. );
  177. $this->_append(
  178. '</select>'."\n".'</td>'."\n"
  179. );
  180. // buttons
  181. $this->_append(
  182. '<td>'.
  183. '<input type="button" value=" -&gt; "'.
  184. ' onclick="'.$this->_name.'_splitSelectLeftToRight()"'.
  185. ' class="'.$cssPrefix.'Button">'.
  186. '<br>'.
  187. '<input type="button" value=" &lt;- "'.
  188. ' onclick="'.$this->_name.'_splitSelectRightToLeft()"'.
  189. ' class="'.$cssPrefix.'Button">'.
  190. '</td>'
  191. );
  192. // right select box
  193. $this->_append(
  194. '<td>'."\n".
  195. '<select'.
  196. ' name="'.$this->_name.'_rightBox"'.
  197. ' size="'.$this->_size.'"'.
  198. //($this->_isMultiple ? ' multiple' : '').
  199. (isset($this->_cssStyle) ?
  200. ' style="'.$this->_cssStyle.'"' : ''
  201. ).
  202. ' onchange="'.$this->_name.'_splitSelectOnChangeRight()"'.
  203. '>'."\n"
  204. );
  205. foreach ($this->_rightIDs as $key)
  206. $this->_append(
  207. '<option'.
  208. ' value="'.$key.'"'.
  209. '>'.
  210. $this->_options[$key].
  211. '</option>'."\n"
  212. );
  213. $this->_append(
  214. '</select>'."\n".'</td>'."\n".
  215. '</tr>'.
  216. ($this->getErrorMsg() ?
  217. '<tr>'."\n".
  218. '<td colspan="3"'.
  219. (isset($this->_tblFieldCellWidth) ?
  220. ' width="'.$this->_tblFieldCellWidth.'"' : ''
  221. ).
  222. '>'."\n".
  223. $this->getErrorSource()."\n".
  224. '</td>'."\n".
  225. '</tr>'."\n"
  226. :
  227. ''
  228. )."\n".
  229. ($this->_comment ?
  230. '<tr>'."\n".
  231. '<td>&nbsp;</td>'.
  232. '<td'.
  233. (isset($this->_tblFieldCellWidth) ?
  234. ' width="'.$this->_tblFieldCellWidth.'"' : ''
  235. ).
  236. '>'."\n".
  237. $this->getCommentSource()."\n".
  238. '</td>'."\n".
  239. '</tr>'."\n"
  240. :
  241. ''
  242. ).
  243. '</table>'
  244. );
  245. // getting the parent form name
  246. $parentForm = $this->getParentFormObj();
  247. $formName = $parentForm->getName();
  248. $nam = $this->_name;
  249. $this->_append(
  250. <<<EOC
  251. <script language="JavaScript">
  252. <!--
  253. var ${nam}_fp = document.forms['${formName}'];
  254. var ${nam}_leftOpts = ${nam}_fp.elements['${nam}_leftBox'].options;
  255. var ${nam}_rightOpts = ${nam}_fp.elements['${nam}_rightBox'].options;
  256. var ${nam}_valueElt = ${nam}_fp.elements['${nam}'];
  257. function ${nam}_updateValueElt() {
  258. var packedRightIDs = '';
  259. for (var i=0; i<${nam}_rightOpts.length; i++) {
  260. packedRightIDs += ${nam}_rightOpts[i].value +
  261. (i < ${nam}_rightOpts.length - 1 ? "||" : "");
  262. }
  263. ${nam}_valueElt.value = packedRightIDs;
  264. }
  265. function ${nam}_splitSelectRightToLeft() {
  266. ${nam}_splitSelectAToB(${nam}_rightOpts, ${nam}_leftOpts);
  267. ${nam}_updateValueElt();
  268. }
  269. function ${nam}_splitSelectLeftToRight() {
  270. ${nam}_splitSelectAToB(${nam}_leftOpts, ${nam}_rightOpts);
  271. ${nam}_updateValueElt();
  272. }
  273. function ${nam}_splitSelectAToB(a, b) {
  274. for (var i=0; i<a.length; i++) {
  275. if (a[i].selected) {
  276. b[b.length] = new Option(
  277. a[i].text, a[i].value, false, true
  278. );
  279. a[i] = null;
  280. }
  281. }
  282. }
  283. function ${nam}_splitSelectOnChangeLeft() {
  284. ${nam}_rightOpts.selectedIndex = -1;
  285. }
  286. function ${nam}_splitSelectOnChangeRight() {
  287. ${nam}_leftOpts.selectedIndex = -1;
  288. }
  289. // -->
  290. </script>
  291. EOC
  292. );
  293. }
  294. }
  295. ?>