function saveMember($member_data, Member $member, $is_new = true)
 {
     try {
         DB::beginWork();
         if (!$is_new) {
             $old_parent = $member->getParentMemberId();
         }
         $member->setFromAttributes($member_data);
         /* @var $member Member */
         $object_type = ObjectTypes::findById($member->getObjectTypeId());
         if (!$object_type instanceof ObjectType) {
             throw new Exception(lang("you must select a valid object type"));
         }
         if ($member->getParentMemberId() == 0) {
             $dot = DimensionObjectTypes::findById(array('dimension_id' => $member->getDimensionId(), 'object_type_id' => $member->getObjectTypeId()));
             if (!$dot->getIsRoot()) {
                 throw new Exception(lang("member cannot be root", lang($object_type->getName())));
             }
             $member->setDepth(1);
         } else {
             $allowedParents = $this->getAssignableParents($member->getDimensionId(), $member->getObjectTypeId());
             if (!$is_new) {
                 $childrenIds = $member->getAllChildrenIds(true);
             }
             $hasValidParent = false;
             if ($member->getId() == $member->getParentMemberId() || !$is_new && in_array($member->getParentMemberId(), $childrenIds)) {
                 throw new Exception(lang("invalid parent member"));
             }
             foreach ($allowedParents as $parent) {
                 if ($parent['id'] == $member->getParentMemberId()) {
                     $hasValidParent = true;
                     break;
                 }
             }
             if (!$hasValidParent) {
                 throw new Exception(lang("invalid parent member"));
             }
             $parent = Members::findById($member->getParentMemberId());
             if ($parent instanceof Member) {
                 $member->setDepth($parent->getDepth() + 1);
             } else {
                 $member->setDepth(1);
             }
         }
         if ($object_type->getType() == 'dimension_object') {
             $handler_class = $object_type->getHandlerClass();
             if ($is_new || $member->getObjectId() == 0) {
                 eval('$dimension_object = ' . $handler_class . '::instance()->newDimensionObject();');
             } else {
                 $dimension_object = Objects::findObject($member->getObjectId());
             }
             if ($dimension_object) {
                 $dimension_object->modifyMemberValidations($member);
                 $dimension_obj_data = array_var($_POST, 'dim_obj');
                 if (!array_var($dimension_obj_data, 'name')) {
                     $dimension_obj_data['name'] = $member->getName();
                 }
                 eval('$fields = ' . $handler_class . '::getPublicColumns();');
                 foreach ($fields as $field) {
                     if (array_var($field, 'type') == DATA_TYPE_DATETIME) {
                         $dimension_obj_data[$field['col']] = getDateValue($dimension_obj_data[$field['col']]);
                     }
                 }
                 $member->save();
                 $dimension_object->setFromAttributes($dimension_obj_data, $member);
                 $dimension_object->save();
                 $member->setObjectId($dimension_object->getId());
                 $member->save();
                 Hook::fire("after_add_dimension_object_member", $member, $null);
             }
         } else {
             $member->save();
         }
         // Other dimensions member restrictions
         $restricted_members = array_var($_POST, 'restricted_members');
         if (is_array($restricted_members)) {
             MemberRestrictions::clearRestrictions($member->getId());
             foreach ($restricted_members as $dim_id => $dim_members) {
                 foreach ($dim_members as $mem_id => $member_restrictions) {
                     $restricted = isset($member_restrictions['restricted']);
                     if ($restricted) {
                         $order_num = array_var($member_restrictions, 'order_num', 0);
                         $member_restriction = new MemberRestriction();
                         $member_restriction->setMemberId($member->getId());
                         $member_restriction->setRestrictedMemberId($mem_id);
                         $member_restriction->setOrder($order_num);
                         $member_restriction->save();
                     }
                 }
             }
         }
         // Save member property members (also check for required associations)
         if (array_var($_POST, 'save_properties')) {
             $required_association_ids = DimensionMemberAssociations::getRequiredAssociatations($member->getDimensionId(), $member->getObjectTypeId(), true);
             $missing_req_association_ids = array_fill_keys($required_association_ids, true);
             // if keeps record change is_active, if not delete record
             $old_properties = MemberPropertyMembers::getAssociatedPropertiesForMember($member->getId());
             foreach ($old_properties as $property) {
                 $association = DimensionMemberAssociations::findById($property->getAssociationId());
                 if (!$association->getKeepsRecord()) {
                     $property->delete();
                 }
             }
             $new_properties = array();
             $associated_members = array_var($_POST, 'associated_members', array());
             foreach ($associated_members as $prop_member_id => $assoc_id) {
                 $active_association = null;
                 if (isset($missing_req_association_ids[$assoc_id])) {
                     $missing_req_association_ids[$assoc_id] = false;
                 }
                 $conditions = "`association_id` = {$assoc_id} AND `member_id` = " . $member->getId() . " AND `is_active` = 1";
                 $active_associations = MemberPropertyMembers::find(array('conditions' => $conditions));
                 if (count($active_associations) > 0) {
                     $active_association = $active_associations[0];
                 }
                 $association = DimensionMemberAssociations::findById($assoc_id);
                 if ($active_association instanceof MemberPropertyMember) {
                     if ($active_association->getPropertyMemberId() != $prop_member_id) {
                         if ($association->getKeepsRecord()) {
                             $active_association->setIsActive(false);
                             $active_association->save();
                         }
                         // save current association
                         $mpm = new MemberPropertyMember();
                         $mpm->setAssociationId($assoc_id);
                         $mpm->setMemberId($member->getId());
                         $mpm->setPropertyMemberId($prop_member_id);
                         $mpm->setIsActive(true);
                         $mpm->save();
                         $new_properties[] = $mpm;
                     }
                 } else {
                     // save current association
                     $mpm = new MemberPropertyMember();
                     $mpm->setAssociationId($assoc_id);
                     $mpm->setMemberId($member->getId());
                     $mpm->setPropertyMemberId($prop_member_id);
                     $mpm->setIsActive(true);
                     $mpm->save();
                     $new_properties[] = $mpm;
                 }
             }
             $missing_names = array();
             $missing_count = 0;
             foreach ($missing_req_association_ids as $assoc => $missing) {
                 $assoc_instance = DimensionMemberAssociations::findById($assoc);
                 if ($assoc_instance instanceof DimensionMemberAssociation) {
                     $assoc_dim = Dimensions::getDimensionById($assoc_instance->getAssociatedDimensionMemberAssociationId());
                     if ($assoc_dim instanceof Dimension) {
                         if (!in_array($assoc_dim->getName(), $missing_names)) {
                             $missing_names[] = $assoc_dim->getName();
                         }
                     }
                 }
                 if ($missing) {
                     $missing_count++;
                 }
             }
             if ($missing_count > 0) {
                 throw new Exception(lang("missing required associations", implode(", ", $missing_names)));
             }
             $args = array($member, $old_properties, $new_properties);
             Hook::fire('edit_member_properties', $args, $ret);
         }
         if ($is_new) {
             // set all permissions for the creator
             $dimension = $member->getDimension();
             $allowed_object_types = array();
             $dim_obj_types = $dimension->getAllowedObjectTypeContents();
             foreach ($dim_obj_types as $dim_obj_type) {
                 // To draw a row for each object type of the dimension
                 if (!in_array($dim_obj_type->getContentObjectTypeId(), $allowed_object_types) && $dim_obj_type->getDimensionObjectTypeId() == $member->getObjectTypeId()) {
                     $allowed_object_types[] = $dim_obj_type->getContentObjectTypeId();
                 }
             }
             $allowed_object_types[] = $object_type->getId();
             foreach ($allowed_object_types as $ot) {
                 $cmp = ContactMemberPermissions::findOne(array('conditions' => 'permission_group_id = ' . logged_user()->getPermissionGroupId() . ' AND member_id = ' . $member->getId() . ' AND object_type_id = ' . $ot));
                 if (!$cmp instanceof ContactMemberPermission) {
                     $cmp = new ContactMemberPermission();
                     $cmp->setPermissionGroupId(logged_user()->getPermissionGroupId());
                     $cmp->setMemberId($member->getId());
                     $cmp->setObjectTypeId($ot);
                 }
                 $cmp->setCanWrite(1);
                 $cmp->setCanDelete(1);
                 $cmp->save();
             }
             // set all permissions for permission groups that has allow all in the dimension
             $permission_groups = ContactDimensionPermissions::findAll(array("conditions" => array("`dimension_id` = ? AND `permission_type` = 'allow all'", $dimension->getId())));
             if (is_array($permission_groups)) {
                 foreach ($permission_groups as $pg) {
                     foreach ($allowed_object_types as $ot) {
                         $cmp = ContactMemberPermissions::findById(array('permission_group_id' => $pg->getPermissionGroupId(), 'member_id' => $member->getId(), 'object_type_id' => $ot));
                         if (!$cmp instanceof ContactMemberPermission) {
                             $cmp = new ContactMemberPermission();
                             $cmp->setPermissionGroupId($pg->getPermissionGroupId());
                             $cmp->setMemberId($member->getId());
                             $cmp->setObjectTypeId($ot);
                         }
                         $cmp->setCanWrite(1);
                         $cmp->setCanDelete(1);
                         $cmp->save();
                     }
                 }
             }
             // Inherit permissions from parent node, if they are not already set
             if ($member->getDepth() && $member->getParentMember()) {
                 $parentNodeId = $member->getParentMember()->getId();
                 $condition = "member_id = {$parentNodeId}";
                 foreach (ContactMemberPermissions::instance()->findAll(array("conditions" => $condition)) as $parentPermission) {
                     /* @var $parentPermission ContactMemberPermission */
                     $g = $parentPermission->getPermissionGroupId();
                     $t = $parentPermission->getObjectTypeId();
                     $w = $parentPermission->getCanWrite();
                     $d = $parentPermission->getCanDelete();
                     $existsCondition = "member_id = " . $member->getId() . " AND permission_group_id= {$g} AND object_type_id = {$t}";
                     if (!ContactMemberPermissions::instance()->count(array("conditions" => $existsCondition))) {
                         $newPermission = new ContactMemberPermission();
                         $newPermission->setPermissionGroupId($g);
                         $newPermission->setObjectTypeId($t);
                         $newPermission->setCanWrite($w);
                         $newPermission->setCanDelete($d);
                         $newPermission->setMemberId($member->getId());
                         $newPermission->save();
                     }
                 }
             }
             // Fill sharing table if is a dimension object (after permission creation);
             if (isset($dimension_object) && $dimension_object instanceof ContentDataObject) {
                 $dimension_object->addToSharingTable();
             }
         } else {
             // if parent changed rebuild object_members for every object in this member
             if ($old_parent != $member->getParentMemberId()) {
                 $sql = "SELECT om.object_id FROM " . TABLE_PREFIX . "object_members om WHERE om.member_id=" . $member->getId();
                 $object_ids = DB::executeAll($sql);
                 if (!is_array($object_ids)) {
                     $object_ids = array();
                 }
                 foreach ($object_ids as $row) {
                     $content_object = Objects::findObject($row['object_id']);
                     if (!$content_object instanceof ContentDataObject) {
                         continue;
                     }
                     $parent_ids = array();
                     if ($old_parent > 0) {
                         $all_parents = Members::findById($old_parent)->getAllParentMembersInHierarchy(true);
                         foreach ($all_parents as $p) {
                             $parent_ids[] = $p->getId();
                         }
                         if (count($parent_ids) > 0) {
                             DB::execute("DELETE FROM " . TABLE_PREFIX . "object_members WHERE object_id=" . $content_object->getId() . " AND member_id IN (" . implode(",", $parent_ids) . ")");
                         }
                     }
                     $content_object->addToMembers(array($member));
                     $content_object->addToSharingTable();
                 }
             }
         }
         DB::commit();
         flash_success(lang('success save member', lang(ObjectTypes::findById($member->getObjectTypeId())->getName()), $member->getName()));
         ajx_current("back");
         // Add od to array on new members
         if ($is_new) {
             $member_data['member_id'] = $member->getId();
         }
         evt_add("after member save", $member_data);
         return $member;
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
         ajx_current("empty");
     }
 }
function core_dim_add_contact_member_associations($contact_member, $member) {
	// one way
	$associations = DimensionMemberAssociations::findAll(array("conditions" => array("`dimension_id` = ? AND `object_type_id` = ? AND `associated_dimension_id` = ? AND `associated_object_type_id` = ?", 
		$member->getDimensionId(), $member->getObjectTypeId(), $contact_member->getDimensionId(), $contact_member->getObjectTypeId())));
	foreach ( $associations as $a ) {
		$mpm = MemberPropertyMembers::findOne(array('conditions' => array('association_id = ? AND member_id = ? AND property_member_id = ?', $a->getId(), $member->getId(), $contact_member->getId())));
		if (!$mpm instanceof MemberPropertyMember) {
			$mpm = new MemberPropertyMember();
			$mpm->setAssociationId($a->getId());		
			$mpm->setMemberId($member->getId());
			$mpm->setPropertyMemberId($contact_member->getId());
			$mpm->setIsActive(1);
			$mpm->save();
		}
	}
	
	// reverse way
	$associations = DimensionMemberAssociations::findAll(array("conditions" => array("`dimension_id` = ? AND `object_type_id` = ? AND `associated_dimension_id` = ? AND `associated_object_type_id` = ?", 
		$contact_member->getDimensionId(), $contact_member->getObjectTypeId(), $member->getDimensionId(), $member->getObjectTypeId())));
	foreach ( $associations as $a ) {
		$mpm = MemberPropertyMembers::findOne(array('conditions' => array('association_id = ? AND property_member_id = ? AND member_id = ?', $a->getId(), $member->getId(), $contact_member->getId())));
		if (!$mpm instanceof MemberPropertyMember) {
			$mpm = new MemberPropertyMember();
			$mpm->setAssociationId($a->getId());		
			$mpm->setMemberId($contact_member->getId());
			$mpm->setPropertyMemberId($member->getId());
			$mpm->setIsActive(1);
			$mpm->save();
		}
	}
}