Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue n° 30845 - Resources not saved on new event creation #31889

Merged
merged 17 commits into from
Nov 16, 2024
Merged
75 changes: 75 additions & 0 deletions htdocs/comm/action/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@
$object->type_code = GETPOST('actioncode', 'aZ09');
}

$listofresourceid = array();

if (!$error) {
// Initialisation of object actioncomm
$object->priority = GETPOSTISSET("priority") ? GETPOSTINT("priority") : 0;
Expand Down Expand Up @@ -430,6 +432,11 @@
if (!empty($_SESSION['assignedtouser'])) {
$listofuserid = json_decode($_SESSION['assignedtouser'], true);
}

if (!empty($_SESSION['assignedtoresource'])) {
$listofresourceid = json_decode($_SESSION['assignedtoresource'], true);
}

$i = 0;
foreach ($listofuserid as $key => $value) {
if ($i == 0) { // First entry
Expand Down Expand Up @@ -541,6 +548,74 @@

if ($idaction > 0) {
if (!$object->error) {
if (is_array($listofresourceid) && count($listofresourceid)) {
foreach ($listofresourceid as $resource_id => $val) {
$resource_type = 'dolresource';
$busy = 1;//GETPOSTINT('busy');

// Resources association
if (getDolGlobalString('RESOURCE_USED_IN_EVENT_CHECK')) {
$eventDateStart = $object->datep;
$eventDateEnd = $object->datef;
$isFullDayEvent = $object->fulldayevent;
if (empty($eventDateEnd)) {
if ($isFullDayEvent) {
$eventDateStartArr = dol_getdate($eventDateStart);
$eventDateStart = dol_mktime(0, 0, 0, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
$eventDateEnd = dol_mktime(23, 59, 59, $eventDateStartArr['mon'], $eventDateStartArr['mday'], $eventDateStartArr['year']);
}
}

$sql = "SELECT er.rowid, r.ref as r_ref, ac.id as ac_id, ac.label as ac_label";
$sql .= " FROM " . MAIN_DB_PREFIX . "element_resources as er";
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "resource as r ON r.rowid = er.resource_id AND er.resource_type = '" . $db->escape($resource_type) . "'";
$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "actioncomm as ac ON ac.id = er.element_id AND er.element_type = '" . $db->escape($object->element) . "'";
$sql .= " WHERE er.resource_id = " . ((int) $resource_id);
$sql .= " AND er.busy = 1";
$sql .= " AND (";

// event date start between ac.datep and ac.datep2 (if datep2 is null we consider there is no end)
$sql .= " (ac.datep <= '" . $db->idate($eventDateStart) . "' AND (ac.datep2 IS NULL OR ac.datep2 >= '" . $db->idate($eventDateStart) . "'))";
// event date end between ac.datep and ac.datep2
if (!empty($eventDateEnd)) {
$sql .= " OR (ac.datep <= '" . $db->idate($eventDateEnd) . "' AND (ac.datep2 >= '" . $db->idate($eventDateEnd) . "'))";
}
// event date start before ac.datep and event date end after ac.datep2
$sql .= " OR (";
$sql .= "ac.datep >= '" . $db->idate($eventDateStart) . "'";
if (!empty($eventDateEnd)) {
$sql .= " AND (ac.datep2 IS NOT NULL AND ac.datep2 <= '" . $db->idate($eventDateEnd) . "')";
}
$sql .= ")";

$sql .= ")";
$resql = $db->query($sql);
if (!$resql) {
$error++;
$object->error = $db->lasterror();
$object->errors[] = $object->error;
} else {
if ($db->num_rows($resql) > 0) {
// Resource already in use
$error++;
$object->error = $langs->trans('ErrorResourcesAlreadyInUse') . ' : ';
while ($obj = $db->fetch_object($resql)) {
$object->error .= '<br> - ' . $langs->trans('ErrorResourceUseInEvent', $obj->r_ref, $obj->ac_label . ' [' . $obj->ac_id . ']');
}
$object->errors[] = $object->error;
}
$db->free($resql);
}
}

if (!$error) {
$res = $object->add_element_resource($resource_id, $resource_type, $busy, $val['mandatory']);
}
}
}

unset($_SESSION['assignedtoresource']);

// Category association
$categories = GETPOST('categories', 'array');
$object->setCategories($categories);
Expand Down
30 changes: 15 additions & 15 deletions htdocs/public/project/new.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,26 @@ function llxFooterVierge()

$db->begin();

if (!GETPOST("lastname")) {
if (!GETPOST('lastname', 'alpha')) {
$error++;
$errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Lastname"))."<br>\n";
}
if (!GETPOST("firstname")) {
if (!GETPOST('firstname', 'alpha')) {
$error++;
$errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Firstname"))."<br>\n";
}
if (!GETPOST("email")) {
if (!GETPOST('email', 'alpha')) {
$error++;
$errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Email"))."<br>\n";
}
if (!GETPOST("description")) {
if (!GETPOST('description', 'alpha')) {
$error++;
$errmsg .= $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Message"))."<br>\n";
}
if (GETPOST("email") && !isValidEmail(GETPOST("email"))) {
if (GETPOST('email', 'alpha') && !isValidEmail(GETPOST('email', 'alpha'))) {
$error++;
$langs->load("errors");
$errmsg .= $langs->trans("ErrorBadEMail", GETPOST("email"))."<br>\n";
$errmsg .= $langs->trans("ErrorBadEMail", GETPOST('email', 'alpha'))."<br>\n";
}
// Set default opportunity status
$defaultoppstatus = getDolGlobalInt('PROJECT_DEFAULT_OPPORTUNITY_STATUS_FOR_ONLINE_LEAD');
Expand All @@ -222,21 +222,21 @@ function llxFooterVierge()

if (!$error) {
// Search thirdparty and set it if found to the new created project
$result = $thirdparty->fetch(0, '', '', '', '', '', '', '', '', '', GETPOST('email'));
$result = $thirdparty->fetch(0, '', '', '', '', '', '', '', '', '', GETPOST('email', 'alpha'));
if ($result > 0) {
$proj->socid = $thirdparty->id;
} else {
// Create the prospect
if (GETPOST('societe')) {
$thirdparty->name = GETPOST('societe');
$thirdparty->name_alias = dolGetFirstLastname(GETPOST('firstname'), GETPOST('lastname'));
if (GETPOST('societe', 'alpha')) {
$thirdparty->name = GETPOST('societe', 'alpha');
$thirdparty->name_alias = dolGetFirstLastname(GETPOST('firstname', 'alpha'), GETPOST('lastname', 'alpha'));
} else {
$thirdparty->name = dolGetFirstLastname(GETPOST('firstname'), GETPOST('lastname'));
$thirdparty->name = dolGetFirstLastname(GETPOST('firstname', 'alpha'), GETPOST('lastname', 'alpha'));
}
$thirdparty->email = GETPOST('email');
$thirdparty->address = GETPOST('address');
$thirdparty->zip = GETPOST('zip');
$thirdparty->town = GETPOST('town');
$thirdparty->email = GETPOST('email', 'alpha');
$thirdparty->address = GETPOST('address', 'alpha');
$thirdparty->zip = GETPOST('zip', 'int');
$thirdparty->town = GETPOST('town', 'alpha');
$thirdparty->country_id = GETPOSTINT('country_id');
$thirdparty->state_id = GETPOSTINT('state_id');
$thirdparty->client = $thirdparty::PROSPECT;
Expand Down
2 changes: 1 addition & 1 deletion htdocs/resource/class/dolresource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function create(User $user, int $no_trigger = 0)
$sql .= ") VALUES (";
$sql .= getEntity('resource') . ", ";
foreach ($new_resource_values as $value) {
$sql .= " " . ((isset($value) && $value > 0) ? "'" . $this->db->escape($value) . "'" : 'NULL') . ",";
$sql .= " " . (!empty($value) ? "'" . $this->db->escape($value) . "'" : 'NULL') . ",";
}
$sql .= " '" . $this->db->idate($this->date_creation) . "',";
$sql .= " " . (!empty($user->id) ? ((int) $user->id) : "null");
Expand Down
Loading