public function xSaveQuarterCalendarAction() { $this->request->defineParams(array('quarters' => array('type' => 'array'))); $quarters = $this->getParam('quarters'); //Validate the quarter dates if (empty($quarters) || !is_array($quarters) || count($quarters) != 4) { throw new UnexpectedValueException(sprintf("Four periods should be defined.")); } $normalized = []; $year = gmdate('Y'); $ny = 0; $i = 0; foreach ($quarters as $value) { if (preg_match('/^([\\d]{1,2})\\-([\\d]{1,2})$/', $value, $matches)) { $m = $matches[1]; $d = $matches[2]; $lastDayOfMonth = date('t', strtotime(sprintf("%04d-%02d-01", $year, $m))); if ($m < 1 || $m > 12) { throw new OutOfBoundsException(sprintf("Invalid month number %02d.", $m)); } else { if ($d < 1 || $d > $lastDayOfMonth) { throw new OutOfBoundsException(sprintf("Invalid day (%02d) of month (%02d). Last day of this month is %d", $d, $m, $lastDayOfMonth)); } else { if ($m == 2 && ($d = 29)) { throw new OutOfBoundsException(sprintf("You cannot specify Feb 29 as start date of the quarter.")); } } } $v = sprintf("%02d-%02d", $m, $d); if (in_array($v, $normalized)) { throw new OutOfBoundsException(sprintf("You cannot specify the same day twice (%s)", $v)); } if ($i > 0) { if ($normalized[$i - 1] > $v) { $ny++; } if ($ny > 1) { throw new OutOfBoundsException("Periods should be consistent."); } } $normalized[$i++] = $v; } else { throw new UnexpectedValueException(sprintf("Invalid date [MM-DD] %s", strip_tags($value))); } } //Saving $entity = new SettingEntity(); $entity->id = SettingEntity::ID_BUDGET_DAYS; $entity->value = json_encode($normalized); $entity->save(); if (!SettingEntity::getValue(SettingEntity::ID_QUARTERS_DAYS_CONFIRMED)) { $entity = new SettingEntity(); $entity->id = SettingEntity::ID_QUARTERS_DAYS_CONFIRMED; $entity->value = 1; $entity->save(); } $this->response->data(array('quarter' => $normalized)); $this->response->success('Fiscal calendar has been successfully saved'); }