123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- namespace app\models;
- use Yii;
- use yii\base\Model;
- use app\models\entity\ProjectsLocotech;
- use app\models\entity\CheckpointTypesForTasks;
- use app\models\entity\CheckpointTypeHierarchy;
- use app\models\entity\Tasks;
- use app\models\entity\Tasktypes;
- use app\models\entity\CheckpointTypes;
- use app\models\entity\Checkpoints;
- class CheckPointModel extends Model
- {
- public $id;
- public $typeId;
- public $priority;
- public $name;
- public $parentTaskId;
- public $parentCheckpointId;
- public $state;
- public $childrenCheckpoints;
- public $class;
- public $value;
- function __construct(){}
- public static function CreateFromID($id)
- {
- global $link;
- $query = mysqli_query($link, "select * from checkpoints where id=$id");
- if ($checkpoint_arr = mysqli_fetch_array($query))
- {
- return CheckPoint::CreateFromArray($checkpoint_arr);
- }
- return null;
- }
- public static function CreateFromArray($checkpointArr)
- {
- $checkpoint = new CheckPoint();
- $checkpoint->id = $checkpointArr['id'];
- $checkpoint->name = $checkpointArr['name'];
- $checkpoint->priority = $checkpointArr['priority'];
- $checkpoint->typeId = $checkpointArr['type'];
- $checkpoint->state = $checkpointArr['is_set'];
- $checkpoint->class = $checkpointArr['class'];
- $checkpoint->value = $checkpointArr['value'];
- $checkpoint->parentTaskId = $checkpointArr['parent_task_id'];
- $checkpoint->parentCheckpointId = intval($checkpointArr['parent_checkpoint_id']);
- $checkpoint->childrenCheckpoints = array();
- $checkpoint->FillChildren();
- $checkpoint->SetNameAndClass();
- return $checkpoint;
- }
- // работает
-
- public static function CreateFromType(CheckPointTypeModel $checkpointType, $parentCheckpointId = null, $parentTaskId = null)
- {
- //TODO брать все что можно из типа
- //TODO сначала парент, потом чайлдов
- global $link;
- $checkpoint = new CheckPointModel();
- $checkpoint->name = $checkpointType->name;
- $checkpoint->priority = $checkpointType->priority;
- $checkpoint->typeId = $checkpointType->id;
- $checkpoint->state = '0';
- $checkpoint->class = $checkpointType->class;
- if ($parentCheckpointId != null)
- $checkpoint->parentCheckpointId = intval($parentCheckpointId);
- else
- $checkpoint->parentCheckpointId = 0;
- if ($parentTaskId != null)
- $checkpoint->parentTaskId = intval($parentTaskId);
- else
- $checkpoint->parentTaskId = 0;
-
- $checkpointsEntity = new Checkpoints();
- $checkpointsEntity->type = $checkpoint->typeId;
- $checkpointsEntity->parent_checkpoint_id = $checkpoint->parentCheckpointId;
- $checkpointsEntity->text = $checkpoint->name;
- $checkpointsEntity->parent_task_id = $checkpoint->parentTaskId;
- $checkpointsEntity->save();
-
- foreach ($checkpointType->childrenCheckpointTypes as $childype)
- {
- $cp = CheckPoint::CreateFromType($childype, $id, null);
- }
- return $checkpoint;
- }
- // public static function GetallChildren
- public static function CreateFromScratch($class, $name, $parentTaskTypeId = null)
- {
- global $link;
- $checkpointType = new CheckPointType();
- $checkpointType->name = $name;
- $checkpointType->class = $class;
- $checkpointType->childrenCheckpointTypes = array();
- mysqli_query($link, "insert into checkpoint_types (class, name) values ('$class', '$name')");
- $checkpointType->id = mysqli_insert_id($link);
- if ($parentTaskTypeId != null) {
- mysqli_query($link, "insert into checkpoint_type_hierarchy (parent_cp_type, cp_type) values ($parentTaskTypeId, $checkpointType->id)");
- //mysqli_query($link, "update checkpoint_type_hierarchy set parent_cp_type=$parentTaskTypeId, cp_type=$checkpointType->id");
- }
- return $checkpointType;
- }
- public static function GetClasses()
- {
- return array(0, "DropList", "CheckBox", "MultiSelect", "SerialNumber");
- }
- public function SetValue($value)
- {
- global $link;
- $state = false;
- if ($value == "False"){
- $value = 0;$state = true;}
- elseif ($value == "True")
- {$value = 1;$state = true;}
- if (!$state)
- $str = "update checkpoints set value = '$value' where id=".$this->id;
- else
- $str = "update checkpoints set is_set = $value where id=".$this->id;
- mysqli_query($link, $str);
- }
- function GetClassID()
- {
- switch ($this->class)
- {
- case "DropList": return 1;
- case "CheckBox": return 2;
- case "MultiSelect": return 3;
- case "SerialNumber": return 4;
- }
- }
- function GetClassName($class)
- {
- switch ($class)
- {
- case "DropList": return 'Один из нескольких вариантов';
- case "CheckBox": return 'Варианты';
- case "MultiSelect": return 'Выбрать несколько вариантов';
- case "SerialNumber": return "Ввод значения";
- }
- }
- function FillChildren()
- {
- global $link;
- $query = mysqli_query($link, "select * from checkpoints where parent_checkpoint_id=".$this->id." ORDER BY priority");
- while ($checkarr = mysqli_fetch_array($query))
- {
- $child = CheckPoint::CreateFromArray($checkarr);
- array_push($this->childrenCheckpoints, $child);
- }
- }
- function SetNameAndClass()
- {
- global $link;
- $query = mysqli_query($link, "select name, class from checkpoint_types where id=".$this->typeId);
- if ($res = mysqli_fetch_row($query)) {
- $this->name = $res[0];
- $this->class = $res[1];
- }
- }
- function PackChildrenToBinary()
- {
- $buf = "";
- foreach ($this->childrenCheckpoints as $checkpoint)
- {
- $buf .= $checkpoint->PackToBinary();
- }
- return $buf;
- }
- function PackToBinary()
- {
- $lenname = strlen($this->name);
- $len = 21 + $lenname+strlen($this->value);
- $classid = $this->GetClassID();
- $buf = pack("vlCCvllCv*",
- $len,
- $this->id,
- $this->priority,
- $this->state,
- $this->typeId,
- $this->parentTaskId,
- $this->parentCheckpointId,
- $classid,
- $lenname
- ); //v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
- $buf .= $this->name;
- $buf .= $this->value;
- return $buf;
- }
- public static function Find($parentCheckpointId = "all", $parent_task_id = null, $class = null)
- {
- //$parentCheckpointId > 0: find children
- //$parentCheckpointId == 0: find parents
- //$parentCheckpointId == -1: find all
- global $link;
- $addByTask = '';
- $addParentOnly = '';
- if ($parent_task_id) {
- $addByTask = " where parent_task_id=" . $parent_task_id;
- }
- $parentCheckpointId = intval($parentCheckpointId);
- if ($parentCheckpointId > -1) {
- $parentFind = "parent_checkpoint_id=$parentCheckpointId";
- if ($parentCheckpointId === 0)
- $parentFind .= " or parent_checkpoint_id is null";
- if ($parent_task_id) {
- $add = "and (";
- $parentFind .= ")";
- }
- else
- $add = "where";
- $addParentOnly = " $add $parentFind";
- }
- $str = "select * from checkpoints $addByTask $addParentOnly";
- //echo $str;
- $checkpoints = array(); //массив значений чекпойнтов
- $query = mysqli_query($link, $str);
- while ($checkpoint_arr = mysqli_fetch_array($query))
- {
- $check = CheckPoint::CreateFromArray($checkpoint_arr);
- array_push($checkpoints, $check);
- }
- return $checkpoints;
- }
- public static function FindAllTypes()
- {
- global $link;
- $checkpointTypes = array(); //массив значений чекпойнтов
- $query = mysqli_query($link, "select * from checkpoint_types");
- while ($checkpoint_arr = mysqli_fetch_array($query))
- {
- array_push($checkpointTypes, $checkpoint_arr);
- }
- return $checkpointTypes;
- }
- public static function FindAllByTask($parent_task_id)
- {
- global $link;
- $checkpoints = array(); //массив значений чекпойнтов
- $query = mysqli_query($link, "select * from checkpoints where parent_task_id=".$parent_task_id);
- while ($checkpoint_arr = mysqli_fetch_array($query))
- {
- $check = CheckPoint::CreateFromArray($checkpoint_arr);
- array_push($checkpoints, $check);
- }
- return $checkpoints;
- }
- public static function GetCheckPointClasses()
- {
- global $link;
- $class_arr = array();
- $query = mysqli_query($link, "SHOW COLUMNS FROM checkpoint_types WHERE Field = 'class'" );
- if ($class = mysqli_fetch_array($query))
- {
- $enums = $class[1];
- //array_push($class, $class_arr);
- $mch = preg_match ( "/^enum\(/" , $enums);
- $patterns = array();
- $patterns[0] = '/^enum\(/';
- $patterns[1] = '/\)/';
- $patterns[2] = '/\'/';
- $replacements = array();
- $replacements[0] = '';
- $replacements[1] = '';
- $replacements[2] = '';
- $str = preg_replace($patterns, $replacements, $enums);
- $class_arr = explode(',',$str);
- //echo "str $str";
- // var_dump ($mch);
- // print_r($mch);
- // print_r($mch);
- //echo "Class: $class[1]<br>";
- }
- return $class_arr;
- }
- public static function GetAllParentCheckPoints()
- {
- return self::Find( 0);
- }
- public static function GetOrphanChildren()
- {
- return self::Find( 0);
- }
- public static function EchoAllCheckPoints()
- {
- $parents = self::GetAllParentCheckPoints();
- foreach ($parents as $parent)
- {
- $parent->Echo();
- }
- }
- public static function DeleteByTask($taskid)
- {
- //echo "checkpoint DeleteByTask!!<br>";
- $cps = self::Find(0, $taskid);
- //echo "task checkpoints len ".sizeof($cps)."<br>";
- foreach ($cps as $cp)
- {
- //echo "CP delete id: ".$cp->id."<br>";
- $cp->DeleteFull();
- }
- }
- public function DeleteFull()
- {
- //echo "CP delete full: ".$this->id."<br>";
- $this->DeleteChildren();
- $this->Delete();
- }
- public function Delete()
- {
- //echo "CP delete: ".$this->id."<br>";
- global $link;
- mysqli_query($link, "delete from checkpoints where id=".$this->id);
- }
- public function DeleteChildren()
- {
- foreach ($this->childrenCheckpoints as $cp)
- {
- $cp->Delete();
- }
- }
- public function Echo()
- {
- $children = $this->childrenCheckpoints;
- //echo "checkpoint $cpObj->id type $cpObj->typeId";
- echo "<br><b>".$this->name." [$this->class]"."</b><br>";
- if (sizeof($children) > 0)
- {
- echo "<select>";
- foreach ($children as $child) {
- echo "<option>";
- $child->Echo();
- echo "</option>";
- }
- echo "</select><br>";
- }
- }
- public static function EchoClasses($classes, $myclass=null, $excl=null)
- {
- echo "<div style='float: left'>
- <select id='classes' data-contentsFlag='noHideContents' style='width: 400px' class='form-input content__main__form__series' name='cptype'>";
- $str = '<option value="0"><Выберите класс из списка></option>';
- foreach ($classes as $class)
- {
- // echo "found $cname $class $excl ";
- if ($excl && $class == $excl)
- continue;
- if ($myclass && $class != $myclass)
- continue;
- $cname = CheckPoint::GetClassName($class);
- $str .= "<option value='$class'>$cname</option>";
- }
- echo "$str</select></div><br>";
- }
- public static function EchoTypes($tasktype = null)
- {
- if ($tasktype != null)
- $types = CheckPointType::GetCheckPointTypesByTask($tasktype);
- else
- $types = self::FindAllTypes();
- //echo "cp type";
- //echo "<br>";
- //print_r($types);
- echo "<br>";
- foreach ($types as $type)
- {
- $cpt = CheckPointType::CreateFromID($type['cp_type_id']);
- echo "<br>- $cpt->name checkpoint type ".$type['cp_type_id'].":<br>";
- // $templates = CheckPointType::CheckPointTemplate($type['cp_type_id']);
- // foreach ($templates as $template)
- // {
- // echo "template<br>";
- // echo "parent cp type ".$template['parent_cp_type']."<br>";
- // echo "cp type ".$template['cp_type']."<br>";
- // }
- }
- }
- public static function CreateCheckPointType()
- {
- //выбор типа
- //чекбокс = просто создание типа
- //листы = создание типа + заполнение его чекбоксами (создание темплейта)
- //добавление чекпойнта в таск через редактирование типа тасков
- }
- }
|