selectField.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\forms;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Select Form Field
  6. *
  7. * @author Michal Czech <michael@modulesgarden.com>
  8. */
  9. class selectField extends abstractField{
  10. public $translateOptions = true;
  11. public $addValueIfNotExits = false;
  12. public $options;
  13. public $select2 = false;
  14. public $multiple;
  15. function prepare() {
  16. if($this->select2)
  17. {
  18. $this->type = 'select2';
  19. if(empty($this->addIDs))
  20. {
  21. $this->addIDs = 'RandID'.rand(0,100);
  22. }
  23. }
  24. else
  25. {
  26. $this->type = 'select';
  27. }
  28. if(array_keys($this->options) == range(0, count($this->options) - 1))
  29. {
  30. $options = array();
  31. foreach($this->options as $value)
  32. {
  33. $options[$value] = $value;
  34. }
  35. $this->options = $options;
  36. }
  37. else
  38. {
  39. $this->translateOptions = false;
  40. }
  41. if($this->addValueIfNotExits)
  42. {
  43. if($this->value && !isset($this->options[$this->value]))
  44. {
  45. $this->options[$this->value] = $this->value;
  46. }
  47. }
  48. if($this->translateOptions)
  49. {
  50. if(!is_array($this->options))throw new main\mgLibs\exceptions\system('Invalid Fields Options');
  51. $options = array();
  52. foreach($this->options as $value)
  53. {
  54. $options[$value] = main\mgLibs\lang::T($this->formName,$this->name,'options',$value);
  55. }
  56. $this->options = $options;
  57. }
  58. }
  59. }