function add()
 {
     if (!can_manage_templates(logged_user())) {
         flash_error(lang("no access permissions"));
         ajx_current("empty");
         return;
     }
     $template = new COTemplate();
     $template_data = array_var($_POST, 'template');
     if (!is_array($template_data)) {
         $template_data = array('name' => '', 'description' => '');
     } else {
         //No le agrego miembros
         // 			$member_ids = json_decode(array_var($_POST, 'members'));
         // 			$context = active_context();
         // 			$member_ids = array();
         // 			foreach ($context as $selection) {
         // 				if ($selection instanceof Member) $member_ids[] = $selection->getId();
         // 			}
         // 			if(count($selected_members)==0){
         // 				$member_ids=$object->getMemberIds();
         // 			}
         // 			$controller->add_to_members($copy, $selected_members);
         $cotemplate = new COTemplate();
         $cotemplate->setFromAttributes($template_data);
         $object_ids = array();
         try {
             DB::beginWork();
             $cotemplate->save();
             $objects = array_var($_POST, 'objects');
             foreach ($objects as $objid) {
                 $object = Objects::findObject($objid);
                 $oid = $cotemplate->addObject($object);
                 $object_ids[$objid] = $oid;
                 // 					COTemplates::validateObjectContext($object, $member_ids);
             }
             $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();
                     }
                 }
             }
             $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);
             evt_add('reload tab panel', 'tasks-panel');
             DB::commit();
             ApplicationLogs::createLog($cotemplate, ApplicationLogs::ACTION_ADD);
             flash_success(lang("success add template"));
             if (array_var($_POST, "add_to")) {
                 ajx_current("start");
             } else {
                 ajx_current("back");
             }
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
     }
     $objects = array();
     if (array_var($_GET, 'id')) {
         /*	TODO: Feng 2 
         		  	$object = Objects::findObject(array_var($_GET, 'id'));
         			if ($object instanceof ProjectDataObject) {
         				$objects[] = $object;
         				tpl_assign('add_to', true);
         			}
         			*/
     }
     tpl_assign('objects', $objects);
     tpl_assign('cotemplate', $template);
     tpl_assign('template_data', $template_data);
 }
 function add()
 {
     if (!can_manage_templates(logged_user())) {
         flash_error(lang("no access permissions"));
         ajx_current("empty");
         return;
     }
     $template = new COTemplate();
     $template_data = array_var($_POST, 'template');
     if (!is_array($template_data)) {
         $template_data = array('name' => '', 'description' => '');
     } else {
         $cotemplate = new COTemplate();
         $cotemplate->setFromAttributes($template_data);
         $object_ids = array();
         try {
             DB::beginWork();
             $cotemplate->save();
             $objects = array_var($_POST, 'objects');
             foreach ($objects as $objid) {
                 $split = explode(":", $objid);
                 $object = get_object_by_manager_and_id($split[1], $split[0]);
                 $oid = $cotemplate->addObject($object);
                 $object_ids[$objid] = $oid;
             }
             $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();
                     }
                 }
             }
             $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();
                 }
             }
             $wss = Projects::findByCSVIds(array_var($_POST, "ws_ids"));
             WorkspaceTemplates::deleteByTemplate($cotemplate->getId());
             foreach ($wss as $ws) {
                 $obj = new WorkspaceTemplate();
                 $obj->setWorkspaceId($ws->getId());
                 $obj->setTemplateId($cotemplate->getId());
                 $obj->setInludeSubWs(false);
                 $obj->save();
             }
             DB::commit();
             ApplicationLogs::createLog($cotemplate, null, ApplicationLogs::ACTION_ADD);
             flash_success(lang("success add template"));
             if (array_var($_POST, "add_to")) {
                 ajx_current("start");
             } else {
                 ajx_current("back");
             }
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
     }
     $objects = array();
     if (array_var($_GET, 'id')) {
         $object = get_object_by_manager_and_id(array_var($_GET, 'id'), array_var($_GET, 'manager'));
         if ($object instanceof ProjectDataObject) {
             $objects[] = $object;
             tpl_assign('add_to', true);
         }
     }
     tpl_assign('objects', $objects);
     tpl_assign('cotemplate', $template);
     tpl_assign('template_data', $template_data);
 }