CheckPointModel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\entity\ProjectsLocotech;
  6. use app\models\entity\CheckpointTypesForTasks;
  7. use app\models\entity\CheckpointTypeHierarchy;
  8. use app\models\entity\Tasks;
  9. use app\models\entity\Tasktypes;
  10. use app\models\entity\CheckpointTypes;
  11. use app\models\entity\Checkpoints;
  12. class CheckPointModel extends Model
  13. {
  14. public $id;
  15. public $typeId;
  16. public $priority;
  17. public $name;
  18. public $parentTaskId;
  19. public $parentCheckpointId;
  20. public $state;
  21. public $childrenCheckpoints;
  22. public $class;
  23. public $value;
  24. function __construct(){}
  25. public static function CreateFromID($id)
  26. {
  27. global $link;
  28. $query = mysqli_query($link, "select * from checkpoints where id=$id");
  29. if ($checkpoint_arr = mysqli_fetch_array($query))
  30. {
  31. return CheckPoint::CreateFromArray($checkpoint_arr);
  32. }
  33. return null;
  34. }
  35. public static function CreateFromArray($checkpointArr)
  36. {
  37. $checkpoint = new CheckPoint();
  38. $checkpoint->id = $checkpointArr['id'];
  39. $checkpoint->name = $checkpointArr['name'];
  40. $checkpoint->priority = $checkpointArr['priority'];
  41. $checkpoint->typeId = $checkpointArr['type'];
  42. $checkpoint->state = $checkpointArr['is_set'];
  43. $checkpoint->class = $checkpointArr['class'];
  44. $checkpoint->value = $checkpointArr['value'];
  45. $checkpoint->parentTaskId = $checkpointArr['parent_task_id'];
  46. $checkpoint->parentCheckpointId = intval($checkpointArr['parent_checkpoint_id']);
  47. $checkpoint->childrenCheckpoints = array();
  48. $checkpoint->FillChildren();
  49. $checkpoint->SetNameAndClass();
  50. return $checkpoint;
  51. }
  52. // работает
  53. public static function CreateFromType(CheckPointTypeModel $checkpointType, $parentCheckpointId = null, $parentTaskId = null)
  54. {
  55. //TODO брать все что можно из типа
  56. //TODO сначала парент, потом чайлдов
  57. global $link;
  58. $checkpoint = new CheckPointModel();
  59. $checkpoint->name = $checkpointType->name;
  60. $checkpoint->priority = $checkpointType->priority;
  61. $checkpoint->typeId = $checkpointType->id;
  62. $checkpoint->state = '0';
  63. $checkpoint->class = $checkpointType->class;
  64. if ($parentCheckpointId != null)
  65. $checkpoint->parentCheckpointId = intval($parentCheckpointId);
  66. else
  67. $checkpoint->parentCheckpointId = 0;
  68. if ($parentTaskId != null)
  69. $checkpoint->parentTaskId = intval($parentTaskId);
  70. else
  71. $checkpoint->parentTaskId = 0;
  72. $checkpointsEntity = new Checkpoints();
  73. $checkpointsEntity->type = $checkpoint->typeId;
  74. $checkpointsEntity->parent_checkpoint_id = $checkpoint->parentCheckpointId;
  75. $checkpointsEntity->text = $checkpoint->name;
  76. $checkpointsEntity->parent_task_id = $checkpoint->parentTaskId;
  77. $checkpointsEntity->save();
  78. foreach ($checkpointType->childrenCheckpointTypes as $childype)
  79. {
  80. $cp = CheckPoint::CreateFromType($childype, $id, null);
  81. }
  82. return $checkpoint;
  83. }
  84. // public static function GetallChildren
  85. public static function CreateFromScratch($class, $name, $parentTaskTypeId = null)
  86. {
  87. global $link;
  88. $checkpointType = new CheckPointType();
  89. $checkpointType->name = $name;
  90. $checkpointType->class = $class;
  91. $checkpointType->childrenCheckpointTypes = array();
  92. mysqli_query($link, "insert into checkpoint_types (class, name) values ('$class', '$name')");
  93. $checkpointType->id = mysqli_insert_id($link);
  94. if ($parentTaskTypeId != null) {
  95. mysqli_query($link, "insert into checkpoint_type_hierarchy (parent_cp_type, cp_type) values ($parentTaskTypeId, $checkpointType->id)");
  96. //mysqli_query($link, "update checkpoint_type_hierarchy set parent_cp_type=$parentTaskTypeId, cp_type=$checkpointType->id");
  97. }
  98. return $checkpointType;
  99. }
  100. public static function GetClasses()
  101. {
  102. return array(0, "DropList", "CheckBox", "MultiSelect", "SerialNumber");
  103. }
  104. public function SetValue($value)
  105. {
  106. global $link;
  107. $state = false;
  108. if ($value == "False"){
  109. $value = 0;$state = true;}
  110. elseif ($value == "True")
  111. {$value = 1;$state = true;}
  112. if (!$state)
  113. $str = "update checkpoints set value = '$value' where id=".$this->id;
  114. else
  115. $str = "update checkpoints set is_set = $value where id=".$this->id;
  116. mysqli_query($link, $str);
  117. }
  118. function GetClassID()
  119. {
  120. switch ($this->class)
  121. {
  122. case "DropList": return 1;
  123. case "CheckBox": return 2;
  124. case "MultiSelect": return 3;
  125. case "SerialNumber": return 4;
  126. }
  127. }
  128. function GetClassName($class)
  129. {
  130. switch ($class)
  131. {
  132. case "DropList": return 'Один из нескольких вариантов';
  133. case "CheckBox": return 'Варианты';
  134. case "MultiSelect": return 'Выбрать несколько вариантов';
  135. case "SerialNumber": return "Ввод значения";
  136. }
  137. }
  138. function FillChildren()
  139. {
  140. global $link;
  141. $query = mysqli_query($link, "select * from checkpoints where parent_checkpoint_id=".$this->id." ORDER BY priority");
  142. while ($checkarr = mysqli_fetch_array($query))
  143. {
  144. $child = CheckPoint::CreateFromArray($checkarr);
  145. array_push($this->childrenCheckpoints, $child);
  146. }
  147. }
  148. function SetNameAndClass()
  149. {
  150. global $link;
  151. $query = mysqli_query($link, "select name, class from checkpoint_types where id=".$this->typeId);
  152. if ($res = mysqli_fetch_row($query)) {
  153. $this->name = $res[0];
  154. $this->class = $res[1];
  155. }
  156. }
  157. function PackChildrenToBinary()
  158. {
  159. $buf = "";
  160. foreach ($this->childrenCheckpoints as $checkpoint)
  161. {
  162. $buf .= $checkpoint->PackToBinary();
  163. }
  164. return $buf;
  165. }
  166. function PackToBinary()
  167. {
  168. $lenname = strlen($this->name);
  169. $len = 21 + $lenname+strlen($this->value);
  170. $classid = $this->GetClassID();
  171. $buf = pack("vlCCvllCv*",
  172. $len,
  173. $this->id,
  174. $this->priority,
  175. $this->state,
  176. $this->typeId,
  177. $this->parentTaskId,
  178. $this->parentCheckpointId,
  179. $classid,
  180. $lenname
  181. ); //v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  182. $buf .= $this->name;
  183. $buf .= $this->value;
  184. return $buf;
  185. }
  186. public static function Find($parentCheckpointId = "all", $parent_task_id = null, $class = null)
  187. {
  188. //$parentCheckpointId > 0: find children
  189. //$parentCheckpointId == 0: find parents
  190. //$parentCheckpointId == -1: find all
  191. global $link;
  192. $addByTask = '';
  193. $addParentOnly = '';
  194. if ($parent_task_id) {
  195. $addByTask = " where parent_task_id=" . $parent_task_id;
  196. }
  197. $parentCheckpointId = intval($parentCheckpointId);
  198. if ($parentCheckpointId > -1) {
  199. $parentFind = "parent_checkpoint_id=$parentCheckpointId";
  200. if ($parentCheckpointId === 0)
  201. $parentFind .= " or parent_checkpoint_id is null";
  202. if ($parent_task_id) {
  203. $add = "and (";
  204. $parentFind .= ")";
  205. }
  206. else
  207. $add = "where";
  208. $addParentOnly = " $add $parentFind";
  209. }
  210. $str = "select * from checkpoints $addByTask $addParentOnly";
  211. //echo $str;
  212. $checkpoints = array(); //массив значений чекпойнтов
  213. $query = mysqli_query($link, $str);
  214. while ($checkpoint_arr = mysqli_fetch_array($query))
  215. {
  216. $check = CheckPoint::CreateFromArray($checkpoint_arr);
  217. array_push($checkpoints, $check);
  218. }
  219. return $checkpoints;
  220. }
  221. public static function FindAllTypes()
  222. {
  223. global $link;
  224. $checkpointTypes = array(); //массив значений чекпойнтов
  225. $query = mysqli_query($link, "select * from checkpoint_types");
  226. while ($checkpoint_arr = mysqli_fetch_array($query))
  227. {
  228. array_push($checkpointTypes, $checkpoint_arr);
  229. }
  230. return $checkpointTypes;
  231. }
  232. public static function FindAllByTask($parent_task_id)
  233. {
  234. global $link;
  235. $checkpoints = array(); //массив значений чекпойнтов
  236. $query = mysqli_query($link, "select * from checkpoints where parent_task_id=".$parent_task_id);
  237. while ($checkpoint_arr = mysqli_fetch_array($query))
  238. {
  239. $check = CheckPoint::CreateFromArray($checkpoint_arr);
  240. array_push($checkpoints, $check);
  241. }
  242. return $checkpoints;
  243. }
  244. public static function GetCheckPointClasses()
  245. {
  246. global $link;
  247. $class_arr = array();
  248. $query = mysqli_query($link, "SHOW COLUMNS FROM checkpoint_types WHERE Field = 'class'" );
  249. if ($class = mysqli_fetch_array($query))
  250. {
  251. $enums = $class[1];
  252. //array_push($class, $class_arr);
  253. $mch = preg_match ( "/^enum\(/" , $enums);
  254. $patterns = array();
  255. $patterns[0] = '/^enum\(/';
  256. $patterns[1] = '/\)/';
  257. $patterns[2] = '/\'/';
  258. $replacements = array();
  259. $replacements[0] = '';
  260. $replacements[1] = '';
  261. $replacements[2] = '';
  262. $str = preg_replace($patterns, $replacements, $enums);
  263. $class_arr = explode(',',$str);
  264. //echo "str $str";
  265. // var_dump ($mch);
  266. // print_r($mch);
  267. // print_r($mch);
  268. //echo "Class: $class[1]<br>";
  269. }
  270. return $class_arr;
  271. }
  272. public static function GetAllParentCheckPoints()
  273. {
  274. return self::Find( 0);
  275. }
  276. public static function GetOrphanChildren()
  277. {
  278. return self::Find( 0);
  279. }
  280. public static function EchoAllCheckPoints()
  281. {
  282. $parents = self::GetAllParentCheckPoints();
  283. foreach ($parents as $parent)
  284. {
  285. $parent->Echo();
  286. }
  287. }
  288. public static function DeleteByTask($taskid)
  289. {
  290. //echo "checkpoint DeleteByTask!!<br>";
  291. $cps = self::Find(0, $taskid);
  292. //echo "task checkpoints len ".sizeof($cps)."<br>";
  293. foreach ($cps as $cp)
  294. {
  295. //echo "CP delete id: ".$cp->id."<br>";
  296. $cp->DeleteFull();
  297. }
  298. }
  299. public function DeleteFull()
  300. {
  301. //echo "CP delete full: ".$this->id."<br>";
  302. $this->DeleteChildren();
  303. $this->Delete();
  304. }
  305. public function Delete()
  306. {
  307. //echo "CP delete: ".$this->id."<br>";
  308. global $link;
  309. mysqli_query($link, "delete from checkpoints where id=".$this->id);
  310. }
  311. public function DeleteChildren()
  312. {
  313. foreach ($this->childrenCheckpoints as $cp)
  314. {
  315. $cp->Delete();
  316. }
  317. }
  318. public function Echo()
  319. {
  320. $children = $this->childrenCheckpoints;
  321. //echo "checkpoint $cpObj->id type $cpObj->typeId";
  322. echo "<br><b>".$this->name." [$this->class]"."</b><br>";
  323. if (sizeof($children) > 0)
  324. {
  325. echo "<select>";
  326. foreach ($children as $child) {
  327. echo "<option>";
  328. $child->Echo();
  329. echo "</option>";
  330. }
  331. echo "</select><br>";
  332. }
  333. }
  334. public static function EchoClasses($classes, $myclass=null, $excl=null)
  335. {
  336. echo "<div style='float: left'>
  337. <select id='classes' data-contentsFlag='noHideContents' style='width: 400px' class='form-input content__main__form__series' name='cptype'>";
  338. $str = '<option value="0"><Выберите класс из списка></option>';
  339. foreach ($classes as $class)
  340. {
  341. // echo "found $cname $class $excl ";
  342. if ($excl && $class == $excl)
  343. continue;
  344. if ($myclass && $class != $myclass)
  345. continue;
  346. $cname = CheckPoint::GetClassName($class);
  347. $str .= "<option value='$class'>$cname</option>";
  348. }
  349. echo "$str</select></div><br>";
  350. }
  351. public static function EchoTypes($tasktype = null)
  352. {
  353. if ($tasktype != null)
  354. $types = CheckPointType::GetCheckPointTypesByTask($tasktype);
  355. else
  356. $types = self::FindAllTypes();
  357. //echo "cp type";
  358. //echo "<br>";
  359. //print_r($types);
  360. echo "<br>";
  361. foreach ($types as $type)
  362. {
  363. $cpt = CheckPointType::CreateFromID($type['cp_type_id']);
  364. echo "<br>- $cpt->name checkpoint type ".$type['cp_type_id'].":<br>";
  365. // $templates = CheckPointType::CheckPointTemplate($type['cp_type_id']);
  366. // foreach ($templates as $template)
  367. // {
  368. // echo "template<br>";
  369. // echo "parent cp type ".$template['parent_cp_type']."<br>";
  370. // echo "cp type ".$template['cp_type']."<br>";
  371. // }
  372. }
  373. }
  374. public static function CreateCheckPointType()
  375. {
  376. //выбор типа
  377. //чекбокс = просто создание типа
  378. //листы = создание типа + заполнение его чекбоксами (создание темплейта)
  379. //добавление чекпойнта в таск через редактирование типа тасков
  380. }
  381. }