Ejemplo n.º 1
0
function inviteColaboratorExe()
{
    if (!is_numeric($_SESSION['userId'])) {
        print "Wrong way";
        exit;
    }
    if (empty($_REQUEST['email'])) {
        print "Email is empty";
        exit;
    }
    $d = new Delegate();
    $loggedUser = $d->userGetById($_SESSION['userId']);
    $diagram = $d->diagramGetById($_REQUEST['diagramId']);
    //see if he has the right to invite collaborators
    $userdiagram = $d->userdiagramGetByIds($_SESSION['userId'], $_REQUEST['diagramId']);
    if (!is_object($userdiagram)) {
        addError("You have no rights to invite users.");
        redirect('../colaborators.php?diagramId=' . $diagram->id);
        exit;
    }
    if ($userdiagram->level != Userdiagram::LEVEL_AUTHOR) {
        addError("No rights to invite people");
        redirect('../colaborators.php?diagramId=' . $diagram->id);
        exit;
    }
    $email = trim($_REQUEST['email']);
    /* Alreay a collaborator?
     * See if email belongs to an existing colaborator (so we can skip)*/
    $collaborators = $d->usersGetAsCollaboratorNative($diagram->id);
    foreach ($collaborators as $collaborator) {
        if ($collaborator->email == $email) {
            addError("This email belongs to an already present collaborator");
            redirect('../colaborators.php?diagramId=' . $diagram->id);
        }
    }
    //add colaborator
    $user = $d->userGetByEmail($email);
    $invitation = new Invitation();
    $invitation->createdDate = now();
    $invitation->diagramId = $diagram->id;
    $invitation->token = uniqid();
    if (is_object($user)) {
        //already in the system
        $invitation->email = $user->email;
    } else {
        //not in the system, invite by email (register first)
        $invitation->email = $email;
    }
    $d->invitationCreate($invitation);
    if (is_object($user)) {
        //already in the system
        //TODO: email
        $body = sprintf("<html>\n                        <head>\n                            <title>Diagramo - Invited to edit diagram</title>\n                        </head>\n                        <body>\n                            Hello, <p/>\n                            %s invited you to edit the diagram: %s. Please access your account for more information.\n                        </body>\n                    </html>", $loggedUser->email, $diagram->title);
    } else {
        //TODO: email
        $url = WEBADDRESS . '/register.php?i=' . $invitation->token;
        $body = sprintf("<html>\n                        <head>\n                            <title>Diagramo - Invited to edit diagram</title>\n                        </head>\n                        <body>\n                            Hello, <p/>\n                            %s invited you to edit the diagram: %s. Please click the link to accept it.\n                            <a href=\"%s\">%s</a>\n                        </body>\n                    </html>", $loggedUser->email, $diagram->title, $url, $url);
    }
    //send needed emails
    if (sendEmail($email, '*****@*****.**', "Invitation", $body)) {
        addMessage("Invitation email sent!");
    } else {
        addError("Invitation email NOT sent!");
    }
    //refirect back to collaborators
    redirect('../colaborators.php?diagramId=' . $_REQUEST['diagramId']);
}