Esempio n. 1
0
 /**
  * Get field lang.
  *
  * @access   public
  * @return   array
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 public function getLangs()
 {
     return $this->isMultilanguage() ? Core::getLanguages() : ['und'];
 }
Esempio n. 2
0
                        </ul>
                    <?php 
    }
    ?>
                </div>
            <?php 
}
?>

            <?php 
if ($oForm->hasMultilangField()) {
    ?>
                <div id="form-language-checker">
                    <div id="form-language-checker-container">
                        <?php 
    foreach (\Plethora\Core::getLanguages() as $i => $lang) {
        ?>
                            <?php 
        $sIsActive = NULL;
        ?>

                            <?php 
        if ($i === 0 || in_array($lang, $oForm->getCheckedLanguages())) {
            $sIsActive = 'active';
        } else {
            $sIsActive = 'disabled';
        }
        ?>
                            <div id="form-language-checker-single-num-<?php 
        echo $i;
        ?>
Esempio n. 3
0
 /**
  * Checks if the form contains any errors.
  *
  * @access   public
  * @return   boolean
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 public function isValid()
 {
     if ($this->validationResult !== NULL) {
         return $this->validationResult;
     }
     # if form is submitted
     if ($this->isSubmitted()) {
         # make some pre-validation operations
         foreach ($this->getFields() as $field) {
             /* @var $field Field */
             $whenSubmittedMethod = new \ReflectionMethod(get_class($field), 'whenFormSubmitted');
             $whenSubmittedMethod->setAccessible(TRUE);
             $whenSubmittedMethod->invoke($field);
             $whenSubmittedMethod->setAccessible(FALSE);
             # if particular field isn't required and it's value is empty, remove all rules
             if ($field->isRequired() === FALSE) {
                 foreach ($field->getLangs() as $sLang) {
                     foreach ($field->getValue($sLang) as $i => $value) {
                         if ($value == '' || $value === []) {
                             $this->getValidator()->blockRulesFor($field->getName(), $sLang, $i);
                         }
                     }
                 }
             }
             # if particular field is disabled, remove all rules
             if ($field->isDisabled()) {
                 $this->getValidator()->blockRulesFor($field->getName());
             }
             # remove all rules for multilang field and it's unchecked language
             if ($field->isMultilanguage()) {
                 foreach (Core::getLanguages() as $sLang) {
                     if (!in_array($sLang, $this->getCheckedLanguages())) {
                         $this->getValidator()->blockRulesFor($field->getName(), $sLang);
                     }
                 }
             }
         }
         # CSRF token validation
         if ($this->csrfToken) {
             $aMethodValues = $this->getMethodValue();
             $sTokenFromForm = Helper\Arrays::path($aMethodValues, $this->getName() . '.csrf_token');
             $sTokenFromSession = $this->getFormToken();
             if ($sTokenFromForm !== $sTokenFromSession) {
                 $this->addFormError(__('Bad request token. Please, send the form once again.'));
             }
         }
         # make some operations for all fields before validation
         foreach ($this->getFields() as $field) {
             /* @var $field Field */
             $field->beforeValidation();
         }
         # check amount of field values
         foreach ($this->getFields() as $field) {
             /* @var $field Field */
             foreach ($field->getLangs() as $sLang) {
                 if (in_array($sLang, $this->getCheckedLanguages())) {
                     $iValuesAmount = count($field->getValue($sLang));
                     if ($field->getQuantity() === 0) {
                         if ($field->getQuantityMin() > $iValuesAmount) {
                             $this->getValidator()->addError($field->getName() . '_' . $sLang, __('Insufficient amount of values given for this field (should be :number).', ['number' => $field->getQuantityMin()]));
                         }
                         if ($field->getQuantityMax() != 0 && $field->getQuantityMax() < $iValuesAmount) {
                             $this->getValidator()->addError($field->getName() . '_' . $sLang, __('Too hight amount of values given for this field (should be :number).', ['number' => $field->getQuantityMin()]));
                         }
                     } elseif ($field->getQuantity() != $iValuesAmount) {
                         $this->getValidator()->addError($field->getName() . '_' . $sLang, __('The amount of values for this field should be :number (:amount given).', ['number' => $field->getQuantity(), 'amount' => $iValuesAmount]));
                     }
                 }
             }
         }
         # if form has not been checked earler, do it now
         if (!$this->getValidator()->isChecked()) {
             $this->getValidator()->check();
             $this->refactorErrors();
         }
         # make some operations for all fields which are valid
         foreach ($this->getFields() as $field) {
             /* @var $field Field */
             foreach ($field->getLangs() as $sLang) {
                 if (!$field->hasErrors($sLang)) {
                     $field->afterValidationWhenValid($sLang);
                 }
             }
         }
     }
     # check if form has any errors
     $hasErrors = $this->getValidator()->hasErrors();
     # if form is submitted and doesn't have errors
     if ($this->isSubmitted() && !$hasErrors) {
         foreach ($this->getFields() as $field) {
             /* @var $field Field */
             $field->afterValidation();
         }
     }
     # return value whether the form is valid
     return $this->validationResult = !$hasErrors;
 }
Esempio n. 4
0
 /**
  * Method in which can do some operations before saving to database.
  *
  * @access   protected
  * @param    Form $form
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 protected function beforeSave(Form &$form)
 {
     if ($this->getModel()->hasLocales()) {
         $aDoNotSaveFor = array_diff(Core::getLanguages(), $form->getCheckedLanguages());
         foreach ($aDoNotSaveFor as $sLang) {
             $this->getModel()->removeLocales($sLang);
         }
     }
     if (property_exists($this->getModel(), 'author') && !$this->getModel()->getAuthor() instanceof User) {
         $this->getModel()->setAuthor(User::getLoggedUser());
     }
     if (property_exists($this->getModel(), 'modification_date')) {
         $this->getModel()->updateModificationDate();
     }
 }
Esempio n. 5
0
 /**
  * Create new locales Model.
  *
  * @access   public
  * @param    string $sLang
  * @return   ModelCore\Locales
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 private function createNewLocale($sLang)
 {
     if (in_array($sLang, Core::getLanguages())) {
         $sLocaleClassName = $this->getClass() . '\\Locales';
         $oLocale = new $sLocaleClassName();
         /* @var $oLocale ModelCore\Locales */
         $oLocale->setLanguage($sLang);
         $oLocale->setParent($this);
         $this->locales->add($oLocale);
         unset($sLocaleClassName);
         return $oLocale;
     }
     return NULL;
 }