| 1: | <?php
|
| 2: |
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: |
|
| 9: | namespace Sphp\comp\html{
|
| 10: |
|
| 11: | class Select extends \Sphp\tools\Control {
|
| 12: |
|
| 13: | public $options = '';
|
| 14: | public $opt = Array();
|
| 15: | public $blnuseasoc = false;
|
| 16: | public $selectedIndex = 0;
|
| 17: | private $formName = '';
|
| 18: | private $errmsg = '';
|
| 19: | private $msgName = '';
|
| 20: | private $notvalue = '';
|
| 21: |
|
| 22: | private $fstatic = false;
|
| 23: | private $strFirstValue = "null";
|
| 24: |
|
| 25: | protected function genhelpPropList() {
|
| 26: | parent::genhelpPropList();
|
| 27: | $this->addHelpPropFunList('setForm','Bind with Form JS Event','','$val');
|
| 28: | $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
|
| 29: | $this->addHelpPropFunList('setFirstValue','Set First Option to Display','','$val');
|
| 30: | $this->addHelpPropFunList('setOptionsKeyArray','Options as Associative Array, Option Text and value may be different','','');
|
| 31: | $this->addHelpPropFunList('unsetOptionsKeyArray','Options generate with same text and value','','');
|
| 32: | $this->addHelpPropFunList('setSelectedIndex','Set option selected','','');
|
| 33: | $this->addHelpPropFunList('setNotValue','Value that is not valid to submit','','$val');
|
| 34: | $this->addHelpPropFunList('setOptions','Set Options list as comma separated string value=text','','$val');
|
| 35: | $this->addHelpPropFunList('setOptionsArray','Set options as array','','$val');
|
| 36: | $this->addHelpPropFunList('setOptionsJSON','Set options as array via json string','','$json');
|
| 37: | $this->addHelpPropFunList('setOptionsFromTable','Set options from database','','$valueField,$textField:d"",$tableName:d"",$logic:d"", $sql:d"", $cacheTime:d0');
|
| 38: | $this->addHelpPropFunList('setOptionsElement','Over write option element','','$index, $value:d"", $text:d""');
|
| 39: | $this->addHelpPropFunList('removeOptionsElement','Remove option element','','$index');
|
| 40: | }
|
| 41: |
|
| 42: | public function oninit() {
|
| 43: | $this->tagName = "select";
|
| 44: | if($this->getAttribute("msgname") != ""){
|
| 45: | $this->msgName = $this->getAttribute("msgname");
|
| 46: | }
|
| 47: | }
|
| 48: | public function setErrMsg($msg){
|
| 49: | $this->errmsg .= '<strong class="alert-danger">' . $msg . '</strong>';
|
| 50: | setErr($this->name, $msg);
|
| 51: | }
|
| 52: |
|
| 53: | public function setForm($val) {
|
| 54: | $this->formName = $val;
|
| 55: | }
|
| 56: |
|
| 57: | public function setMsgName($val) {
|
| 58: | $this->msgName = $val;
|
| 59: | $this->setAttribute('placeholder', $val);
|
| 60: | }
|
| 61: |
|
| 62: | public function setFirstValue($val) {
|
| 63: | $this->strFirstValue = $val;
|
| 64: | }
|
| 65: |
|
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: |
|
| 73: | public function setOptionsKeyArray() {
|
| 74: | $this->blnuseasoc = true;
|
| 75: | }
|
| 76: |
|
| 77: | public function getOptionsKeyArray() {
|
| 78: | return $this->blnuseasoc;
|
| 79: | }
|
| 80: |
|
| 81: | public function unsetOptionsKeyArray() {
|
| 82: | $this->blnuseasoc = false;
|
| 83: | }
|
| 84: |
|
| 85: | public function setSelectedIndex($val) {
|
| 86: | $this->selectedIndex = $val;
|
| 87: | }
|
| 88: |
|
| 89: | public function getSelectedIndex() {
|
| 90: | return $this->selectedIndex;
|
| 91: | }
|
| 92: |
|
| 93: | public function getSelectedValue() {
|
| 94: | if(trim($this->value)!=""){
|
| 95: | return $this->value;
|
| 96: | }else if(count($this->opt)>0){
|
| 97: | if($this->opt[0][0]!=""){
|
| 98: | return $this->opt[0][0];
|
| 99: | }else{
|
| 100: | return $this->opt[0][1];
|
| 101: | }
|
| 102: | }
|
| 103: |
|
| 104: | }
|
| 105: |
|
| 106: | public function setNotValue($val) {
|
| 107: | $this->notvalue = $val;
|
| 108: | if ($this->issubmit) {
|
| 109: | if ($this->notvalue == $this->getValue()) {
|
| 110: | $this->setErrMsg( $this->getAttribute("msgname") .' ' . "Please Select a Value");
|
| 111: | }
|
| 112: | }
|
| 113: | }
|
| 114: |
|
| 115: | |
| 116: | |
| 117: | |
| 118: |
|
| 119: | public function setOptions($val) {
|
| 120: | $this->unsetOptionsKeyArray();
|
| 121: | $this->options = $val;
|
| 122: | $this->opt = array();
|
| 123: | $this->genOptionArray();
|
| 124: | }
|
| 125: |
|
| 126: | public function getOptions() {
|
| 127: | return $this->options;
|
| 128: | }
|
| 129: |
|
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: |
|
| 136: | public function setOptionsArray($val) {
|
| 137: | $this->opt = $val;
|
| 138: | $this->options = '';
|
| 139: | $this->genOptionList();
|
| 140: | }
|
| 141: | public function setOptionsJSON($val) {
|
| 142: | $this->opt = json_decode($val,true);
|
| 143: | $this->options = '';
|
| 144: | $this->setOptionsKeyArray();
|
| 145: | $this->genOptionList();
|
| 146: | }
|
| 147: |
|
| 148: | public function getOptionsArray() {
|
| 149: | return $this->opt;
|
| 150: | }
|
| 151: |
|
| 152: | private function genOptionArray() {
|
| 153: | $str = explode(',', $this->options);
|
| 154: | foreach ($str as $index => $val) {
|
| 155: | $this->opt[] = Array('', $val);
|
| 156: | }
|
| 157: | }
|
| 158: |
|
| 159: | private function genOptionList() {
|
| 160: | $str = $this->opt;
|
| 161: | foreach ($str as $index => $val) {
|
| 162: | if ($this->options == '') {
|
| 163: | $this->options = $val[1];
|
| 164: | } else {
|
| 165: | $this->options .= "," . $val[1];
|
| 166: | }
|
| 167: | }
|
| 168: | }
|
| 169: |
|
| 170: | public function setOptionsElement($index, $value = '', $text = '') {
|
| 171: | $this->opt[$index] = Array($value, $text);
|
| 172: | $this->options = '';
|
| 173: | $this->genOptionList();
|
| 174: | }
|
| 175: |
|
| 176: | public function getOptionsElement($index) {
|
| 177: | return $this->opt[$index];
|
| 178: | }
|
| 179: |
|
| 180: | public function removeOptionsElement($index) {
|
| 181: | unset($this->opt[$index]);
|
| 182: | $this->options = '';
|
| 183: | $this->genOptionList();
|
| 184: | }
|
| 185: |
|
| 186: | public function setOptionsFromTable($valueField, $textField = '', $tableName = '', $logic = '', $sql = '', $cacheTime = '0') {
|
| 187: | $tblName = \SphpBase::page()->tblName;
|
| 188: | $mysql = \SphpBase::dbEngine();
|
| 189: | $blnMultiTextField = false;
|
| 190: | $blnMultiValField = false;
|
| 191: | $arr1 = Array();
|
| 192: | $ar1 = explode(",", $textField);
|
| 193: | if (count($ar1) > 1) {
|
| 194: | $blnMultiTextField = true;
|
| 195: | }
|
| 196: | $ar2 = explode(",", $valueField);
|
| 197: | if (count($ar2) > 1) {
|
| 198: | $blnMultiValField = true;
|
| 199: | }
|
| 200: |
|
| 201: | if ($tableName == '') {
|
| 202: | $tableName = $tblName;
|
| 203: | }
|
| 204: | if($sql==''){
|
| 205: | if ($textField == '') {
|
| 206: | if($blnMultiValField){
|
| 207: | $textField = $ar2[0];
|
| 208: | }else{
|
| 209: | $textField = $valueField;
|
| 210: | }
|
| 211: | $sql = "SELECT $valueField FROM $tableName $logic";
|
| 212: | } else {
|
| 213: | $sql = "SELECT $valueField,$textField FROM $tableName $logic";
|
| 214: | }
|
| 215: | }
|
| 216: |
|
| 217: | $result = $mysql->fetchQuery($sql, $cacheTime, '', $valueField);
|
| 218: | foreach ($result as $index2 => $row2) {
|
| 219: | foreach ($row2 as $index => $row) {
|
| 220: | $strv1 = "";
|
| 221: | $strt1 = "";
|
| 222: | if ($blnMultiTextField) {
|
| 223: | foreach ($ar1 as $key => $value) {
|
| 224: | $strt1 .= $row[$value] . ' ';
|
| 225: | }
|
| 226: | } else {
|
| 227: | $strt1 = $row[$textField];
|
| 228: | }
|
| 229: | if ($blnMultiValField) {
|
| 230: | foreach ($ar2 as $key => $value) {
|
| 231: | $strv1 .= $row[$value] . ',';
|
| 232: | }
|
| 233: | } else {
|
| 234: | $strv1 = $row[$valueField];
|
| 235: | }
|
| 236: | $arr1[] = Array($strv1, $strt1);
|
| 237: | }
|
| 238: | }
|
| 239: | $this->setOptionsKeyArray();
|
| 240: | $this->setOptionsArray($arr1);
|
| 241: | }
|
| 242: |
|
| 243: | public function onprejsrender() {
|
| 244: | if ($this->formName != '' && $this->notvalue != '') {
|
| 245: | $jscode = "if(blnSubmit==true && " . $this->getJSValue() . "=='" . $this->notvalue . "'){
|
| 246: | blnSubmit = false ;
|
| 247: | alert('Please Select One Option From " . $this->msgName . "');
|
| 248: | document.getElementById('$this->name').focus();
|
| 249: | }";
|
| 250: | addFooterJSFunctionCode("{$this->formName}_submit", "$this->name", $jscode);
|
| 251: | }
|
| 252: | }
|
| 253: |
|
| 254: | public function processStaticOptions($element) {
|
| 255: | if ($element->tag == 'option') {
|
| 256: | if (($element->hasAttribute('value') && strtolower($element->getAttribute('value')) == strtolower($this->value)) || strtolower($element->innertext) == strtolower($this->value)) {
|
| 257: | $element->attr[' selected'] = 'selected';
|
| 258: | }
|
| 259: | $this->fstatic = true;
|
| 260: | }
|
| 261: | }
|
| 262: |
|
| 263: | public function onrender() {
|
| 264: | $HTMLParser = new \Sphp\tools\HTMLParser();
|
| 265: | if($this->errmsg!=""){
|
| 266: | $this->setPostTag($this->errmsg);
|
| 267: | }
|
| 268: | if ($this->getAttribute('class') == '') {
|
| 269: | $this->class = "form-control";
|
| 270: | }
|
| 271: | if ($this->options != '') {
|
| 272: | $this->innerHTML = $this->getOptionsHTML();
|
| 273: | } else {
|
| 274: | $this->innerHTML = $HTMLParser->parseHTMLTag($this->innerHTML, 'processStaticOptions', $this);
|
| 275: | if (!$this->fstatic) {
|
| 276: | $this->innerHTML .= '<option selected="selected">' . $this->value . '</option>';
|
| 277: | }
|
| 278: | }
|
| 279: | }
|
| 280: |
|
| 281: | public function getOptionsHTML() {
|
| 282: | $vals = $this->value;
|
| 283: | $strOut = '';
|
| 284: | $CF = 0;
|
| 285: | $arr1 = Array();
|
| 286: |
|
| 287: | if ($this->strFirstValue != "null") {
|
| 288: | $stra = explode(',', $this->strFirstValue);
|
| 289: | if (count($stra) > 1) {
|
| 290: | $arr1[] = Array($stra[0], $stra[1]);
|
| 291: | } else {
|
| 292: | $arr1[] = Array($stra[0], $stra[0]);
|
| 293: | }
|
| 294: | $holder = array_merge($arr1, $this->opt);
|
| 295: | } else {
|
| 296: | $holder = $this->opt;
|
| 297: | }
|
| 298: |
|
| 299: |
|
| 300: | if ($vals != '') {
|
| 301: | $holder2 = $vals;
|
| 302: | foreach ($holder as $key2 => $val) {
|
| 303: | $key = $val[0];
|
| 304: | if (!$this->blnuseasoc) {
|
| 305: | $key = $val[1];
|
| 306: | }
|
| 307: |
|
| 308: | if ($key == $holder2) {
|
| 309: | $strOut .= "<option value=\"$key\" selected>" . $val[1] . "</option>";
|
| 310: | } else {
|
| 311: | $strOut .= "<option value=\"$key\">" . $val[1] . "</option>";
|
| 312: | }
|
| 313: | }
|
| 314: | } else {
|
| 315: | foreach ($holder as $key2 => $val) {
|
| 316: | $key = $val[0];
|
| 317: | if (!$this->blnuseasoc) {
|
| 318: | $key = $val[1];
|
| 319: | }
|
| 320: | if ($CF == $this->selectedIndex) {
|
| 321: | $this->value = $key;
|
| 322: | $strOut .= "<option value=\"$key\" selected>" . $val[1] . "</option>";
|
| 323: | } else {
|
| 324: | $strOut .= "<option value=\"$key\">" . $val[1] . "</option>";
|
| 325: | }
|
| 326: | $CF += 1;
|
| 327: | }
|
| 328: | }
|
| 329: | return $strOut;
|
| 330: | }
|
| 331: |
|
| 332: |
|
| 333: | public function getJSValue() {
|
| 334: | return "document.getElementById('$this->name').options[document.getElementById('$this->name').selectedIndex].value";
|
| 335: | }
|
| 336: |
|
| 337: | public function setJSValue($exp) {
|
| 338: | return " jql(\"select#$this->name\").val($exp); ";
|
| 339: | }
|
| 340: |
|
| 341: | public function setJSOptionValue($exp) {
|
| 342: | return "document.getElementById('$this->name').options[document.getElementById('$this->name').selectedIndex] = $exp;";
|
| 343: | }
|
| 344: |
|
| 345: | public function getJSSelectedIndex() {
|
| 346: | return "document.getElementById('$this->name').selectedIndex";
|
| 347: | }
|
| 348: |
|
| 349: | }
|
| 350: | }
|
| 351: | |