* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/core/class/fields/starsfield.class.php * \ingroup core * \brief File of class to stars field */ require_once DOL_DOCUMENT_ROOT . '/core/class/fields/commonfield.class.php'; /** * Class to stars field */ class StarsField extends CommonField { /** * @var array List of value deemed as empty (null always deemed as empty) */ public $emptyValues = array('', -1); /** * Return HTML string to put an input search field into a page * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @param string $moreCss Value for css to define style/length of field. * @param string $moreAttrib To add more attributes on html input tag * @return string */ public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '') { global $langs; $size = (int) $fieldInfos->size; $moreCss = $this->getInputCss($fieldInfos, $moreCss); $moreAttrib = trim((string) $moreAttrib); if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib; $value = (int) $value; $htmlName = $keyPrefix . $key . $keySuffix; $options = array(); for ($i = 0; $i <= $size; $i++) { // TODO ajouter la tranduction $options[$i] = $langs->trans("StarsFieldValue", $i); } return self::$form->selectarray($htmlName, $options, $value, 1, 0, 0, $moreAttrib, 0, 0, 0, '', $moreCss); } /** * Return HTML string to put an input field into a page * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @param string $moreCss Value for css to define style/length of field. * @param string $moreAttrib To add more attributes on html input tag * @return string */ public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '') { $size = (int) $fieldInfos->size; $moreCss = $this->getInputCss($fieldInfos, $moreCss); $moreAttrib = trim((string) $moreAttrib); if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib; $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : ''; $value = (int) $value; $htmlName = $keyPrefix . $key . $keySuffix; return self::$form->inputStars($htmlName, $size, $value, $moreCss, $moreAttrib . $autoFocus); } /** * Return HTML string to show a field into a page * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @param string $moreCss Value for css to define style/length of field. * @param string $moreAttrib To add more attributes on html input tag * @return string */ public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '') { $size = (int) $fieldInfos->size; $value = (int) $value; return self::$form->outputStars($size, $value); } /** * Get input CSS * * @param FieldInfos $fieldInfos Properties of the field * @param string $moreCss Value for css to define style/length of field. * @param string $defaultCss Default value for css to define style/length of field. * @return string * @see self::printInputSearchField(), self::printInputField() */ public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '') { return parent::getInputCss($fieldInfos, $moreCss, $defaultCss); } /** * Verify if the field value is valid * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $value Value to check (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @return bool * @see self::printInputField() */ public function verifyFieldValue($fieldInfos, $key, $value) { $fieldInfos->minValue = 0; $fieldInfos->maxValue = (int) $fieldInfos->size; $result = parent::verifyFieldValue($fieldInfos, $key, $value); if ($result && !$this->isEmptyValue($fieldInfos, $value)) { if (!self::$validator->isNumeric($value)) { return false; } $result = true; } return $result; } /** * Verify if the field value from GET/POST is valid * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @return bool * @see self::printInputField() */ public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '') { $htmlName = $keyPrefix . $key . $keySuffix; return $this->verifyFieldValue($fieldInfos, $key, GETPOST($htmlName, 'restricthtml')); } /** * Get field value from GET/POST * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $defaultValue Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @return mixed * @see self::printInputField() */ public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '') { $htmlName = $keyPrefix . $key . $keySuffix; if (GETPOSTISSET($htmlName)) { $value = GETPOSTINT($htmlName); } else { $value = $defaultValue; } return $value; } /** * Get search field value from GET/POST * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $defaultValue Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @param string $keyPrefix Prefix string to add into name and id of field (can be used to avoid duplicate names) * @param string $keySuffix Suffix string to add into name and id of field (can be used to avoid duplicate names) * @return mixed * @see self::printInputSearchField() */ public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '') { $htmlName = $keyPrefix . $key . $keySuffix; if (GETPOSTISSET($htmlName)) { $value = GETPOSTINT($htmlName); } else { $value = $defaultValue; } return $value; } /** * Get sql filter for search field * * @param FieldInfos $fieldInfos Properties of the field * @param string $key Key of field * @param mixed $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value, for array type must be array) * @return string * @see self::printInputSearchField(), self::getPostSearchFieldValue() */ public function sqlFilterSearchField($fieldInfos, $key, $value) { if (!$this->isEmptyValue($fieldInfos, $value)) { $alias = $fieldInfos->sqlAlias ?? 't.'; $value = (int) $value; if ($value >= 0) { return natural_search($alias . ($fieldInfos->nameInTable ?? $key), (string) $value, 1); } } return ''; } }