repository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace MGModule\DNSManager2\models\whmcs\customFields;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Product Custom Fields Colletion
  6. *
  7. * @author Michal Czech <michael@modulesgarden.com>
  8. */
  9. class repository{
  10. public $relationID;
  11. public $type;
  12. private $_fields = array();
  13. static private $configuration;
  14. static function setConfiguration(array $configuration){
  15. self::$configuration = $configuration;
  16. }
  17. /**
  18. * Load Product Custom Fields
  19. *
  20. * @author Michal Czech <michael@modulesgarden.com>
  21. * @param int $productID
  22. */
  23. function __construct($type,$relationID) {
  24. $this->type = $type;
  25. $this->relationID = $relationID;
  26. $result = main\mgLibs\MySQL\query::select(
  27. customField::fieldDeclaration()
  28. ,customField::tableName()
  29. , array(
  30. 'relid' => $this->relationID
  31. ,'type' => $this->type
  32. )
  33. );
  34. while ($row = $result->fetch()){
  35. $this->_fields[] = new customField($row['id'],$this->type, $this->relationID,$row);
  36. }
  37. }
  38. /**
  39. * Compare current Fields with Declaration from Module Configuration
  40. *
  41. * @author Michal Czech <michael@modulesgarden.com>
  42. * @param bool $onlyRequired
  43. * @return array
  44. */
  45. function checkFields(array $configuration = array()){
  46. if(empty($configuration))
  47. {
  48. $configuration = self::$configuration;
  49. }
  50. $missingFields = array();
  51. foreach($configuration as $fieldDeclaration)
  52. {
  53. $found = false;
  54. foreach($this->_fields as $field)
  55. {
  56. if($fieldDeclaration->name === $field->name)
  57. {
  58. $found = true;
  59. break;
  60. }
  61. }
  62. if(!$found)
  63. {
  64. $name = (empty($fieldDeclaration->friendlyName))?$fieldDeclaration->name:$fieldDeclaration->friendlyName;
  65. $missingFields[$fieldDeclaration->name] = $name;
  66. }
  67. }
  68. return $missingFields;
  69. }
  70. /**
  71. * Generate Custom Fields Depends on declaration in Module Configuration
  72. *
  73. * @author Michal Czech <michael@modulesgarden.com>
  74. */
  75. function generateFromConfiguration(array $configuration = array()){
  76. if(empty($configuration))
  77. {
  78. $configuration = self::$configuration;
  79. }
  80. foreach($configuration as $fieldDeclaration)
  81. {
  82. $found = false;
  83. foreach($this->_fields as $field)
  84. {
  85. if($fieldDeclaration->name === $field->name)
  86. {
  87. $found = true;
  88. break;
  89. }
  90. }
  91. if(!$found)
  92. {
  93. $fieldDeclaration->save();
  94. $this->_fields[] = $fieldDeclaration;
  95. }
  96. }
  97. }
  98. /**
  99. *
  100. * @return customField[]
  101. */
  102. function get(){
  103. return $this->_fields;
  104. }
  105. }