Example #1
0
 /**
  * @param Context $ctx
  */
 public function render($ctx)
 {
     $this->ctx = $ctx;
     $path = $this->getTemplateDirPath() . "/" . $this->getTemplateName();
     global $form, $format;
     $form = $ctx->getForm();
     $format = $ctx->getUIManager()->getFormatter();
     // Make $page and $ui globals, so they can be accessed by the view template.
     global $page, $ui, $lang;
     $page = $this->getPage();
     $ui = $this->ctx->getUIManager();
     $lang = Application::getTranslator()->getLanguage();
     include $path;
 }
 /**
  * (non-PHPdoc)
  * @see Constraint::doValidate()
  */
 public function doValidate($ctx)
 {
     $value = $ctx->getRequest()->getString($this->getName(), '');
     // Validate only if there is a value
     if (!$value) {
         return true;
     }
     $converter = DataConverter::getInstance();
     if (!$converter->parseDate($value)) {
         $msg = sprintf(Application::getTranslator()->_('The field %1$s must be a valid date'), $this->getLabel());
         $this->addFieldError($ctx, $this->getName(), $msg);
         return false;
     }
     return true;
 }
 public function doValidate($ctx)
 {
     $value = $ctx->getRequest()->getString($this->getName(), null);
     if ($value === null) {
         $value = '';
         if (isset($_FILES[$this->getName()]['name'])) {
             $value = $_FILES[$this->getName()]['name'];
         }
     }
     if ($value == '') {
         $msg = sprintf(Application::getTranslator()->_('The field %1$s is required'), $this->getLabel());
         $this->addFieldError($ctx, $this->getName(), $msg);
         return false;
     }
     return true;
 }
 public function doValidate($ctx)
 {
     $formattedValue = $ctx->getRequest()->getString($this->getName(), null);
     // Validate only if there is a value
     if ($formattedValue === null || $formattedValue === '') {
         return true;
     }
     $format = new Formatter($ctx->getUser()->getTimezone(), $ctx->getUser()->getLocale());
     $value = $format->getNumber($formattedValue);
     if ($value === false) {
         $msg = sprintf(Application::getTranslator()->_('The field %1$s must be a valid number'), $this->getLabel());
         $this->addFieldError($ctx, $this->getName(), $msg);
         return false;
     }
     if ($this->minValue) {
         if ($this->minValue->isExclusive()) {
             if ($value <= $this->minValue->getValue()) {
                 $msg = sprintf(Application::getTranslator()->_('The value of the %1$s field must be greater than %2$s'), $this->getLabel(), $this->minValue->getValue());
                 $this->addFieldError($ctx, $this->getName(), $msg);
                 return false;
             }
         } else {
             if ($value < $this->minValue->getValue()) {
                 $msg = sprintf(Application::getTranslator()->_('The value of the %1$s field must be greater or equal to %2$s'), $this->getLabel(), $this->minValue->getValue());
                 $this->addFieldError($ctx, $this->getName(), $msg);
                 return false;
             }
         }
     }
     if ($this->maxValue) {
         if ($this->maxValue->isExclusive()) {
             if ($value >= $this->maxValue->getValue()) {
                 $msg = sprintf(Application::getTranslator()->_('The value of the %1$s field must be smaller than %2$s'), $this->getLabel(), $this->maxValue->getValue());
                 $this->addFieldError($ctx, $this->getName(), $msg);
                 return false;
             }
         } else {
             if ($value > $this->maxValue->getValue()) {
                 $msg = sprintf(Application::getTranslator()->_('The value of the %1$s field must be smaller or equal to %2$s'), $this->getLabel(), $this->maxValue->getValue());
                 $this->addFieldError($ctx, $this->getName(), $msg);
                 return false;
             }
         }
     }
     return true;
 }
Example #5
0
 private function invokeControllerMethod()
 {
     $pathInfo = self::getPathInfo();
     $this->timeLogger->setText($pathInfo);
     $ctx = $this->getContext();
     $router = $this->loadRouter();
     $ctx->setRouter($router);
     $route = $router->match($pathInfo, $_SERVER);
     // If no route found, show a 404
     if (!$route) {
         throw new PageNotFoundException(Application::getTranslator()->_("Invalid URL."));
     }
     list($controllerAlias, $methodName, $lang, $redirectPath) = $this->getRouteResult($route);
     Logger::debug("Route: " . $route->name . " (controller={$controllerAlias}, method={$methodName}, lang={$lang}, redirect={$redirectPath})");
     $controllerClassName = null;
     try {
         $controllerClassName = $this->controllerClassNameFromAlias($controllerAlias);
     } catch (IllegalArgumentException $e) {
         throw new PageNotFoundException($e->getMessage(), 0, $e);
     }
     $ctx->setControllerAlias($controllerAlias);
     if (!class_exists($controllerClassName)) {
         throw new PageNotFoundException("Controller class not found: {$controllerClassName}");
     }
     $controller = new $controllerClassName();
     // Check if access is allowed. Controller will redirect if not.
     // TODO: Show a 403 if no access allowed
     if (!$controller->checkAccess($ctx)) {
         header('HTTP/1.1 403 Forbidden');
         return;
     }
     // If locale is required and set, but does not exist throw 404 error
     if ($controller->isLocaleSupported() && $lang && !in_array($lang, self::$translator->getAvailableLocales())) {
         throw new PageNotFoundException(Application::getTranslator()->_("Invalid URL."));
     }
     $locale = $this->getLocale($ctx, $controller, $lang);
     $supportedLocale = $this->getSupportedLocale($locale);
     /**
      * Support the 'redirect' directive of the route.
      * If the route included a 'redirect' value, we redirect to that path,
      * passing all route values + 'lang'.
      */
     if ($redirectPath) {
         $data = array_merge($route->params, array('lang' => $supportedLocale));
         $url = '/' . $router->generate($redirectPath, $data);
         // Include query string when redirecting
         $qs = $_SERVER['QUERY_STRING'];
         if ($qs) {
             $url .= "?{$qs}";
         }
         $ctx->redirect($url, true);
     }
     I18nUtil::setDefaultLocale($supportedLocale);
     self::$translator->setLocale($supportedLocale);
     header('Content-Language: ' . self::$translator->getLocale());
     // Allow dashes in method name (for SEO purposes). Converts to camelCase.
     $methodName = $this->camelize($methodName);
     if (!method_exists($controller, $methodName)) {
         throw new PageNotFoundException("Missing action method '{$methodName}' in controller {$controllerClassName}");
     }
     $view = null;
     // Invoke the controller's method
     try {
         $view = $controller->{$methodName}($ctx);
     } catch (ForwardViewException $e) {
         // Hanlde 'forwarding': A controller method threw this exception
         // containing a view instead of returning it in a normal way.
         $view = $e->getView();
     }
     if ($view instanceof View) {
         if ($ctx->getUIManager()->getErrorManager()->hasErrors()) {
             $ctx->getForm()->setValues($ctx->getAttributes());
         }
         if (self::$translator) {
             $view->setTranslator(self::$translator);
         }
         $view->init($ctx);
         global $form;
         $view->render($ctx);
     }
 }
Example #6
0
 /**
  * Localization array for the Jquery UI date time picker.
  * @return array
  */
 private function getDatePickerLocalization()
 {
     $daysArr = Zend_Locale_Data::getList(Transaction::getInstance()->getUser()->getLocale(), "days");
     $weekArr = Zend_Locale_Data::getList(Transaction::getInstance()->getUser()->getLocale(), "week");
     $firstDay = $daysArr['format']['narrow'][$weekArr["firstDay"]] - 1;
     $tr = Application::getTranslator();
     // Most i18n values come from dedicated jquery.ui.datepicker-<LANG>.js
     // Make sure to include this file on all pages.
     // The fields defined here are the ones used by the datetimepicker extension.
     $regional = array("closeText" => $tr->_('Done'), "dateFormat" => $tr->_('m/dd/yy'), "firstDay" => $firstDay, "isRTL" => false, "showMonthAfterYear" => false, "yearSuffix" => '');
     if ($this->showTime) {
         $regional["currentText"] = $tr->_('Now');
         $regional["amNames"] = array('AM', 'A');
         $regional["pmNames"] = array('PM', 'P');
         // Important: timeFormat must be compatible with pattern used by Formatter::datetime for each locale
         $regional["timeFormat"] = $tr->_('h:mm TT');
         $regional["timeSuffix"] = '';
         $regional["timeOnlyTitle"] = $tr->_('Choose Time');
         $regional["timeText"] = $tr->_('Time');
         $regional["hourText"] = $tr->_('Hour');
         $regional["minuteText"] = $tr->_('Minute');
         $regional["secondText"] = $tr->_('Second');
         $regional["millisecText"] = $tr->_('Millisecond');
         $regional["timezoneText"] = $tr->_('Time Zone');
     }
     return $regional;
 }
 /**
  * Get the pattern to be used to format and parse date/time.
  * The pattern is locale specific.
  * The returned pattern is according to this specification: http://userguide.icu-project.org/formatparse/datetime
  * 
  * Note:
  * 1. The $locale parameter is here becuase it *should* be used, however
  *    current implementation uses Application::getTranslator(), which is
  *    static so locale is not being use. It should be changed to use the given locale.
  * 2. Calls to the translator should explicitly send the literal pattern so
  *    that it is picked by the translator parser. (poedit).
  * 
  * @param String $locale
  * @param boolean $withDate whether the pattern should include date. Default is true.
  * @param boolean $withTime whether the pattern should include time. Default is false.
  * @param boolean $timeIncludesSeconds whether the time should include seconds. Default is false.
  *        Relevant only if $withTime is true.
  *        Note: Currently ignored if $withDate is true.
  * @return String
  */
 public static function getDatePattern($locale, $withDate = true, $withTime = false, $timeIncludesSeconds = false)
 {
     if (!$withDate && !$withTime) {
         throw new IllegalArgumentException("Date format must contain date or/and time");
     }
     $translator = Application::getTranslator();
     if ($withDate && $withTime) {
         return $translator->_('M/dd/yy h:mm a');
     }
     if ($withDate) {
         return $translator->_('M/dd/yy');
     }
     if ($withTime) {
         return $timeIncludesSeconds ? $translator->_('h:mm:ss a') : $translator->_('h:mm a');
     }
 }
Example #8
0
 /**
  * Return an array containing all the months
  * The key is the month number and the value is the formatted month
  *
  * @param string $format "abbreviated" ("jan.") or "wide" ("janvier").
  * @return array containing the 12 months. January is in index 1.
  */
 public static function getMonthsArray($format)
 {
     $zendMonthArr = Zend_Locale::getTranslationList("months", Application::getTranslator()->getLocale());
     return $zendMonthArr["format"][$format];
 }