/system/classes/datatype.php

https://gitlab.com/srueegger/Social-Meal · PHP · 211 lines · 101 code · 17 blank · 93 comment · 14 complexity · 8f04f5295433b3884c2184b1b04b1eea MD5 · raw file

  1. <?PHP
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /**
  20. * @package ContentLion-Core
  21. * @author Stefan Wienstroeer
  22. */
  23. class DataType{
  24. private $id = -1;
  25. private $displayName = "";
  26. private $dataName = "";
  27. private $description = "";
  28. private $fields = array();
  29. private $validators = null;
  30. /**
  31. *
  32. * Creates a new datatype object
  33. * @param int id of the datatype
  34. */
  35. public function __construct($id){
  36. $this->id = $id;
  37. $id = DataBase::Current()->EscapeString($id);
  38. if($obj = DataBase::Current()->ReadRow("SELECT * FROM {'dbprefix'}datatypes WHERE id='".$id."'")){
  39. $this->displayName = $obj->displayName;
  40. $this->dataName = $obj->dataName;
  41. $this->description = $obj->description;
  42. }
  43. }
  44. /**
  45. *
  46. * returns a datatype by name. Is null if nothing was found.
  47. * @param string name of the datatype
  48. * @return DataType founded datatype
  49. */
  50. public static function GetByName($name){
  51. $name = DataBase::Current()->EscapeString($name);
  52. $id = DataBase::Current()->ReadField("SELECT id FROM {'dbprefix'}datatypes WHERE dataname = '".$name."'");
  53. if($id){
  54. return new DataType($id);
  55. }
  56. else{
  57. return null;
  58. }
  59. }
  60. /**
  61. *
  62. * returns the datafields
  63. * @return list of datafields
  64. */
  65. public function getFields(){
  66. if(sizeOf($this->fields) == 0){
  67. $this->fields = DataField::getByDataType($this);
  68. }
  69. return $this->fields;
  70. }
  71. /**
  72. *
  73. * returns the id of the datatype
  74. * @return int id
  75. */
  76. public function getID(){
  77. return $this->id;
  78. }
  79. /**
  80. *
  81. * @param array $values
  82. * @param array $ignoreColumns
  83. * @return string
  84. */
  85. public function getInsertStatement($values, $ignoreColumns = array()){
  86. return "INSERT INTO {'dbprefix'}".$this->dataName." (".$this->getCommaSeparatedColumnNames($ignoreColumns).") VALUES (".$this->commaSepearate($values).");";
  87. }
  88. /**
  89. *
  90. * @param array $ignoreColumns
  91. * @return string
  92. */
  93. private function getCommaSeparatedColumnNames($ignoreColumns){
  94. $res = "";
  95. foreach($this->getFields() as $field){
  96. if(!is_array($ignoreColumns) || !in_array($field->getDataName(),$ignoreColumns)){
  97. $res .= $field->getDataName().",";
  98. }
  99. }
  100. if(strlen($res) > 0){
  101. $res = substr($res,0,-1);
  102. }
  103. return $res;
  104. }
  105. /**
  106. *
  107. * @param array $values
  108. * @return string
  109. */
  110. private function commaSepearate($values){
  111. $res = "";
  112. foreach($values as $value){
  113. $res .= $value.",";
  114. }
  115. if(strlen($res) > 0){
  116. $res = substr($res,0,-1);
  117. }
  118. return $res;
  119. }
  120. /**
  121. *
  122. * @return array
  123. */
  124. private function getValidators(){
  125. if($this->validators == null){
  126. $this->validators = DataTypeValidator::getByDataType($this);
  127. }
  128. return $this->validators;
  129. }
  130. /**
  131. *
  132. * validates the params for insert
  133. * @param array key-value pairs, fields have to be escaped!
  134. * @return boolean is valid
  135. */
  136. public function validate($params){
  137. $res = true;
  138. foreach($this->getValidators() as $validator){
  139. $res = $validator->validate($params) && $res;
  140. }
  141. return $res;
  142. }
  143. /**
  144. *
  145. * @return array
  146. */
  147. public function getValidatiingErrors(){
  148. $res = array();
  149. foreach($this->getValidators() as $validator){
  150. $message = $validator->getErrorMessage();
  151. if($message != ""){
  152. $res[] = $message;
  153. }
  154. }
  155. return $res;
  156. }
  157. /**
  158. *
  159. * @return array
  160. */
  161. public function getAll(){
  162. return DataBase::Current()->ReadRows("SELECT * FROM {'dbprefix'}".$this->dataName);
  163. }
  164. /**
  165. *
  166. * @return string
  167. */
  168. public function getDataName(){
  169. return $this->dataName;
  170. }
  171. /**
  172. *
  173. * @return array
  174. */
  175. public function getShares(){
  176. return DatatypeShare::getByDataType($this);
  177. }
  178. /**
  179. *
  180. * @param string $apikey
  181. * @return DataTypeShare
  182. */
  183. public function getShare($apikey){
  184. return DataTypeShare::GetByDataTypeAndApiKey($this,$apikey);
  185. }
  186. /**
  187. * @param string $apikey
  188. * @return boolean
  189. */
  190. public function allowShare($apikey){
  191. return $this->getShare($apikey) != null;
  192. }
  193. }
  194. ?>