DBRole::cloneRole PHP Method

cloneRole() public method

public cloneRole ( string $newRoleName, Scalr_Account_User $user, integer $envId ) : integer
$newRoleName string
$user Scalr_Account_User
$envId integer
return integer
    public function cloneRole($newRoleName, $user, $envId)
    {
        $this->db->BeginTrans();
        $accountId = $user->getAccountId();
        try {
            $this->db->Execute("INSERT INTO roles SET\n                name            = ?,\n                origin          = ?,\n                client_id       = ?,\n                env_id          = ?,\n                cat_id          = ?,\n                description     = ?,\n                behaviors       = ?,\n                generation      = ?,\n                os_id           = ?,\n                dtadded         = NOW(),\n                added_by_userid = ?,\n                added_by_email  = ?,\n                is_scalarized   = ?\n            ", array($newRoleName, $accountId ? ROLE_TYPE::CUSTOM : ROLE_TYPE::SHARED, empty($accountId) ? null : intval($accountId), empty($envId) ? null : intval($envId), $this->catId, $this->description, $this->behaviorsRaw, 2, $this->osId, $user->getId(), $user->getEmail(), $this->isScalarized));
            $newRoleId = $this->db->Insert_Id();
            //Set behaviors
            foreach ($this->getBehaviors() as $behavior) {
                $this->db->Execute("INSERT IGNORE INTO role_behaviors SET role_id = ?, behavior = ?", array($newRoleId, $behavior));
            }
            // Set images
            $rsr7 = $this->db->Execute("SELECT * FROM role_images WHERE role_id = ?", array($this->id));
            while ($r7 = $rsr7->FetchRow()) {
                $this->db->Execute("INSERT INTO role_images SET\n                    `role_id` = ?,\n                    `cloud_location` = ?,\n                    `image_id` = ?,\n                    `platform` = ?\n                ", array($newRoleId, $r7['cloud_location'], $r7['image_id'], $r7['platform']));
            }
            $props = $this->db->Execute("SELECT * FROM role_properties WHERE role_id=?", array($this->id));
            while ($p1 = $props->FetchRow()) {
                $this->db->Execute("\n                    INSERT INTO role_properties\n                    SET `role_id` = ?,\n                        `name`\t= ?,\n                        `value`\t= ?\n                    ON DUPLICATE KEY UPDATE\n                        `value` = ?\n                ", array($newRoleId, $p1['name'], $p1['value'], $p1['value']));
            }
            //Set global variables
            $variables = new Scalr_Scripting_GlobalVariables($this->clientId, $this->envId, ScopeInterface::SCOPE_ROLE);
            $variables->setValues($variables->getValues($this->id), $newRoleId);
            //Set scripts
            $rsr8 = $this->db->Execute("SELECT * FROM role_scripts WHERE role_id = ?", array($this->id));
            while ($r8 = $rsr8->FetchRow()) {
                $this->db->Execute("INSERT INTO role_scripts SET\n                    role_id = ?,\n                    event_name = ?,\n                    target = ?,\n                    script_id = ?,\n                    version = ?,\n                    timeout = ?,\n                    issync = ?,\n                    params = ?,\n                    order_index = ?,\n                    script_type = ?,\n                    script_path = ?,\n                    hash = ?\n                ", array($newRoleId, $r8['event_name'], $r8['target'], $r8['script_id'], $r8['version'], $r8['timeout'], $r8['issync'], $r8['params'], $r8['order_index'], $r8['script_type'], $r8['script_path'], CryptoTool::sault(12)));
            }
            //Set environments only for account-scope roles
            if (!empty($accountId) && empty($envId)) {
                $rsr9 = $this->db->Execute("SELECT * FROM role_environments WHERE role_id = ?", array($this->id));
                while ($r9 = $rsr9->FetchRow()) {
                    $this->db->Execute("INSERT INTO role_environments SET\n                    role_id = ?,\n                    env_id = ?\n                ", array($newRoleId, $r9['env_id']));
                }
            }
        } catch (Exception $e) {
            $this->db->RollbackTrans();
            throw $e;
        }
        $this->db->CommitTrans();
        if (!empty($newRoleId)) {
            $newRole = self::loadById($newRoleId);
            $newRole->syncAnalyticsTags();
        }
        return $newRoleId;
    }