/** * validate and saves/removes department custom work hours, and return result of current custom work hours * * @param erLhcoreClassModelDepartament $departament * @param erLhcoreClassModelDepartamentCustomWorkHours[] $departamentCustomWorkHours * @return erLhcoreClassModelDepartamentCustomWorkHours[] */ public static function validateDepartmentCustomWorkHours(erLhcoreClassModelDepartament $departament, $departamentCustomWorkHours = array()) { $availableCustomWorkHours = array(); $definition = array('customPeriodId' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodDateFrom' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodDateTo' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodStartHour' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodStartHourMin' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodEndHour' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY), 'customPeriodEndHourMin' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null, FILTER_REQUIRE_ARRAY)); $form = new ezcInputForm(INPUT_POST, $definition); if ($form->hasValidData('customPeriodId') && !empty($form->customPeriodId)) { foreach ($form->customPeriodId as $key => $customPeriodId) { if (!$customPeriodId) { // if id is not defined save new custom departament work hours $newDepartamentCustomWorkHours = new erLhcoreClassModelDepartamentCustomWorkHours(); $newDepartamentCustomWorkHours->setState(array('dep_id' => $departament->id, 'date_from' => strtotime($form->customPeriodDateFrom[$key]), 'date_to' => strtotime($form->customPeriodDateTo[$key]), 'start_hour' => $form->customPeriodStartHour[$key] . ($form->customPeriodStartHourMin[$key] > 0 ? str_pad($form->customPeriodStartHourMin[$key], 2, '0', STR_PAD_LEFT) : '00'), 'end_hour' => $form->customPeriodEndHour[$key] . ($form->customPeriodEndHourMin[$key] > 0 ? str_pad($form->customPeriodEndHourMin[$key], 2, '0', STR_PAD_LEFT) : '00'))); erLhcoreClassDepartament::getSession()->save($newDepartamentCustomWorkHours); $availableCustomWorkHours[$key] = $newDepartamentCustomWorkHours; unset($departamentCustomWorkHours[$customPeriodId]); } elseif ($customPeriodId && !empty($departamentCustomWorkHours) && isset($departamentCustomWorkHours[$customPeriodId])) { // if id isset, unset from provided array $availableCustomWorkHours[$key] = $departamentCustomWorkHours[$customPeriodId]; unset($departamentCustomWorkHours[$customPeriodId]); } } } // if there are left elements, remove them from DB if (!empty($departamentCustomWorkHours)) { foreach ($departamentCustomWorkHours as $departamentCustomWorkHour) { erLhcoreClassDepartament::getSession()->delete($departamentCustomWorkHour); } } return $availableCustomWorkHours; }