123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- <?php
- namespace CheckPoints;
- use CheckPointTypes\CheckPointType;
- class CheckPoint
- {
- public $id;
- public $typeId;
- public $priority;
- public $name;
- public $parentTaskId;
- public $parentCheckpointId;
- public $state;
- public $childrenCheckpoints;
- public $class;
- public $value;
- public $twxJson;
- public $checkPointType;
- public $parentCheckpoint;
- public $measurement_id;
- public $measurement_field;
- public $editable;
- function __construct(){}
- public static function CreateFromID($id, $fillchildren = true)
- {
- global $link;
- $query = mysqli_query($link, "select * from checkpoints where id=$id");
- if ($checkpoint_arr = mysqli_fetch_array($query))
- {
- return CheckPoint::CreateFromArray($checkpoint_arr, $fillchildren);
- }
- return null;
- }
- public static function CreateFromArray($checkpointArr, $fillchildren = true)
- {
- $checkpoint = new CheckPoint();
- $checkpoint->id = $checkpointArr['id'];
- $checkpoint->name = $checkpointArr['text'];
- $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->measurement_id = $checkpointArr['measurement_id'];
- $checkpoint->measurement_field = $checkpointArr['measurement_field'];
- $checkpoint->editable = $checkpointArr['editable'];
- $checkpoint->childrenCheckpoints = array();
- if ($checkpoint->parentCheckpointId == 0) {
- if ($fillchildren)
- $checkpoint->FillChildren();
- }
- else if ($checkpoint->parentCheckpoint == null)
- $checkpoint->parentCheckpoint = CheckPoint::CreateFromID($checkpoint->parentCheckpointId, false);
- $nameFromType = true;
- if ($checkpoint->name)
- $nameFromType = false;
- $checkpoint->SetNameAndClass($nameFromType);
- return $checkpoint;
- }
- public static function CreateFromType(CheckPointType $checkpointType, $parentCheckpointId = null, $parentTaskId = null)
- {
- //TODO брать все что можно из типа
- //TODO сначала парент, потом чайлдов
- global $link;
- $checkpoint = new CheckPoint();
- $checkpoint->checkPointType = $checkpointType;
- $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;
- mysqli_query($link, "insert into checkpoints (type, parent_checkpoint_id, text, parent_task_id) values ($checkpoint->typeId,$checkpoint->parentCheckpointId, '$checkpoint->name', $checkpoint->parentTaskId)");
- $id = mysqli_insert_id($link);
- //$cptChild = $checkpointType->GetChildrenIDs();
- foreach ($checkpointType->childrenCheckpointTypes as $childype)
- {
- $cp = CheckPoint::CreateFromType($childype, $id, $checkpoint->parentTaskId);
- //$cp = CheckPoint::CreateFromType($childype, $id, $checkpoint->parentTaskId);
- }
- // $checkpoint->childrenCheckpoints = array();
- // $checkpoint->FillChildren();
- 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 AddRemark($echo = false, $task = null, $tasktype = null, $cpnotes = "")
- {
- if ($echo)
- echo "<br><br>".$this->id.": AddRemark";
- if ($task == null) {
- $task = \Task::Find($this->parentTaskId);
- }
- if ($task != null)
- {
- if ($tasktype == null)
- $tasktype = \Tasktype::Find($task->type);
- //TODO заменить настройкой исключения записи замечаний
- if (intval($task->type) == 741 || intval($task->type) == 743)
- {
- if ($echo)
- echo $this->id.": task 741";
- return;
- }
- $cp = $this;
- if ($cp != null)
- {
- if ($echo)
- echo $this->id." cp != null";
- $numdefects = 0;
- if ($echo)
- echo $this->id." state $this->state value $this->value";
- if ('1' == $this->state || $this->value)
- {
- $value = "";
- if ($tasktype != null)
- {
- $value .= "[".$tasktype->name;
- $value .= "] ";
- }
- if ($echo)
- echo $this->id." value $this->value";
- $children = $this->childrenCheckpoints;
- if ($echo)
- echo $this->id." children size ".(sizeof($children));
- $childrenvalue = "";
- if (sizeof($children) > 0)
- {
- foreach ($children as $child) {
- $cpt = $child->getCheckPointType();
- if ($cpt->getWriteToTWX() && $child->state == '1') {
- $childrenvalue .= " " . $child->name;
- $numdefects++;
- }
- }
- }
- /**
- * @var $child CheckPoint
- *
- */
- if ($numdefects > 0)
- {
- $value .= $cp->name . ": ";
- if ($this->value)
- $value .= $this->value." ";
- $value .= $childrenvalue;
- if ($cpnotes != "")
- {
- $value .= "$cpnotes";
- }
- // \Doctrine\Common\Util\Debug::dump($task);
- if ($echo) {
- echo "<br><br>value: ";
- var_dump($value);
- }
- $result = $task->AddRemark($value);
- // if ($echo) {
- // echo "<br><br>result: ";
- // var_dump($result);
- // }
- $cp->SetTwx($result);
- return $result;
- // return $value; //debug
- }
- }
- }
- }
- else {
- if ($echo)
- echo $this->id . ": Task NULL";
- }
- return null;
- }
- public function SetTwx($value)
- {
- global $link;
- mysqli_query($link, "update checkpoints set twx = '$value' where id=".$this->id);
- }
- public function WriteValueToMeasurement($measurement, $value, $uuid)
- {
- WriteLog("WriteValueToMeasurement $this->id $this->typeId",$value);
- global $link;
- $fieldNameToWrite = '';
- $valueToWrite = 0;
- if ($this->typeId == 0)
- return;
- if ($this->measurement_id && $this->measurement_field)
- {
- $fieldNameToWrite = $this->measurement_field;
- if ($fieldNameToWrite == 'measurement_comment') {
- $time = time();
- $valueToWrite = "'{\"datetime\":$time,\"author\":\"$uuid\",\"text\":\"$value\"}'";
- }
- else {
- if (!$value && !$this->state)
- return;
- else {
- if ($this->parentCheckpoint) {
- if ($this->parentCheckpoint->typeId == 820 && $this->typeId==293) {
- //отметка о выполнении
- if ($this->state)
- $valueToWrite = $this->state;
- }
- else
- return;
- }
- else
- $valueToWrite = $value;
- }
- }
- }
- else
- return;
- // if ($this->typeId == 819)
- // {
- // //measurement value
- // $fieldNameToWrite = 'measurement_value';
- // $valueToWrite = $value;
- // }
- // else if ($this->typeId == 814)
- // {
- // $fieldNameToWrite = 'measurement_comment';
- // $time = time();
- // $valueToWrite = "'{\"datetime\":$time,\"author\":\"$uuid\",\"text\":\"$value\"}'";
- // }
- // else if ($this->parentCheckpoint)
- // {
- // if ($this->parentCheckpoint->typeId == 820)
- // {
- // //соответствие норме
- // $fieldNameToWrite = 'value_compliance';
- // $valueToWrite = $this->state;
- // }
- // else
- // return;
- // }
- // else
- // return;
- $str = "update asusg_measurements set $fieldNameToWrite = $valueToWrite where id=$this->measurement_id ";
- WriteLog("checkpoint $this->typeId",$str);
- mysqli_query($link, $str);
- }
- public function SetValue($value)
- {
- global $link;
- $state = false;
- if ($value == "False")
- {$value = 0; $state = true; $this->state='0';}
- elseif ($value == "True")
- {$value = 1; $state = true; $this->state='1';}
- else
- $this->value = $value;
- if ($state === false)
- $str = "update checkpoints set value = '$value' where id=".$this->id;
- else
- $str = "update checkpoints set is_set = $value where id=".$this->id;
- WriteLog("checkpoint $this->id type $this->typeId",$str);
- 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);
- if ($child->parentCheckpoint == null)
- $child->parentCheckpoint = $this;
- array_push($this->childrenCheckpoints, $child);
- }
- }
- function SetNameAndClass($nameFromType = true)
- {
- global $link;
- $query = mysqli_query($link, "select name, class from checkpoint_types where id=".$this->typeId);
- if ($res = mysqli_fetch_row($query)) {
- if ($nameFromType)
- $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 = 22 + $lenname+strlen($this->value);
- $classid = $this->GetClassID();
- $buf = pack("vlCCCvllCv*",
- $len,
- $this->id,
- $this->priority,
- $this->state,
- $this->editable,
- $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();
- if (sizeof($types) > 0) {
- //echo "cp type";
- //echo "<br>";
- //print_r($types);
- echo "<div class='rounded'><h5>Добавленные чекпойнты:</h5>";
- echo "<ul>";
- foreach ($types as $type) {
- $cpt = CheckPointType::CreateFromID($type['cp_type_id']);
- echo "<li> $cpt->name [" . $type['cp_type_id'] . "]</li>";
- // $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>";
- // }
- }
- echo "</ul></div>";
- }
- }
- public function getCheckPointType()
- {
- if ($this->checkPointType != null)
- return $this->checkPointType;
- else
- {
- $cpt = CheckPointType::CreateFromID($this->typeId);
- $this->checkPointType = $cpt;
- return $cpt;
- }
- }
- public static function CreateCheckPointType()
- {
- //выбор типа
- //чекбокс = просто создание типа
- //листы = создание типа + заполнение его чекбоксами (создание темплейта)
- //добавление чекпойнта в таск через редактирование типа тасков
- }
- }
|