function edit()
 {
     if (!can_manage_templates(logged_user())) {
         flash_error(lang("no access permissions"));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add');
     $cotemplate = COTemplates::findById(get_id());
     if (!$cotemplate instanceof COTemplate) {
         flash_error(lang('template dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$cotemplate->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $template_data = array_var($_POST, 'template');
     $object_properties = array();
     if (!is_array($template_data)) {
         $template_data = array('name' => $cotemplate->getObjectName(), 'description' => $cotemplate->getDescription());
         // array
         foreach ($cotemplate->getObjects() as $obj) {
             $object_properties[$obj->getObjectId()] = TemplateObjectProperties::getPropertiesByTemplateObject(get_id(), $obj->getObjectId());
         }
     } else {
         $cotemplate->setFromAttributes($template_data);
         try {
             $member_ids = json_decode(array_var($_POST, 'members'));
             DB::beginWork();
             $cotemplate->save();
             $cotemplate->removeObjects();
             $objects = array_var($_POST, 'objects');
             foreach ($objects as $objid) {
                 $object = Objects::findObject($objid);
                 COTemplates::validateObjectContext($object, $member_ids);
                 $oid = $cotemplate->addObject($object);
                 $object_ids[$objid] = $oid;
             }
             TemplateObjectProperties::deletePropertiesByTemplate(get_id());
             $objectPropertyValues = array_var($_POST, 'propValues');
             $propValueParams = array_var($_POST, 'propValueParam');
             $propValueOperation = array_var($_POST, 'propValueOperation');
             $propValueAmount = array_var($_POST, 'propValueAmount');
             $propValueUnit = array_var($_POST, 'propValueUnit');
             if (is_array($objectPropertyValues)) {
                 foreach ($objectPropertyValues as $objInfo => $propertyValues) {
                     foreach ($propertyValues as $property => $value) {
                         $split = explode(":", $objInfo);
                         $object_id = $split[1];
                         $templateObjPropValue = new TemplateObjectProperty();
                         $templateObjPropValue->setTemplateId($cotemplate->getId());
                         $templateObjPropValue->setObjectId($object_ids[$objInfo]);
                         //$templateObjPropValue->setObjectManager($split[0]);
                         $templateObjPropValue->setProperty($property);
                         $propValue = '';
                         if (isset($propValueParams[$objInfo][$property])) {
                             $param = $propValueParams[$objInfo][$property];
                             $operation = $propValueOperation[$objInfo][$property];
                             $amount = $propValueAmount[$objInfo][$property];
                             $unit = $propValueUnit[$objInfo][$property];
                             $propValue = '{' . $param . '}' . $operation . $amount . $unit;
                         } else {
                             if (is_array($value)) {
                                 $propValue = $value[0];
                             } else {
                                 $propValue = $value;
                             }
                         }
                         $templateObjPropValue->setValue($propValue);
                         $templateObjPropValue->save();
                     }
                 }
             }
             TemplateParameters::deleteParametersByTemplate(get_id());
             $parameters = array_var($_POST, 'parameters');
             if (is_array($parameters)) {
                 foreach ($parameters as $parameter) {
                     $newTemplateParameter = new TemplateParameter();
                     $newTemplateParameter->setTemplateId($cotemplate->getId());
                     $newTemplateParameter->setName($parameter['name']);
                     $newTemplateParameter->setType($parameter['type']);
                     $newTemplateParameter->save();
                 }
             }
             $object_controller = new ObjectController();
             $object_controller->add_to_members($cotemplate, $member_ids);
             DB::commit();
             ApplicationLogs::createLog($cotemplate, ApplicationLogs::ACTION_EDIT);
             flash_success(lang("success edit template"));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
     }
     tpl_assign('object_properties', $object_properties);
     tpl_assign('parameters', TemplateParameters::getParametersByTemplate(get_id()));
     tpl_assign('objects', $cotemplate->getObjects());
     tpl_assign('cotemplate', $cotemplate);
     tpl_assign('template_data', $template_data);
 }
 function edit()
 {
     if (!can_manage_templates(logged_user())) {
         flash_error(lang("no access permissions"));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add');
     $cotemplate = COTemplates::findById(get_id());
     if (!$cotemplate instanceof COTemplate) {
         flash_error(lang('template dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$cotemplate->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $template_data = array_var($_POST, 'template');
     $object_properties = array();
     if (!is_array($template_data)) {
         $template_data = array('name' => $cotemplate->getObjectName(), 'description' => $cotemplate->getDescription());
         // array
         foreach ($cotemplate->getObjects() as $obj) {
             $object_properties[$obj->getObjectId()] = TemplateObjectProperties::getPropertiesByTemplateObject(get_id(), $obj->getObjectId());
         }
         //delete old temporaly template tasks
         self::deleteOldTemporalyTemplateObj();
     } else {
         $cotemplate->setFromAttributes($template_data);
         try {
             $member_ids = json_decode(array_var($_POST, 'members'));
             DB::beginWork();
             $tmp_objects = $cotemplate->getObjects();
             $cotemplate->removeObjects();
             $cotemplate->save();
             $objects = array_var($_POST, 'objects');
             foreach ($objects as $objid) {
                 $object = Objects::findObject($objid);
                 $additional_attributes = array();
                 if ($object instanceof ProjectTask) {
                     $add_attr_milestones = array_var($_POST, "milestones");
                     if (is_array($add_attr_milestones)) {
                         $additional_attributes['milestone'] = array_var($add_attr_milestones, $objid);
                     }
                 }
                 $oid = $cotemplate->addObject($object, $additional_attributes);
                 $object_ids[$objid] = $oid;
             }
             TemplateObjectProperties::deletePropertiesByTemplate(get_id());
             $objectPropertyValues = array_var($_POST, 'propValues');
             $propValueParams = array_var($_POST, 'propValueParam');
             $propValueOperation = array_var($_POST, 'propValueOperation');
             $propValueAmount = array_var($_POST, 'propValueAmount');
             $propValueUnit = array_var($_POST, 'propValueUnit');
             if (is_array($objectPropertyValues)) {
                 foreach ($objectPropertyValues as $objInfo => $propertyValues) {
                     foreach ($propertyValues as $property => $value) {
                         if (!isset($object_ids[$objInfo])) {
                             $object_ids[$objInfo] = $objInfo;
                         }
                         $split = explode(":", $objInfo);
                         $templateObjPropValue = new TemplateObjectProperty();
                         $templateObjPropValue->setTemplateId($cotemplate->getId());
                         $templateObjPropValue->setObjectId($object_ids[$objInfo]);
                         $templateObjPropValue->setProperty($property);
                         $propValue = '';
                         if (isset($propValueParams[$objInfo][$property])) {
                             $param = $propValueParams[$objInfo][$property];
                             $operation = array_var($propValueOperation[$objInfo], $property);
                             $amount = array_var($propValueAmount[$objInfo], $property);
                             $unit = array_var($propValueUnit[$objInfo], $property);
                             $propValue = '{' . $param . '}' . $operation . $amount . $unit;
                         } else {
                             if (is_array($value)) {
                                 $propValue = $value[0];
                             } else {
                                 $propValue = $value;
                             }
                         }
                         $templateObjPropValue->setValue($propValue);
                         $templateObjPropValue->save();
                     }
                 }
             }
             TemplateParameters::deleteParametersByTemplate(get_id());
             $parameters = array_var($_POST, 'parameters');
             if (is_array($parameters)) {
                 foreach ($parameters as $parameter) {
                     $newTemplateParameter = new TemplateParameter();
                     $newTemplateParameter->setTemplateId($cotemplate->getId());
                     $newTemplateParameter->setName(rtrim($parameter['name'], " "));
                     $newTemplateParameter->setType($parameter['type']);
                     $newTemplateParameter->setDefaultValue(array_var($parameter, 'default_value'));
                     Hook::fire('template_parameter_default_value', array('parameter' => $parameter), $newTemplateParameter);
                     $newTemplateParameter->save();
                 }
             }
             //delete permanently objs
             foreach ($tmp_objects as $obj) {
                 if (is_null($objects)) {
                     $objects = array();
                 }
                 if (!in_array($obj->getId(), $objects)) {
                     $obj->delete();
                 }
             }
             DB::commit();
             ApplicationLogs::createLog($cotemplate, ApplicationLogs::ACTION_EDIT);
             flash_success(lang("success edit template"));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
     }
     $objects = $this->add_template_object_to_view($cotemplate->getId());
     tpl_assign('object_properties', $object_properties);
     tpl_assign('parameters', TemplateParameters::getParametersByTemplate(get_id()));
     tpl_assign('objects', $objects);
     tpl_assign('cotemplate', $cotemplate);
     tpl_assign('template_data', $template_data);
 }