<?php
chdir(dirname(__FILE__));
define("CONSOLE_MODE", true);
define('PUBLIC_FOLDER', 'public');
include "./init.php";
header("Content-type: text/plain");
ini_set('memory_limit', '4G');

ini_set("log_errors", 1);
ini_set("error_log", dirname(__FILE__)."/tmp/php-error.log");

// login with superadmin
$admin = Contacts::findOne(array('conditions' => 'user_type = 1'));
CompanyWebsite::instance()->logUserIn($admin);

Env::useHelper('dimension');
Env::useHelper('format');
Env::useHelper('functions', 'advanced_billing');

function create_pm_members_using_cp(){
    @set_time_limit(0);
    ini_set("memory_limit", "2G");

    // Get contact cp values
    $contact_ids_row = DB::execute('SELECT group_concat(DISTINCT value) as contact_ids
                                            FROM fo_custom_property_values
                                            WHERE custom_property_id=129 AND value>0;');

    $contacts_row =  $contact_ids_row->fetchRow();
    $contact_ids = $contacts_row['contact_ids'];
    
    // Get project members
    $contacts = Contacts::findAll(array(
        "conditions" => "`object_id` IN (".$contact_ids.")"
    ));

    $count_pm_members = 0;
    $pm_contact_to_member_map = array();
    $member_controller = new MemberController();
    foreach($contacts as $contact) {
        $c_name = $contact->getName();
        $member = new Member();
        $member_data = array(
            'name' => $c_name,
            'object_type_id' => 31,
            'dimension_id' => 8,
        );
        $pm_member = $member_controller->saveMember($member_data, $member);

        $count_pm_members++;
        $contact_object_id = $contact->getObjectId();
        $pm_member_id = $pm_member->getId();
        $pm_contact_to_member_map[$contact_object_id] = $pm_member_id;
    }

    echo "Created ".$count_pm_members." project manager members \n";

    $project_members = Members::findAll(array(
        "conditions" => "`dimension_id`=4"
    ));

    $p_pm_assoc = DimensionMemberAssociations::getAssociationByCode('project_project_manager');
    $p_pm_assoc_id = $p_pm_assoc->getId();

    $p_pm_count = 0;

    foreach($project_members as $project_member) {
        if(!($project_member instanceof Member)) {
            continue;
        }
        $project_member_object_id = $project_member->getObjectId();
        $project_member_name = $project_member->getName();
        $project_member_cp = CustomPropertyValues::findOne(array(
            "conditions" => "`custom_property_id` = 129 AND `object_id` = ".$project_member_object_id
        ));
        if($project_member_cp instanceof CustomPropertyValue) {
            $project_member_cp_value = $project_member_cp->getValue();
            if($project_member_cp_value > 0) {
                $pm_member_id = $pm_contact_to_member_map[$project_member_cp_value];
                if($pm_member_id > 0) {
                    $mpm = new MemberPropertyMember();
                    $mpm->setAssociationId($p_pm_assoc_id);
                    $mpm->setMemberId($project_member->getId());
                    $mpm->setPropertyMemberId($pm_member_id);
                    $mpm->setIsActive(true);
                    $mpm->setCreatedById(logged_user()->getId());
                    $mpm->setCreatedOn(DateTimeValueLib::now());
                    $mpm->save();
                    $p_pm_count++;
                }
            }
        }
    }

    echo "Created ".$p_pm_count." project manager member property members \n";

   
}

create_pm_members_using_cp();

echo date("H:i:s")." - Assignment is complete \n";
