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]
";
}
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 "