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 "

".$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 "

value: "; var_dump($value); } $result = $task->AddRemark($value); // if ($echo) { // echo "

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]
"; } 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!!
"; $cps = self::Find(0, $taskid); //echo "task checkpoints len ".sizeof($cps)."
"; foreach ($cps as $cp) { //echo "CP delete id: ".$cp->id."
"; $cp->DeleteFull(); } } public function DeleteFull() { //echo "CP delete full: ".$this->id."
"; $this->DeleteChildren(); $this->Delete(); } public function Delete() { //echo "CP delete: ".$this->id."
"; 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 "
".$this->name." [$this->class]"."
"; if (sizeof($children) > 0) { echo "
"; } } public static function EchoClasses($classes, $myclass=null, $excl=null) { echo "

"; } 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 "
"; //print_r($types); echo "
Добавленные чекпойнты:
"; echo "
"; } } 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() { //выбор типа //чекбокс = просто создание типа //листы = создание типа + заполнение его чекбоксами (создание темплейта) //добавление чекпойнта в таск через редактирование типа тасков } }