/**
  * List custom properties
  *
  * @access public
  * @param void
  * @return null
  */
 function custom_properties()
 {
     if (!can_manage_configuration(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $object_types = ObjectTypes::instance()->findAll(array("conditions" => "`type` IN ('content_object')  AND `name` <> 'template_task' AND name <> 'template_milestone' AND `name` <> 'file revision'", "order" => "name"));
     $ordered_object_types = array();
     foreach ($object_types as $ot) {
         $ordered_object_types[$ot->getId()] = lang($ot->getName());
     }
     asort($ordered_object_types, SORT_STRING);
     $select_options = array('<option value="" selected>' . lang('select one') . '</option>');
     foreach ($ordered_object_types as $k => $v) {
         $select_options[] = '<option value="' . $k . '">' . $v . '</option>';
     }
     tpl_assign('object_types', $select_options);
     $custom_properties = array_var($_POST, 'custom_properties');
     $obj_type_id = array_var($_POST, 'objectType');
     $delete = 0;
     if (is_array($custom_properties)) {
         try {
             DB::beginWork();
             foreach ($custom_properties as $id => $data) {
                 $new_cp = null;
                 if ($data['id'] != '') {
                     $new_cp = CustomProperties::getCustomProperty($data['id']);
                 }
                 if ($new_cp == null) {
                     $new_cp = new CustomProperty();
                 }
                 if ($data['deleted'] == "1") {
                     $delete = $delete + 1;
                     if (!$new_cp->isNew()) {
                         $new_cp->delete();
                     }
                     continue;
                 }
                 $new_cp->setObjectTypeId($obj_type_id);
                 $new_cp->setName($data['name']);
                 $new_cp->setType($data['type']);
                 $new_cp->setDescription($data['description']);
                 if ($data['type'] == 'list') {
                     $values = array();
                     $list = explode(",", $data['values']);
                     foreach ($list as $l) {
                         $values[] = trim($l);
                     }
                     $value = implode(",", $values);
                     $new_cp->setValues($value);
                 } else {
                     $new_cp->setValues($data['values']);
                 }
                 if ($data['type'] == 'boolean') {
                     $new_cp->setDefaultValue(isset($data['default_value_boolean']));
                 } else {
                     $new_cp->setDefaultValue($data['default_value']);
                 }
                 $new_cp->setIsRequired(isset($data['required']));
                 $new_cp->setIsMultipleValues(isset($data['multiple_values']));
                 $new_cp->setOrder($data['order'] - $delete);
                 $new_cp->setVisibleByDefault(isset($data['visible_by_default']));
                 $new_cp->save();
                 if (is_array(array_var($data, 'applyto'))) {
                     $applyto = array_var($data, 'applyto');
                     foreach ($applyto as $co_type => $value) {
                         if ($value == 'true') {
                             if (!CustomPropertiesByCoType::findById(array('co_type_id' => $co_type, 'cp_id' => $new_cp->getId()))) {
                                 $obj = new CustomPropertyByCoType();
                                 $obj->setCoTypeId($co_type);
                                 $obj->setCpId($new_cp->getId());
                                 $obj->save();
                             }
                         } else {
                             $obj = CustomPropertiesByCoType::findById(array('co_type_id' => $co_type, 'cp_id' => $new_cp->getId()));
                             if ($obj) {
                                 $obj->delete();
                             }
                         }
                     }
                 }
             }
             DB::commit();
             flash_success(lang('custom properties updated'));
             ajx_current('back');
         } catch (Exception $ex) {
             DB::rollback();
             flash_error($ex->getMessage());
         }
     }
 }
 /**
  * List custom properties
  *
  * @access public
  * @param void
  * @return null
  */
 function custom_properties()
 {
     if (!logged_user()->isCompanyAdmin(owner_company())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     tpl_assign('object_types', array('<option value="" selected>' . lang('select one') . '</option>', '<option value="Companies">' . lang('company') . '</option>', '<option value="Contacts">' . lang('contact') . '</option>', '<option value="MailContents">' . lang('email type') . '</option>', '<option value="ProjectEvents">' . lang('events') . '</option>', '<option value="ProjectFiles">' . lang('file') . '</option>', '<option value="ProjectMilestones">' . lang('milestone') . '</option>', '<option value="ProjectMessages">' . lang('message') . '</option>', '<option value="ProjectTasks">' . lang('task') . '</option>', '<option value="Users">' . lang('user') . '</option>', '<option value="ProjectWebPages">' . lang('webpage') . '</option>', '<option value="Projects">' . lang('workspace') . '</option>'));
     $custom_properties = array_var($_POST, 'custom_properties');
     $obj_type = array_var($_POST, 'objectType');
     if (is_array($custom_properties)) {
         try {
             DB::beginWork();
             foreach ($custom_properties as $id => $data) {
                 $new_cp = new CustomProperty();
                 if ($data['id'] != '') {
                     $new_cp = CustomProperties::getCustomProperty($data['id']);
                 }
                 if ($data['deleted'] == "1") {
                     $new_cp->delete();
                     continue;
                 }
                 $new_cp->setObjectType($obj_type);
                 $new_cp->setName($data['name']);
                 $new_cp->setType($data['type']);
                 $new_cp->setDescription($data['description']);
                 if ($data['type'] == 'list') {
                     $values = array();
                     $list = explode(",", $data['values']);
                     foreach ($list as $l) {
                         $values[] = trim($l);
                     }
                     $value = implode(",", $values);
                     $new_cp->setValues($value);
                 } else {
                     $new_cp->setValues($data['values']);
                 }
                 if ($data['type'] == 'boolean') {
                     $new_cp->setDefaultValue(isset($data['default_value_boolean']));
                 } else {
                     $new_cp->setDefaultValue($data['default_value']);
                 }
                 $new_cp->setIsRequired(isset($data['required']));
                 $new_cp->setIsMultipleValues(isset($data['multiple_values']));
                 $new_cp->setOrder($id);
                 $new_cp->setVisibleByDefault(isset($data['visible_by_default']));
                 $new_cp->save();
                 if (is_array(array_var($data, 'applyto'))) {
                     $applyto = array_var($data, 'applyto');
                     foreach ($applyto as $co_type => $value) {
                         if ($value == 'true') {
                             if (!CustomPropertiesByCoType::findById(array('co_type_id' => $co_type, 'cp_id' => $new_cp->getId()))) {
                                 $obj = new CustomPropertyByCoType();
                                 $obj->setCoTypeId($co_type);
                                 $obj->setCpId($new_cp->getId());
                                 $obj->save();
                             }
                         } else {
                             $obj = CustomPropertiesByCoType::findById(array('co_type_id' => $co_type, 'cp_id' => $new_cp->getId()));
                             if ($obj) {
                                 $obj->delete();
                             }
                         }
                     }
                 }
             }
             DB::commit();
             flash_success(lang('custom properties updated'));
             ajx_current('back');
         } catch (Exception $ex) {
             DB::rollback();
             flash_error($ex->getMessage());
         }
     }
 }