123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?php
- /* Copyright (C) 2011-2015 Laurent Destailleur <eldy@users.sourceforge.net>
- *
- * 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 <http://www.gnu.org/licenses/>.
- * or see http://www.gnu.org/
- *
- * $elementype must be defined.
- */
- /**
- * \file htdocs/core/actions_extrafields.inc.php
- * \brief Code for actions on extrafields admin pages
- */
- $maxsizestring=255;
- $maxsizeint=10;
- $extrasize=GETPOST('size','int');
- $type=GETPOST('type','alpha');
- $param=GETPOST('param','alpha');;
- if ($type=='double' && strpos($extrasize,',')===false) $extrasize='24,8';
- if ($type=='date') $extrasize='';
- if ($type=='datetime') $extrasize='';
- if ($type=='select') $extrasize='';
- // Add attribute
- if ($action == 'add')
- {
- if ($_POST["button"] != $langs->trans("Cancel"))
- {
- // Check values
- if (! $type)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type"));
- $action = 'create';
- }
- if ($type=='varchar' && $extrasize <= 0)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Size"));
- $action = 'edit';
- }
- if ($type=='varchar' && $extrasize > $maxsizestring)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorSizeTooLongForVarcharType",$maxsizestring);
- $action = 'create';
- }
- if ($type=='int' && $extrasize > $maxsizeint)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorSizeTooLongForIntType",$maxsizeint);
- $action = 'create';
- }
- if ($type=='select' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForSelectType");
- $action = 'create';
- }
- if ($type=='sellist' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForSelectListType");
- $action = 'create';
- }
- if ($type=='checkbox' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForCheckBoxType");
- $action = 'create';
- }
- if ($type=='link' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForLinkType");
- $action = 'create';
- }
- if ($type=='radio' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForRadioType");
- $action = 'create';
- }
- if ((($type=='radio') || ($type=='checkbox')) && $param)
- {
- // Construct array for parameter (value of select list)
- $parameters = $param;
- $parameters_array = explode("\r\n",$parameters);
- foreach($parameters_array as $param_ligne)
- {
- if (!empty($param_ligne)) {
- if (preg_match_all('/,/',$param_ligne,$matches))
- {
- if (count($matches[0])>1) {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
- $action = 'create';
- }
- }
- else
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
- $action = 'create';
- }
- }
- }
- }
- if (! $error)
- {
- // attrname must be alphabetical and lower case only
- if (isset($_POST["attrname"]) && preg_match("/^[a-z0-9-_]+$/",$_POST['attrname']) && !is_numeric($_POST["attrname"]))
- {
- // Construct array for parameter (value of select list)
- $default_value = GETPOST('default_value','alpha');
- $parameters = $param;
- $parameters_array = explode("\r\n",$parameters);
- //In sellist we have only one line and it can have come to do SQL expression
- if ($type=='sellist') {
- foreach($parameters_array as $param_ligne)
- {
- $params['options'] = array($parameters=>null);
- }
- }
- else
- {
- //Esle it's separated key/value and coma list
- foreach($parameters_array as $param_ligne)
- {
- list($key,$value) = explode(',',$param_ligne);
- $params['options'][$key] = $value;
- }
- }
- $result=$extrafields->addExtraField(
- GETPOST('attrname', 'alpha'),
- GETPOST('label', 'alpha'),
- $type,
- GETPOST('pos', 'alpha'),
- $extrasize,
- $elementtype,
- (GETPOST('unique', 'alpha')?1:0),
- (GETPOST('required', 'alpha')?1:0),
- $default_value,
- $params,
- (GETPOST('alwayseditable', 'alpha')?1:0),
- (GETPOST('perms', 'alpha')?GETPOST('perms', 'alpha'):''),
- (GETPOST('list', 'alpha')?1:0),
- (GETPOST('ishidden', 'alpha')?1:0),
- GETPOST('computed_value','alpha')
- );
- if ($result > 0)
- {
- setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
- header("Location: ".$_SERVER["PHP_SELF"]);
- exit;
- }
- else
- {
- $error++;
- $mesg=$extrafields->error;
- setEventMessages($mesg, null, 'errors');
- }
- }
- else
- {
- $error++;
- $langs->load("errors");
- $mesg=$langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters",$langs->transnoentities("AttributeCode"));
- setEventMessages($mesg, null, 'errors');
- $action = 'create';
- }
- }
- else
- {
- setEventMessages($mesg, null, 'errors');
- }
- }
- }
- // Rename field
- if ($action == 'update')
- {
- if ($_POST["button"] != $langs->trans("Cancel"))
- {
- // Check values
- if (! $type)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type"));
- $action = 'edit';
- }
- if ($type=='varchar' && $extrasize <= 0)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Size"));
- $action = 'edit';
- }
- if ($type=='varchar' && $extrasize > $maxsizestring)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorSizeTooLongForVarcharType",$maxsizestring);
- $action = 'edit';
- }
- if ($type=='int' && $extrasize > $maxsizeint)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorSizeTooLongForIntType",$maxsizeint);
- $action = 'edit';
- }
- if ($type=='select' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForSelectType");
- $action = 'edit';
- }
- if ($type=='sellist' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForSelectListType");
- $action = 'edit';
- }
- if ($type=='checkbox' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForCheckBoxType");
- $action = 'edit';
- }
- if ($type=='radio' && !$param)
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorNoValueForRadioType");
- $action = 'edit';
- }
- if ((($type=='radio') || ($type=='checkbox')) && $param)
- {
- // Construct array for parameter (value of select list)
- $parameters = $param;
- $parameters_array = explode("\r\n",$parameters);
- foreach($parameters_array as $param_ligne)
- {
- if (!empty($param_ligne)) {
- if (preg_match_all('/,/',$param_ligne,$matches))
- {
- if (count($matches[0])>1) {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
- $action = 'edit';
- }
- }
- else
- {
- $error++;
- $langs->load("errors");
- $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
- $action = 'edit';
- }
- }
- }
- }
- if (! $error)
- {
- if (isset($_POST["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_POST['attrname']))
- {
- $pos = GETPOST('pos','int');
- // Construct array for parameter (value of select list)
- $parameters = $param;
- $parameters_array = explode("\r\n",$parameters);
- //In sellist we have only one line and it can have come to do SQL expression
- if ($type=='sellist') {
- foreach($parameters_array as $param_ligne)
- {
- $params['options'] = array($parameters=>null);
- }
- }
- else
- {
- //Esle it's separated key/value and coma list
- foreach($parameters_array as $param_ligne)
- {
- list($key,$value) = explode(',',$param_ligne);
- $params['options'][$key] = $value;
- }
- }
- $result=$extrafields->update(
- GETPOST('attrname', 'alpha'),
- GETPOST('label', 'alpha'),
- $type,
- $extrasize,
- $elementtype,
- (GETPOST('unique', 'alpha')?1:0),
- (GETPOST('required', 'alpha')?1:0),
- $pos,
- $params,
- (GETPOST('alwayseditable', 'alpha')?1:0),
- (GETPOST('perms', 'alpha')?GETPOST('perms', 'alpha'):''),
- (GETPOST('list', 'alpha')?1:0),
- (GETPOST('ishidden', 'alpha')?1:0),
- GETPOST('default_value','alpha'),
- GETPOST('computed_value','alpha')
- );
- if ($result > 0)
- {
- setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
- header("Location: ".$_SERVER["PHP_SELF"]);
- exit;
- }
- else
- {
- $error++;
- $mesg=$extrafields->error;
- setEventMessages($mesg, null, 'errors');
- }
- }
- else
- {
- $error++;
- $langs->load("errors");
- $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
- setEventMessages($mesg, null, 'errors');
- }
- }
- else
- {
- setEventMessages($mesg, null, 'errors');
- }
- }
- }
- // Delete attribute
- if ($action == 'delete')
- {
- if(isset($_GET["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_GET["attrname"]))
- {
- $result=$extrafields->delete($_GET["attrname"],$elementtype);
- if ($result >= 0)
- {
- header("Location: ".$_SERVER["PHP_SELF"]);
- exit;
- }
- else $mesg=$extrafields->error;
- }
- else
- {
- $error++;
- $langs->load("errors");
- $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
- }
- }
|