/**
  * Return manager instance
  *
  * @access protected
  * @param void
  * @return CustomPropertiesByCoType 
  */
 function manager()
 {
     if (!$this->manager instanceof CustomPropertiesByCoType) {
         $this->manager = CustomPropertiesByCoType::instance();
     }
     return $this->manager;
 }
 function get_custom_properties()
 {
     $object_type = array_var($_GET, 'object_type', '');
     if ($object_type != '') {
         $cp = CustomProperties::getAllCustomPropertiesByObjectType($object_type);
         $customProperties = array();
         foreach ($cp as $custom) {
             $prop = array();
             $prop['id'] = $custom->getId();
             $prop['name'] = $custom->getName();
             $prop['object_type'] = $custom->getObjectType();
             $prop['description'] = $custom->getDescription();
             $prop['type'] = $custom->getType();
             $prop['values'] = $custom->getValues();
             $prop['default_value'] = $custom->getDefaultValue();
             $prop['required'] = $custom->getIsRequired();
             $prop['multiple_values'] = $custom->getIsMultipleValues();
             $prop['visible_by_default'] = $custom->getVisibleByDefault();
             $prop['co_types'] = CustomPropertiesByCoType::instance()->getCoTypesIdsForCpCSV($custom->getId());
             $customProperties[] = $prop;
         }
         ajx_current("empty");
         ajx_extra_data(array("custom_properties" => $customProperties));
     }
 }
 /**
  * Return all custom properties that an object type has
  *
  * @param $object_type
  * @return array
  * 
  */
 static function getAllCustomPropertiesByObjectType($object_type, $co_type = null)
 {
     if ($co_type) {
         $cond = array("`object_type_id` = ? AND `id` IN (?)", $object_type, CustomPropertiesByCoType::instance()->getCustomPropertyIdsByCoTypeCSV($co_type));
     } else {
         $cond = array("`object_type_id` = ?", $object_type);
     }
     return self::findAll(array('conditions' => $cond, 'order' => 'property_order asc'));
 }
 /**
  * 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());
         }
     }
 }
 /**
 * This function will return paginated result. Result is an array where first element is 
 * array of returned object and second populated pagination object that can be used for 
 * obtaining and rendering pagination data using various helpers.
 * 
 * Items and pagination array vars are indexed with 0 for items and 1 for pagination
 * because you can't use associative indexing with list() construct
 *
 * @access public
 * @param array $arguments Query argumens (@see find()) Limit and offset are ignored!
 * @param integer $items_per_page Number of items per page
 * @param integer $current_page Current page number
 * @return array
 */
 function paginate($arguments = null, $items_per_page = 10, $current_page = 1) {
   if(isset($this) && instance_of($this, 'CustomPropertiesByCoType')) {
     return parent::paginate($arguments, $items_per_page, $current_page);
   } else {
     return  CustomPropertiesByCoType::instance()->paginate($arguments, $items_per_page, $current_page);
   } // if
 } // paginate
 /**
  * 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());
         }
     }
 }