Esempio n. 1
0
/**
* Unlink a person from a company @ingroup pages
*/
function personCompaniesDelete()
{
    global $PH;
    $id = getOnePassedId('person', 'people_*');
    $person = Person::getEditableById($id);
    if (!$person) {
        $PH->abortWarning("Could not get object...");
    }
    $company_ids = getPassedIds('company', 'companies_*');
    if (!$company_ids) {
        $PH->abortWarning(__("No companies selected..."));
    }
    $employments = $person->getEmployments();
    $counter = 0;
    $errors = 0;
    foreach ($company_ids as $cid) {
        if (!($company = Company::getEditableById($cid))) {
            $PH->abortWarning("Could not access company by id");
        }
        $assigned_to = false;
        foreach ($employments as $e) {
            if ($e->company == $company->id) {
                $assigned_to = true;
                $e_id = $e->id;
                if ($assigned_to) {
                    $e_remove = Employment::getEditableById($e_id);
                    if (!$e_remove) {
                        $PH->abortWarning("Could not access employment by id");
                    } else {
                        if ($e_remove->delete()) {
                            $counter++;
                        } else {
                            $errors++;
                        }
                    }
                } else {
                    $PH->abortWarning("Company isn't related to this person");
                }
            }
        }
    }
    if ($errors) {
        new FeedbackWarning(sprintf(__("Failed to remove %s companies"), $errors));
    } else {
        new FeedbackMessage(sprintf(__("Removed %s companies"), $counter));
    }
    if (!$PH->showFromPage()) {
        $PH->show('personView', array('person' => $person->id));
    }
}