repository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace MGModule\DNSManager2\models\whmcs\clients\customFields;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Description of repository
  6. *
  7. * @author Michal Czech <michael@modulesgarden.com>
  8. */
  9. class repository {
  10. public $serviceID;
  11. private $_customFields;
  12. /**
  13. * Mozna by bylo dodac wersje z wczytywanie po samym productid
  14. *
  15. * @author Michal Czech <michael@modulesgarden.com>
  16. * @param type $accountID
  17. */
  18. function __construct($serviceID, array $data = array()) {
  19. $this->serviceID = $serviceID;
  20. if($data)
  21. {
  22. foreach ($data as $name => $value)
  23. {
  24. $field = new customField();
  25. $field->name = $name;
  26. $field->value = $value;
  27. $this->_customFields[$field->name] = $field;
  28. }
  29. }
  30. else
  31. {
  32. $this->load();
  33. }
  34. }
  35. function __isset($name) {
  36. return $this->_customFields[$name];
  37. }
  38. function __get($name) {
  39. if(isset($this->_customFields[$name]))
  40. {
  41. return $this->_customFields[$name]->value;
  42. }
  43. }
  44. function __set($name, $value) {
  45. if(isset($this->_customFields[$name]))
  46. {
  47. $this->_customFields[$name]->value = $value;
  48. }
  49. }
  50. function load(){
  51. $query = "
  52. SELECT
  53. C.fieldname as name
  54. ,V.fieldid as fieldid
  55. ,V.value as value
  56. FROM
  57. tblcustomfieldsvalues V
  58. JOIN
  59. tblcustomfields C
  60. ON
  61. V.fieldid = C.id
  62. AND C.type = 'client'
  63. WHERE
  64. V.relid = :account_id
  65. ";
  66. $result = \MGModule\DNSManager2\mgLibs\MySQL\query::query($query,array(
  67. 'account_id' => $this->serviceID
  68. ));
  69. while($row = $result->fetch())
  70. {
  71. $name = explode('|', $row['name']);
  72. if(isset($this->_customFields[$name[0]]))
  73. {
  74. $this->_customFields[$name[0]]->id = $row['fieldid'];
  75. }
  76. else
  77. {
  78. $field = new customField();
  79. $field->id = $row['fieldid'];
  80. $field->name = $name[0];
  81. $field->value = $row['value'];
  82. $this->_customFields[$field->name] = $field;
  83. }
  84. }
  85. }
  86. /**
  87. * Update Custom Fields
  88. *
  89. * @author Michal Czech <michael@modulesgarden.com>
  90. */
  91. function update(){
  92. $this->load();
  93. foreach($this->_customFields as $field){
  94. main\mgLibs\MySQL\query::update(
  95. 'tblcustomfieldsvalues'
  96. , array(
  97. 'value' => $field->value
  98. )
  99. , array(
  100. 'fieldid' => $field->id
  101. ,'relid' => $this->serviceID
  102. )
  103. );
  104. }
  105. }
  106. }