1: <?php
2:
3: /**
4: * Description of Select
5: *
6: * @author SARTAJ
7: */
8:
9: namespace Sphp\Comp\Form{
10:
11: class Select extends \Sphp\tools\Component {
12:
13: public $options = '';
14: private $separator = " ";
15: private $blnopttxtreplace = false;
16: private $opttxtreplace = array();
17: public $opt = Array();
18: public $blnuseasoc = false;
19: public $selectedIndex = 0;
20: private $formName = '';
21: private $errmsg = '';
22: private $msgName = '';
23: private $notvalue = '';
24: // use for check static option tag == value or not found
25: private $fstatic = false;
26: private $strFirstValue = "null";
27: private $cssfield = "";
28: private $cssvals = array();
29: public $blnsearch = false;
30:
31: protected function genhelpPropList() {
32: parent::genhelpPropList();
33: $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$val');
34: $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
35: $this->addHelpPropFunList('setFirstValue','Set First Option to Display','','$val');
36: $this->addHelpPropFunList('setOptionsKeyArray','Options as Associative Array, Option Text and value may be different','','');
37: $this->addHelpPropFunList('unsetOptionsKeyArray','Options generate with same text and value','','');
38: $this->addHelpPropFunList('setSelectedIndex','Set option selected','','');
39: $this->addHelpPropFunList('setNotValue','Value that is not valid to submit','','$val');
40: $this->addHelpPropFunList('setOptions','Set Options list as comma separated string value=text','','$val');
41: $this->addHelpPropFunList('setOptionsArray','Set options as array','','$val');
42: $this->addHelpPropFunList('setOptionsJSON','Set options as array via json string','','$json');
43: $this->addHelpPropFunList('setOptionsFromTable','Set options from database','','$valueField,$textField:d"",$tableName:d"",$logic:d"", $sql:d"", $cacheTime:d0');
44: $this->addHelpPropFunList('setOptionsElement','Over write option element','','$index, $value:d"", $text:d""');
45: $this->addHelpPropFunList('removeOptionsElement','Remove option element','','$index');
46: }
47:
48: protected function oninit() {
49: $this->tagName = "select";
50: if(!$this->element->hasAttribute("name")){
51: $this->HTMLName = $this->name;
52: }else{
53: $this->HTMLName = $this->getAttribute("name");
54: }
55:
56: if($this->getAttribute("msgname") != ""){
57: $this->msgName = $this->getAttribute("msgname");
58: }
59: }
60: public function setErrMsg($msg){
61: $this->errmsg .= '<strong class="alert-danger">' . $msg . '</strong>';
62: if(\SphpBase::sphp_request()->isAJAX()){
63: \SphpBase::JSServer()->addJSONJSBlock('$("#'. $this->name .'").after("<strong class=\"alert-danger\">' . $msg . '! </strong>");');
64: }
65: setErr($this->name, $msg);
66: }
67: /**
68: * Find and replace string in option text
69: * @param type $find find string
70: * @param type $rep replace string
71: */
72: public function fu_replace($find,$rep){
73: $this->blnopttxtreplace = true;
74: $this->opttxtreplace = array($find,$rep);
75: }
76: /**
77: * Separator for Option Text from multi fields of db with this Character
78: * @param string $sepa default = " "
79: */
80: public function fu_setSeparator($sepa){
81: $this->separator = $sepa;
82: }
83: /**
84: * DB Table Field use for set as class in option tag
85: * @param string $tblfield DB Table field
86: */
87: public function fu_setCssField($tblfield){
88: $this->cssfield = $tblfield;
89: }
90: /**
91: * Make Select as Type-able and Searchable
92: */
93: public function fu_enableSearch(){
94: $this->blnsearch = true;
95: }
96: public function fi_setForm($val) {
97: $this->formName = $val;
98: }
99:
100: public function fu_setMsgName($val) {
101: $this->msgName = $val;
102: $this->setAttribute('placeholder', $val);
103: }
104:
105: public function fu_setFirstValue($val) {
106: $this->strFirstValue = $val;
107: }
108:
109: /**
110: * Use Options as diffrent in show in html and in value html filed<br>
111: * options[key] = val <br>
112: * option value= key <br>
113: * option text = val <br>
114: * By default = false
115: */
116: public function setOptionsKeyArray() {
117: $this->blnuseasoc = true;
118: }
119:
120: public function getOptionsKeyArray() {
121: return $this->blnuseasoc;
122: }
123:
124: public function unsetOptionsKeyArray() {
125: $this->blnuseasoc = false;
126: }
127:
128: public function fu_setSelectedIndex($val) {
129: $this->selectedIndex = $val;
130: }
131:
132: public function getSelectedIndex() {
133: return $this->selectedIndex;
134: }
135:
136: public function getSelectedValue() {
137: if(trim($this->value)!=""){
138: return $this->value;
139: }else if(count($this->opt)>0){
140: if($this->opt[0][0]!=""){
141: return $this->opt[0][0];
142: }else{
143: return $this->opt[0][1];
144: }
145: }
146:
147: }
148:
149: public function fi_setNotValue($val) {
150: $this->notvalue = $val;
151: if ($this->issubmit) {
152: if ($this->notvalue == $this->getValue()) {
153: $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Please Select a Value");
154: }
155: }
156: }
157:
158: /**
159: * Pass parameter like $val = "vcd,dvd,mp3";<br>
160: * @param String $val
161: */
162: public function fu_setOptions($val) {
163: $this->unsetOptionsKeyArray();
164: $this->options = $val;
165: $this->opt = array();
166: $this->genOptionArray();
167: }
168:
169: public function getOptions() {
170: return $this->options;
171: }
172:
173: /**
174: * Pass parameter like $val[] = Array('VCD','Code 1');<br>
175: * $val[] = Array('DVD','Code 2');<br>
176: * DVD = value of option tag and 'Code 2' = text<br>
177: * @param Array $val
178: */
179: public function setOptionsArray($val) {
180: $this->opt = $val;
181: $this->options = '';
182: $this->genOptionList();
183: }
184: /**
185: * Set JSON String as key value pairs.
186: * funsetOptionsJSON='[["0","Parent"], ["1", "Child"]]'
187: * funsetOptionsJSON='{"0":"Parent", "1": "Child"}'
188: * @param string $val json string
189: */
190: public function fu_setOptionsJSON($val) {
191: //$val = trim($val);
192: $opt2 = json_decode($val,true);
193: $opt1 = array();
194: if($val[0] == '{'){
195: foreach($opt2 as $key=>$val){
196: $opt1[] = [$key,$val];
197: }
198: $this->opt = $opt1;
199: }else{
200: $this->opt = $opt2;
201: }
202: $this->options = '';
203: $this->setOptionsKeyArray();
204: $this->genOptionList();
205: }
206:
207: public function getOptionsArray() {
208: return $this->opt;
209: }
210:
211: private function genOptionArray() {
212: $str = explode(',', $this->options);
213: foreach ($str as $index => $val) {
214: $this->opt[] = Array('', $val);
215: }
216: }
217:
218: private function genOptionList() {
219: $str = $this->opt;
220: foreach ($str as $index => $val) {
221: if ($this->options == '') {
222: $this->options = $val[1];
223: } else {
224: $this->options .= "," . $val[1];
225: }
226: }
227: }
228:
229: public function setOptionsElement($index, $value = '', $text = '') {
230: $this->opt[$index] = Array($value, $text);
231: $this->options = '';
232: $this->genOptionList();
233: }
234:
235: public function getOptionsElement($index) {
236: return $this->opt[$index];
237: }
238:
239: public function removeOptionsElement($index) {
240: unset($this->opt[$index]);
241: $this->options = '';
242: $this->genOptionList();
243: }
244:
245: public function fu_setOptionsFromTable($valueField, $textField = '', $tableName = '', $logic = '', $sql = '', $cacheTime = '0') {
246: $tblName = \SphpBase::page()->tblName;
247: $mysql = \SphpBase::dbEngine();
248: $blnMultiTextField = false;
249: $blnMultiValField = false;
250: $arr1 = Array();
251: $blncsfield = false;
252: $csfield = "";
253:
254: if($this->cssfield != ""){
255: $csfield = ',' . $this->cssfield;
256: $blncsfield = true;
257: }
258: $ar1 = explode(",", $textField);
259: if (count($ar1) > 1) {
260: $blnMultiTextField = true;
261: }
262: $ar2 = explode(",", $valueField);
263: if (count($ar2) > 1) {
264: $blnMultiValField = true;
265: }
266:
267: if ($tableName == '') {
268: $tableName = $tblName;
269: }
270: if($sql==''){
271: if ($textField == '') {
272: if($blnMultiValField){
273: $textField = $ar2[0];
274: }else{
275: $textField = $valueField;
276: }
277: $sql = "SELECT $valueField $csfield FROM $tableName $logic";
278: } else {
279: $sql = "SELECT $valueField,$textField $csfield FROM $tableName $logic";
280: }
281: }
282:
283: $result = $mysql->fetchQuery($sql, $cacheTime, '', $valueField);
284: foreach ($result as $index2 => $row2) {
285: foreach ($row2 as $index => $row) {
286: $strv1 = "";
287: $strt1 = "";
288: if ($blnMultiTextField) {
289: foreach ($ar1 as $key => $value) {
290: if($strt1 != ""){
291: $strt1 .= $this->separator . $row[$value] ;
292: }else{
293: $strt1 .= $row[$value] ;
294: }
295: }
296: } else {
297: $strt1 = $row[$textField];
298: }
299: if ($blnMultiValField) {
300: foreach ($ar2 as $key => $value) {
301: $strv1 .= $row[$value] . ',';
302: }
303: } else {
304: $strv1 = $row[$valueField];
305: }
306: $arr1[] = Array($strv1, $strt1);
307: if($blncsfield) $this->cssvals[] = $row[$this->cssfield]; // use for set class attribute of option tag
308: }
309: }
310: $this->setOptionsKeyArray();
311: $this->setOptionsArray($arr1);
312: }
313:
314: protected function onprejsrender() {
315: if ($this->formName != '' && $this->notvalue != '') {
316: $jscode = "if(blnSubmit==true && " . $this->getJSValue() . "=='" . $this->notvalue . "'){
317: blnSubmit = false ;
318: displayValidationError(document.getElementById('$this->name'),'Please Select One Option From " . $this->msgName . "');
319: document.getElementById('$this->name').focus();
320: }";
321: addHeaderJSFunctionCode("{$this->formName}_submit", "$this->name", $jscode);
322: }
323: }
324:
325: public function processStaticOptions($element) {
326: if ($element->tag == 'option') {
327: if (($element->hasAttribute('value') && strtolower($element->getAttribute('value')) == strtolower($this->value)) || strtolower($element->innertext) == strtolower($this->value)) {
328: $element->attr[' selected'] = 'selected';
329: }
330: $this->fstatic = true;
331: }
332: }
333:
334: protected function onprerender() {
335: if($this->blnsearch){
336: addFileLink($this->myrespath . "/jslib/select2.min.css");
337: addFileLink($this->myrespath . "/jslib/select2.min.js");
338: }
339: }
340:
341: protected function onrender() {
342: if($this->getVisible()){
343: $HTMLParser = new \Sphp\tools\HTMLParser();
344: if($this->blnsearch){
345: addHeaderJSFunctionCode("ready", "select2","$(\"#{$this->name}\").select2({
346: theme: \"classic\",
347: width: '100%',
348: placeholder: \"{$this->placeholder}\",
349: dropdownParent: $('body')
350: });");
351: }
352: if($this->errmsg!=""){
353: $this->setPostTag($this->errmsg);
354: }
355: if ($this->getAttribute('class') == '') {
356: $this->class = "form-control";
357: }
358: if ($this->options != '') {
359: $this->innerHTML = $this->getOptionsHTML();
360: } else {
361: $this->innerHTML = $HTMLParser->parseHTMLTag($this->innerHTML, 'processStaticOptions', $this);
362: if (!$this->fstatic) {
363: $this->innerHTML .= '<option selected="selected">' . $this->value . '</option>';
364: }
365: }
366: switch($this->styler){
367: case 1:{
368: $this->setPreTag('<div class="form-floating mb-3">');
369: $this->setPostTag('<label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label></div>');
370: break;
371: }case 2:{
372: $this->setPreTag('<div class="mb-3">
373: <label for="'. $this->HTMLID .'" class="form-label">'. $this->msgName .'</label>');
374: $this->setPostTag('<div id="'. $this->HTMLID .'Help" class="form-text">'. $this->helptext .'</div></div>');
375: break;
376: }
377: }
378: }else{
379: $this->setAttribute('value', $this->getSelectedValue());
380: }
381: }
382:
383: public function getOptionsHTML() {
384: $vals = $this->value;
385: $strOut = '';
386: $CF = 0;
387: $arr1 = Array();
388:
389: $blncsfield = false;
390: if(count($this->cssvals) > 0){
391: $blncsfield = true;
392: }
393:
394: if ($this->strFirstValue != "null") {
395: $stra = explode(',', $this->strFirstValue);
396: if (count($stra) > 1) {
397: $arr1[] = Array($stra[0], $stra[1]);
398: } else {
399: $arr1[] = Array($stra[0], $stra[0]);
400: }
401: $holder = array_merge($arr1, $this->opt);
402: $ar2 = array('firstopt');
403: if($blncsfield) $this->cssvals = array_merge($ar2,$this->cssvals);
404: } else {
405: $holder = $this->opt;
406: }
407:
408:
409: if ($vals != '') {
410: $holder2 = $vals;
411: foreach ($holder as $key2 => $val) {
412: $cssatr = "";
413: if($blncsfield) $cssatr = 'class="'. $this->cssvals[$key2] .'"';
414:
415: $key = $val[0];
416: if (!$this->blnuseasoc) {
417: $key = $val[1];
418: }
419: // replace text according to replace string
420: if($this->blnopttxtreplace) $val[1] = str_replace($this->opttxtreplace[0],$this->opttxtreplace[1],$val[1]);
421:
422: if ($key == $holder2) {
423: $strOut .= "<option $cssatr value=\"$key\" selected>" . $val[1] . "</option>";
424: } else {
425: $strOut .= "<option $cssatr value=\"$key\">" . $val[1] . "</option>";
426: }
427: }
428: } else {
429: foreach ($holder as $key2 => $val) {
430: $cssatr = "";
431: if($blncsfield) $cssatr = 'class="'. $this->cssvals[$key2] .'"';
432:
433: $key = $val[0];
434: if (!$this->blnuseasoc) {
435: $key = $val[1];
436: }
437: // replace text according to replace string
438: if($this->blnopttxtreplace) $val[1] = str_replace($this->opttxtreplace[0],$this->opttxtreplace[1],$val[1]);
439:
440: if ($CF == $this->selectedIndex) {
441: $this->value = $key;
442: $strOut .= "<option $cssatr value=\"$key\" selected>" . $val[1] . "</option>";
443: } else {
444: $strOut .= "<option $cssatr value=\"$key\">" . $val[1] . "</option>";
445: }
446: $CF += 1;
447: }
448: }
449: return $strOut;
450: }
451:
452: // javascript functions
453: public function getJSValue() {
454: return "document.getElementById('$this->name').options[document.getElementById('$this->name').selectedIndex].value";
455: }
456:
457: public function setJSValue($exp) {
458: return " jql(\"select#$this->name\").val($exp); ";
459: }
460:
461: public function setJSOptionValue($exp) {
462: return "document.getElementById('$this->name').options[document.getElementById('$this->name').selectedIndex] = $exp;";
463: }
464:
465: public function getJSSelectedIndex() {
466: return "document.getElementById('$this->name').selectedIndex";
467: }
468:
469: }
470: }
471: