customField.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace MGModule\DNSManager2\models\whmcs\customFields;
  3. use MGModule\DNSManager2 as main;
  4. /**
  5. * Product Custom Fields depends on WHMCS
  6. *
  7. * @Table(name=tblcustomfields,preventUpdate,prefixed=false)
  8. * @author Michal Czech <michael@modulesgarden.com>
  9. */
  10. class customField extends \MGModule\DNSManager2\mgLibs\models\orm{
  11. /**
  12. * @Column()
  13. * @var int
  14. */
  15. public $id;
  16. /**
  17. *
  18. * @var string
  19. */
  20. public $parentType;
  21. /**
  22. *
  23. * @var int
  24. */
  25. public $parentId;
  26. /**
  27. *
  28. * @var int
  29. */
  30. public $definition;
  31. /**
  32. * @Column(name=fieldname,as=fieldnameFull)
  33. * @var string
  34. */
  35. public $name;
  36. /**
  37. *
  38. * @var string
  39. */
  40. public $friendlyName;
  41. /**
  42. * @Column()
  43. * @var string
  44. */
  45. public $fieldtype = 'text';
  46. /**
  47. *
  48. * @Column()
  49. * @var string
  50. */
  51. public $description = '';
  52. /**
  53. * @Column(as=fieldoptionsEncoded)
  54. * @var array
  55. */
  56. public $fieldoptions = array();
  57. /**
  58. * @Column()
  59. * @var string
  60. */
  61. public $regexpr = '';
  62. /**
  63. * @Column()
  64. * @var boolean
  65. */
  66. public $adminonly = 'on';
  67. /**
  68. * @Column()
  69. * @var boolean
  70. */
  71. public $required = '';
  72. /**
  73. * @Column()
  74. * @var boolean
  75. */
  76. public $showorder = '';
  77. /**
  78. * @Column()
  79. * @var boolean
  80. */
  81. public $showinvoice = '';
  82. /**
  83. * @Column()
  84. * @var boolean
  85. */
  86. public $sortorder = 0;
  87. /**
  88. * Load Custom Field
  89. *
  90. * @author Michal Czech <michael@modulesgarden.com>
  91. * @param int $productID
  92. * @param int $id
  93. * @param array $data
  94. */
  95. function __construct($id = null, $parentType = null, $parentID = null, $data = array()) {
  96. $this->id = $id;
  97. $this->parentType = $parentType;
  98. $this->parentId = $parentID;
  99. if($this->id && empty($data))
  100. {
  101. $conditions = array(
  102. 'id' => $this->id
  103. ,'reldid' => $this->parentId
  104. ,'type' => $this->parentType
  105. );
  106. $data = main\mgLibs\MySQL\query::select(
  107. self::$fieldDeclaration
  108. , self::tableName()
  109. , $conditions
  110. )->fetch();
  111. if(empty($data))
  112. {
  113. throw new main\mgLibs\exceptions\system('Unable to find custom field:'.http_build_query($conditions));
  114. }
  115. }
  116. if(!empty($data))
  117. {
  118. if(!empty($data['fieldnameFull']))
  119. {
  120. $tmp = explode('|',$data['fieldnameFull']);
  121. $data['name'] = $data['fieldnameFull'] = $tmp[0];
  122. if(!empty($tmp[1]))
  123. {
  124. $data['friendlyName'] = $tmp[1];
  125. }
  126. }
  127. if(isset($data['fieldoptionsEncoded']))
  128. {
  129. $data['fieldoptions'] = array_filter(explode(',', $data['fieldoptionsEncoded']));
  130. }
  131. $this->fillProperties($data);
  132. }
  133. }
  134. /**
  135. * Save Field
  136. *
  137. * @author Michal Czech <michael@modulesgarden.com>
  138. */
  139. function save(){
  140. $data = array(
  141. 'type' => $this->parentType
  142. ,'relid' => $this->productID
  143. ,'fieldtype' => $this->fieldtype
  144. ,'description' => $this->description
  145. ,'fieldoptions' => implode(',',$this->fieldoptions)
  146. ,'regexpr' => $this->regexpr
  147. ,'adminonly' => $this->adminonly
  148. ,'required' => $this->required
  149. ,'showorder' => $this->showorder
  150. ,'showinvoice' => $this->showinvoice
  151. ,'sortorder' => $this->sortorder
  152. );
  153. if(empty($this->friendlyName))
  154. {
  155. $data['fieldname'] = $this->name;
  156. }
  157. else
  158. {
  159. $data['fieldname'] = $this->name.'|'.$this->friendlyName;
  160. }
  161. if($this->id)
  162. {
  163. main\mgLibs\MySQL\query::update(
  164. self::tableName()
  165. , $data
  166. , array(
  167. 'id' => $this->id
  168. )
  169. );
  170. }
  171. else
  172. {
  173. $this->id = main\mgLibs\MySQL\query::insert(
  174. self::tableName()
  175. ,$data
  176. );
  177. }
  178. }
  179. }