testItem.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace MGModule\DNSManager2\models\testGroup\testItem;
  3. use MGModule\DNSManager2 as main;
  4. use MGModule\DNSManager2\mgLibs;
  5. use MGModule\DNSManager2\models\testGroup\simpleItem\SimpleItem;
  6. /**
  7. * Example Item Class
  8. *
  9. * @Table(name=test_item)
  10. * @author Michal Czech <michael@modulesgarden.com>
  11. */
  12. class testItem extends main\mgLibs\models\orm{
  13. static $avaibleOptionsA = array(
  14. 1 => 'Option1'
  15. ,2 => 'Option2'
  16. ,3 => 'Option3'
  17. );
  18. static $avaibleOptionsB = array(1,2,3,4);
  19. static $avaibleOptionsC = array(1,2,3,4,5,6,7,8,9,10);
  20. /**
  21. * ID field
  22. *
  23. * @Column(id)
  24. * @var int
  25. */
  26. public $id;
  27. /**
  28. * Text Field
  29. *
  30. * @Column(varchar=512,name=text_field)
  31. * @Validation(notEmpty)
  32. * @var string
  33. */
  34. public $textField;
  35. /**
  36. * Text2 Field
  37. *
  38. * @Column(varchar=256)
  39. * @Validation(notEmpty)
  40. * @var string
  41. */
  42. public $text2;
  43. /**
  44. * Checkbox Value
  45. *
  46. * @Column(text,as=checkboxSerialized)
  47. * @var array
  48. */
  49. public $checkbox;
  50. /**
  51. * Boolean Field
  52. *
  53. * @Column(boolean)
  54. * @var boolean
  55. */
  56. public $onoff;
  57. /**
  58. * Value
  59. *
  60. * @Column(smallint)
  61. * @var int
  62. */
  63. public $radio;
  64. /**
  65. * @Column(int)
  66. * @var int
  67. */
  68. public $option;
  69. /**
  70. * Number of relations
  71. *
  72. * @var int
  73. */
  74. public $simpleNum;
  75. /**
  76. * Multi Value
  77. *
  78. * @Column(text,as=option2Serialized)
  79. * @Validation(notEmpty)
  80. * @var array
  81. */
  82. public $option2;
  83. /**
  84. * Password
  85. *
  86. * @Column(varchar=32,as=passwordEncrypted)
  87. * @var string
  88. */
  89. public $password;
  90. function __construct($id = false, $data = array()) {
  91. if($id !== false && empty($data))
  92. {
  93. $sql = "
  94. SELECT
  95. ".mgLibs\MySQL\query::formatSelectFields(testItem::fieldDeclaration(),'B')."
  96. ,count(S.". SimpleItem::getProperyColumn('id').") as simpleNum
  97. FROM
  98. ".testItem::tableName()." B
  99. JOIN
  100. ".SimpleItem::tableName()." S
  101. WHERE
  102. B.id = :id
  103. GROUP BY
  104. B.id
  105. ";
  106. $data = main\mgLibs\MySQL\query::query($sql,array('id'=>$id))->fetch();
  107. if(empty($data))
  108. {
  109. throw new main\mgLibs\exceptions\system('Unable to find Element with ID:'.$id);
  110. }
  111. }
  112. if($data)
  113. {
  114. $data['checkbox'] = unserialize(base64_decode($data['checkboxSerialized']));
  115. $data['option2'] = unserialize(base64_decode($data['option2Serialized']));
  116. $data['password'] = $this->decrypt($data['passwordEncrypted']);
  117. }
  118. parent::__construct(false, $data);
  119. }
  120. function save(){
  121. $data = array(
  122. 'text_field' => $this->textField
  123. ,'text2' => $this->text2
  124. ,'checkbox' => base64_encode(serialize($this->checkbox))
  125. ,'onoff' => (bool)$this->onoff
  126. ,'radio' => $this->radio
  127. ,'option' => $this->option
  128. ,'option2' => base64_encode(serialize($this->option2))
  129. ,'password' => $this->encrypt($this->password)
  130. );
  131. if($this->id)
  132. {
  133. main\mgLibs\MySQL\query::update(
  134. self::tableName()
  135. , $data
  136. , array(
  137. 'id' => $this->id
  138. )
  139. );
  140. }
  141. else
  142. {
  143. $this->id = main\mgLibs\MySQL\query::insert(
  144. self::tableName()
  145. , $data
  146. );
  147. }
  148. }
  149. }