function core_dim_add_new_contact_to_person_dimension($object)
{
    /* @var $object Contact */
    $person_ot = ObjectTypes::findOne(array("conditions" => "`name` = 'person'"));
    $company_ot = ObjectTypes::findOne(array("conditions" => "`name` = 'company'"));
    $person_dim = Dimensions::findOne(array("conditions" => "`code` = 'feng_persons'"));
    if ($person_ot instanceof ObjectType && $person_dim instanceof Dimension) {
        $oid = $object->isCompany() ? $company_ot->getId() : $person_ot->getId();
        $tmp_mem = Members::findOne(array("conditions" => "`dimension_id` = " . $person_dim->getId() . " AND `object_type_id` = {$oid} AND `object_id` = " . $object->getId()));
        $reload_dimension = true;
        if ($tmp_mem instanceof Member) {
            $member = $tmp_mem;
            $reload_dimension = false;
        } else {
            $member = new Member();
            $member->setName($object->getObjectName());
            $member->setDimensionId($person_dim->getId());
            $parent_member_id = 0;
            $depth = 1;
            if ($object->isCompany()) {
                $member->setObjectTypeId($company_ot->getId());
            } else {
                $member->setObjectTypeId($person_ot->getId());
                if ($object->getCompanyId() > 0) {
                    $pmember = Members::findOne(array('conditions' => '`object_id` = ' . $object->getCompanyId() . ' AND `object_type_id` = ' . $company_ot->getId() . ' AND `dimension_id` = ' . $person_dim->getId()));
                    if ($pmember instanceof Member) {
                        $parent_member_id = $pmember->getId();
                        $depth = $pmember->getDepth() + 1;
                    }
                }
            }
            $member->setParentMemberId($parent_member_id);
            $member->setDepth($depth);
            $member->setObjectId($object->getId());
            $member->save();
        }
        $sql = "INSERT INTO `" . TABLE_PREFIX . "contact_dimension_permissions` (`permission_group_id`, `dimension_id`, `permission_type`)\n\t\t\t\t SELECT `c`.`permission_group_id`, " . $person_dim->getId() . ", 'check'\n\t\t\t\t FROM `" . TABLE_PREFIX . "contacts` `c` \n\t\t\t\t WHERE `c`.`is_company`=0 AND `c`.`user_type`!=0 AND `c`.`disabled`=0 AND `c`.`object_id`=" . $object->getId() . "\n\t\t\t\t ON DUPLICATE KEY UPDATE `dimension_id`=`dimension_id`;";
        DB::execute($sql);
        $sql = "INSERT INTO `" . TABLE_PREFIX . "contact_member_permissions` (`permission_group_id`, `member_id`, `object_type_id`, `can_write`, `can_delete`)\n\t\t\t\t SELECT `c`.`permission_group_id`, " . $member->getId() . ", `ot`.`id`, (`c`.`object_id` = " . $object->getId() . ") as `can_write`, (`c`.`object_id` = " . $object->getId() . ") as `can_delete`\n\t\t\t\t FROM `" . TABLE_PREFIX . "contacts` `c` JOIN `" . TABLE_PREFIX . "object_types` `ot` \n\t\t\t\t WHERE `c`.`is_company`=0 AND `c`.`object_id`=" . $object->getId() . "\n\t\t\t\t \tAND `c`.`user_type`!=0 AND `c`.`disabled`=0\n\t\t\t\t\tAND `ot`.`type` IN ('content_object', 'comment')\n\t\t\t\t ON DUPLICATE KEY UPDATE `member_id`=`member_id`;";
        DB::execute($sql);
        DB::execute("DELETE FROM `" . TABLE_PREFIX . "contact_member_permissions` WHERE `permission_group_id` = 0;");
        // NEW! Add contact to its own member to be searchable
        if (logged_user() instanceof Contact) {
            $ctrl = new ObjectController();
            $ctrl->add_to_members($object, array($member->getId()));
        } else {
            $object->addToMembers(array($member));
            $object->addToSharingTable();
        }
        // add permission to creator
        if ($object->getCreatedBy() instanceof Contact) {
            DB::execute("INSERT INTO `" . TABLE_PREFIX . "contact_member_permissions` (`permission_group_id`, `member_id`, `object_type_id`, `can_write`, `can_delete`)\n\t\t\t\t SELECT " . $object->getCreatedBy()->getPermissionGroupId() . ", " . $member->getId() . ", `ot`.`id`, 1, 1\n\t\t\t\t FROM `" . TABLE_PREFIX . "object_types` `ot` \n\t\t\t\t WHERE `ot`.`type` IN ('content_object', 'comment')\n\t\t\t\t ON DUPLICATE KEY UPDATE `member_id`=`member_id`;");
        }
        if ($reload_dimension) {
            evt_add("reload dimension tree", $member->getDimensionId());
        }
    }
}
function workspaces_after_user_add($object, $ignored)
{
    /* @var $object Contact */
    $workspaces_dim = Dimensions::findOne(array("conditions" => "`code` = 'workspaces'"));
    if ($workspaces_dim instanceof Dimension) {
        $sql = "INSERT INTO `" . TABLE_PREFIX . "contact_dimension_permissions` (`permission_group_id`, `dimension_id`, `permission_type`)\r\n\t\t\t\t SELECT `c`.`permission_group_id`, " . $workspaces_dim->getId() . ", 'check'\r\n\t\t\t\t FROM `" . TABLE_PREFIX . "contacts` `c`\r\n\t\t\t\t WHERE `c`.`is_company`=0 AND `c`.`user_type`!=0 AND `c`.`disabled`=0 AND `c`.`object_id`=" . $object->getId() . "\r\n\t\t\t\t ON DUPLICATE KEY UPDATE `dimension_id`=`dimension_id`;";
        DB::execute($sql);
    }
}