From 772661c6ba3203ff3795222d5124060f7e509569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 25 Oct 2020 19:44:01 +0100 Subject: [PATCH 1/3] clean code --- htdocs/categories/class/categorie.class.php | 359 +++++++------------- htdocs/categories/edit.php | 35 +- 2 files changed, 138 insertions(+), 256 deletions(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index d3752ef43d8..0a7654917a5 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -208,6 +208,11 @@ class Categorie extends CommonObject */ public $color; + /** + * @var int Visible + */ + public $visible; + /** * @var int Id of thirdparty when CATEGORY_ASSIGNED_TO_A_CUSTOMER is set */ @@ -312,11 +317,9 @@ class Categorie extends CommonObject $sql = "SELECT rowid, fk_parent, entity, label, description, color, fk_soc, visible, type, ref_ext"; $sql .= ", date_creation, tms, fk_user_creat, fk_user_modif"; $sql .= " FROM ".MAIN_DB_PREFIX."categorie"; - if ($id > 0) - { + if ($id > 0) { $sql .= " WHERE rowid = ".$id; - } elseif (!empty($ref_ext)) - { + } elseif (!empty($ref_ext)) { $sql .= " WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."'"; } else { $sql .= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")"; @@ -325,27 +328,25 @@ class Categorie extends CommonObject dol_syslog(get_class($this)."::fetch", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { - if ($this->db->num_rows($resql) > 0) - { + if ($resql) { + if ($this->db->num_rows($resql) > 0) { $res = $this->db->fetch_array($resql); $this->id = $res['rowid']; //$this->ref = $res['rowid']; - $this->fk_parent = $res['fk_parent']; + $this->fk_parent = (int) $res['fk_parent']; $this->label = $res['label']; $this->description = $res['description']; $this->color = $res['color']; - $this->socid = $res['fk_soc']; - $this->visible = $res['visible']; - $this->type = $res['type']; + $this->socid = (int) $res['fk_soc']; + $this->visible = (int) $res['visible']; + $this->type = (int) $res['type']; $this->ref_ext = $res['ref_ext']; - $this->entity = $res['entity']; + $this->entity = (int) $res['entity']; $this->date_creation = $this->db->jdate($res['date_creation']); $this->date_modification = $this->db->jdate($res['tms']); - $this->user_creation = $res['fk_user_creat']; - $this->user_modification = $res['fk_user_modif']; + $this->user_creation = (int) $res['fk_user_creat']; + $this->user_modification = (int) $res['fk_user_modif']; // Retrieve all extrafield // fetch optionals attributes and labels @@ -397,8 +398,7 @@ class Categorie extends CommonObject if (empty($this->visible)) $this->visible = 0; $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0); - if ($this->already_exists()) - { + if ($this->already_exists()) { $this->error = $langs->trans("ImpossibleAddCat", $this->label); $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel"); dol_syslog($this->error, LOG_WARNING); @@ -412,8 +412,7 @@ class Categorie extends CommonObject $sql .= " label,"; $sql .= " description,"; $sql .= " color,"; - if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) - { + if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { $sql .= "fk_soc,"; } $sql .= " visible,"; @@ -424,54 +423,47 @@ class Categorie extends CommonObject $sql .= " date_creation,"; $sql .= " fk_user_creat"; $sql .= ") VALUES ("; - $sql .= $this->db->escape($this->fk_parent).","; + $sql .= (int) $this->fk_parent.","; $sql .= "'".$this->db->escape($this->label)."',"; $sql .= "'".$this->db->escape($this->description)."',"; $sql .= "'".$this->db->escape($this->color)."',"; - if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) - { + if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { $sql .= ($this->socid != -1 ? $this->socid : 'null').","; } $sql .= "'".$this->db->escape($this->visible)."',"; $sql .= $this->db->escape($type).","; $sql .= (!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : 'null').","; $sql .= (!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : 'null').","; - $sql .= $this->db->escape($conf->entity).","; + $sql .= (int) $conf->entity.","; $sql .= "'".$this->db->idate($now)."', "; $sql .= (int) $user->id; $sql .= ")"; $res = $this->db->query($sql); - if ($res) - { + if ($res) { $id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie"); - if ($id > 0) - { + if ($id > 0) { $this->id = $id; $action = 'create'; // Actions on extra fields - if (!$error) - { + if (!$error) { $result = $this->insertExtraFields(); - if ($result < 0) - { + if ($result < 0) { $error++; } } - if (!$error) - { + if (!$error) { // Call trigger $result = $this->call_trigger('CATEGORY_CREATE', $user); if ($result < 0) { $error++; } // End call triggers } - if (!$error) - { + if (!$error) { $this->db->commit(); return $id; } else { @@ -510,8 +502,7 @@ class Categorie extends CommonObject $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0); $this->visible = ($this->visible != "" ? intval($this->visible) : 0); - if ($this->already_exists()) - { + if ($this->already_exists()) { $this->error = $langs->trans("ImpossibleUpdateCat"); $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel"); return -1; @@ -524,8 +515,7 @@ class Categorie extends CommonObject $sql .= " description = '".$this->db->escape($this->description)."',"; $sql .= " ref_ext = '".$this->db->escape($this->ref_ext)."',"; $sql .= " color = '".$this->db->escape($this->color)."'"; - if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) - { + if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { $sql .= ", fk_soc = ".($this->socid != -1 ? $this->socid : 'null'); } $sql .= ", visible = '".$this->db->escape($this->visible)."'"; @@ -534,22 +524,18 @@ class Categorie extends CommonObject $sql .= " WHERE rowid = ".$this->id; dol_syslog(get_class($this)."::update", LOG_DEBUG); - if ($this->db->query($sql)) - { + if ($this->db->query($sql)) { $action = 'update'; // Actions on extra fields - if (!$error) - { + if (!$error) { $result = $this->insertExtraFields(); - if ($result < 0) - { + if ($result < 0) { $error++; } } - if (!$error) - { + if (!$error) { // Call trigger $result = $this->call_trigger('CATEGORY_MODIFY', $user); if ($result < 0) { $error++; $this->db->rollback(); return -1; } @@ -586,8 +572,7 @@ class Categorie extends CommonObject $this->db->begin(); - if (!$error && !$notrigger) - { + if (!$error && !$notrigger) { // Call trigger $result = $this->call_trigger('CATEGORY_DELETE', $user); if ($result < 0) $error++; @@ -595,14 +580,12 @@ class Categorie extends CommonObject } /* FIX #1317 : Check for child category and move up 1 level*/ - if (!$error) - { + if (!$error) { $sql = "UPDATE ".MAIN_DB_PREFIX."categorie"; $sql .= " SET fk_parent = ".$this->fk_parent; $sql .= " WHERE fk_parent = ".$this->id; - if (!$this->db->query($sql)) - { + if (!$this->db->query($sql)) { $this->error = $this->db->lasterror(); $error++; } @@ -630,18 +613,15 @@ class Categorie extends CommonObject } // Removed extrafields - if (!$error) - { + if (!$error) { $result = $this->deleteExtraFields(); - if ($result < 0) - { + if ($result < 0) { $error++; dol_syslog(get_class($this)."::delete erreur ".$this->error, LOG_ERR); } } - if (!$error) - { + if (!$error) { $this->db->commit(); return 1; } else { @@ -676,29 +656,23 @@ class Categorie extends CommonObject $sql .= " VALUES (".$this->id.", ".$obj->id.")"; dol_syslog(get_class($this).'::add_type', LOG_DEBUG); - if ($this->db->query($sql)) - { - if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) - { + if ($this->db->query($sql)) { + if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) { $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie'; $sql .= " WHERE rowid = ".$this->id; dol_syslog(get_class($this)."::add_type", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { - if ($this->db->num_rows($resql) > 0) - { + if ($resql) { + if ($this->db->num_rows($resql) > 0) { $objparent = $this->db->fetch_object($resql); - if (!empty($objparent->fk_parent)) - { + if (!empty($objparent->fk_parent)) { $cat = new Categorie($this->db); $cat->id = $objparent->fk_parent; if (!$cat->containsObject($type, $obj->id)) { $result = $cat->add_type($obj, $type); - if ($result < 0) - { + if ($result < 0) { $this->error = $cat->error; $error++; } @@ -710,8 +684,7 @@ class Categorie extends CommonObject $this->error = $this->db->lasterror(); } - if ($error) - { + if ($error) { $this->db->rollback(); return -1; } @@ -725,8 +698,7 @@ class Categorie extends CommonObject if ($result < 0) { $error++; } // End call triggers - if (!$error) - { + if (!$error) { $this->db->commit(); return 1; } else { @@ -735,8 +707,7 @@ class Categorie extends CommonObject } } else { $this->db->rollback(); - if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') - { + if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { $this->error = $this->db->lasterrno(); return -3; } else { @@ -778,16 +749,14 @@ class Categorie extends CommonObject $sql .= " AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".$obj->id; dol_syslog(get_class($this).'::del_type', LOG_DEBUG); - if ($this->db->query($sql)) - { + if ($this->db->query($sql)) { // Call trigger $this->context = array('unlinkoff'=>$obj); // Save object we want to link category to into category instance to provide information to trigger $result = $this->call_trigger('CATEGORY_UNLINK', $user); if ($result < 0) { $error++; } // End call triggers - if (!$error) - { + if (!$error) { $this->db->commit(); return 1; } else { @@ -829,8 +798,7 @@ class Categorie extends CommonObject $sql .= " AND c.fk_categorie = ".$this->id; $sql .= " AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = o.rowid"; // Protection for external users - if (($type == 'customer' || $type == 'supplier') && $user->socid > 0) - { + if (($type == 'customer' || $type == 'supplier') && $user->socid > 0) { $sql .= " AND o.rowid = ".$user->socid; } if ($limit > 0 || $offset > 0) $sql .= $this->db->plimit($limit + 1, $offset); @@ -838,10 +806,8 @@ class Categorie extends CommonObject dol_syslog(get_class($this)."::getObjectsInCateg", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { - while ($rec = $this->db->fetch_array($resql)) - { + if ($resql) { + while ($rec = $this->db->fetch_array($resql)) { if ($onlyids) { $objs[] = $rec['fk_'.(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])]; } else { @@ -927,20 +893,17 @@ class Categorie extends CommonObject $offset = 0; $nbtotalofrecords = ''; - if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) - { + if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { $result = $this->db->query($sql); $nbtotalofrecords = $this->db->num_rows($result); - if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0 - { + if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0 $page = 0; $offset = 0; } } if ($limit) { - if ($page < 0) - { + if ($page < 0) { $page = 0; } $offset = $limit * $page; @@ -949,17 +912,14 @@ class Categorie extends CommonObject } $result = $this->db->query($sql); - if ($result) - { + if ($result) { $i = 0; $num = $this->db->num_rows($result); $min = min($num, ($limit <= 0 ? $num : $limit)); - while ($i < $min) - { + while ($i < $min) { $obj = $this->db->fetch_object($result); $category_static = new Categorie($this->db); - if ($category_static->fetch($obj->rowid)) - { + if ($category_static->fetch($obj->rowid)) { $categories[$i]['id'] = $category_static->id; $categories[$i]['fk_parent'] = $category_static->fk_parent; $categories[$i]['label'] = $category_static->label; @@ -1004,11 +964,9 @@ class Categorie extends CommonObject $sql .= " AND entity IN (".getEntity('category').")"; $res = $this->db->query($sql); - if ($res) - { + if ($res) { $cats = array(); - while ($rec = $this->db->fetch_array($res)) - { + while ($rec = $this->db->fetch_array($res)) { $cat = new Categorie($this->db); $cat->fetch($rec['rowid']); $cats[] = $cat; @@ -1039,10 +997,8 @@ class Categorie extends CommonObject dol_syslog(get_class($this)."::load_motherof", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { - while ($obj = $this->db->fetch_object($resql)) - { + if ($resql) { + while ($obj = $this->db->fetch_object($resql)) { $this->motherof[$obj->id_son] = $obj->id_parent; } return 1; @@ -1083,19 +1039,15 @@ class Categorie extends CommonObject return -1; } - if (is_string($markafterid)) - { + if (is_string($markafterid)) { $markafterid = explode(',', $markafterid); - } elseif (is_numeric($markafterid)) - { - if ($markafterid > 0) - { + } elseif (is_numeric($markafterid)) { + if ($markafterid > 0) { $markafterid = array($markafterid); } else { $markafterid = array(); } - } elseif (!is_array($markafterid)) - { + } elseif (!is_array($markafterid)) { $markafterid = array(); } @@ -1115,11 +1067,9 @@ class Categorie extends CommonObject dol_syslog(get_class($this)."::get_full_arbo get category list", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { + if ($resql) { $i = 0; - while ($obj = $this->db->fetch_object($resql)) - { + while ($obj = $this->db->fetch_object($resql)) { $this->cats[$obj->rowid]['rowid'] = $obj->rowid; $this->cats[$obj->rowid]['id'] = $obj->rowid; $this->cats[$obj->rowid]['fk_parent'] = $obj->fk_parent; @@ -1137,15 +1087,13 @@ class Categorie extends CommonObject // We add the fullpath property to each elements of first level (no parent exists) dol_syslog(get_class($this)."::get_full_arbo call to build_path_from_id_categ", LOG_DEBUG); - foreach ($this->cats as $key => $val) - { + foreach ($this->cats as $key => $val) { //print 'key='.$key.'
'."\n"; $this->build_path_from_id_categ($key, 0); // Process a branch from the root category key (this category has no parent) } // Include or exclude leaf including $markafterid from tree - if (count($markafterid) > 0) - { + if (count($markafterid) > 0) { $keyfiltercatid = '('.implode('|', $markafterid).')'; //print "Look to discard category ".$markafterid."\n"; @@ -1153,13 +1101,11 @@ class Categorie extends CommonObject $keyfilter2 = '_'.$keyfiltercatid.'$'; $keyfilter3 = '^'.$keyfiltercatid.'_'; $keyfilter4 = '_'.$keyfiltercatid.'_'; - foreach ($this->cats as $key => $val) - { + foreach ($this->cats as $key => $val) { $test = (preg_match('/'.$keyfilter1.'/', $val['fullpath']) || preg_match('/'.$keyfilter2.'/', $val['fullpath']) || preg_match('/'.$keyfilter3.'/', $val['fullpath']) || preg_match('/'.$keyfilter4.'/', $val['fullpath'])); - if (($test && !$include) || (!$test && $include)) - { + if (($test && !$include) || (!$test && $include)) { unset($this->cats[$key]); } } @@ -1189,8 +1135,7 @@ class Categorie extends CommonObject // phpcs:enable dol_syslog(get_class($this)."::build_path_from_id_categ id_categ=".$id_categ." protection=".$protection, LOG_DEBUG); - if (!empty($this->cats[$id_categ]['fullpath'])) - { + if (!empty($this->cats[$id_categ]['fullpath'])) { // Already defined dol_syslog(get_class($this)."::build_path_from_id_categ fullpath and fulllabel already defined", LOG_WARNING); return; @@ -1204,8 +1149,7 @@ class Categorie extends CommonObject $this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label']; $i = 0; $cursor_categ = $id_categ; //print 'Work for id_categ='.$id_categ.'
'."\n"; - while ((empty($protection) || $i < $protection) && !empty($this->motherof[$cursor_categ])) - { + while ((empty($protection) || $i < $protection) && !empty($this->motherof[$cursor_categ])) { //print '  cursor_categ='.$cursor_categ.' i='.$i.' '.$this->motherof[$cursor_categ].'
'."\n"; $this->cats[$id_categ]['fullpath'] = '_'.$this->motherof[$cursor_categ].$this->cats[$id_categ]['fullpath']; $this->cats[$id_categ]['fulllabel'] = $this->cats[$this->motherof[$cursor_categ]]['label'].' >> '.$this->cats[$id_categ]['fulllabel']; @@ -1231,8 +1175,7 @@ class Categorie extends CommonObject { // phpcs:enable // Display $this->cats - foreach ($this->cats as $key => $val) - { + foreach ($this->cats as $key => $val) { print 'id: '.$this->cats[$key]['id']; print ' label: '.$this->cats[$key]['label']; print ' mother: '.$this->cats[$key]['fk_parent']; @@ -1265,11 +1208,9 @@ class Categorie extends CommonObject $sql .= " AND fk_parent = 0"; $res = $this->db->query($sql); - if ($res) - { + if ($res) { $cats = array(); - while ($rec = $this->db->fetch_array($res)) - { + while ($rec = $this->db->fetch_array($res)) { $cat = new Categorie($this->db); $cat->fetch($rec['rowid']); $cats[$rec['rowid']] = $cat; @@ -1319,18 +1260,15 @@ class Categorie extends CommonObject dol_syslog(get_class($this)."::already_exists", LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { - if ($this->db->num_rows($resql) > 0) // Checking for empty resql - { + if ($resql) { + if ($this->db->num_rows($resql) > 0) { // Checking for empty resql $obj = $this->db->fetch_array($resql); /* If object called create, obj cannot have is id. * If object called update, he mustn't have the same label as an other category for this mother. * So if the result have the same id, update is not for label, and if result have an other one, * update may be for label. */ - if ($obj[0] > 0 && $obj[0] != $this->id) - { + if ($obj[0] > 0 && $obj[0] != $this->id) { dol_syslog(get_class($this)."::already_exists category with name=".$this->label." and parent ".$this->fk_parent." exists: rowid=".$obj[0]." current_id=".$this->id, LOG_DEBUG); return 1; } @@ -1361,24 +1299,19 @@ class Categorie extends CommonObject $ways = array(); $allways = $this->get_all_ways(); // Load array of categories - foreach ($allways as $way) - { + foreach ($allways as $way) { $w = array(); $i = 0; $forced_color = ''; - foreach ($way as $cat) - { + foreach ($way as $cat) { $i++; - if (empty($nocolor)) - { + if (empty($nocolor)) { $forced_color = 'toreplace'; - if ($i == count($way)) - { + if ($i == count($way)) { // Check contrast with background and correct text color $forced_color = 'categtextwhite'; - if ($cat->color) - { + if ($cat->color) { if (colorIsLight($cat->color)) $forced_color = 'categtextblack'; } } @@ -1421,12 +1354,9 @@ class Categorie extends CommonObject $res = $this->db->query($sql); - if ($res) - { - while ($rec = $this->db->fetch_array($res)) - { - if ($rec['fk_parent'] > 0) - { + if ($res) { + while ($rec = $this->db->fetch_array($res)) { + if ($rec['fk_parent'] > 0) { $cat = new Categorie($this->db); $cat->fetch($rec['fk_parent']); $parents[] = $cat; @@ -1452,13 +1382,10 @@ class Categorie extends CommonObject $ways = array(); $parents = $this->get_meres(); - if (!empty($parents)) - { - foreach ($parents as $parent) - { + if (!empty($parents)) { + foreach ($parents as $parent) { $allways = $parent->get_all_ways(); - foreach ($allways as $way) - { + foreach ($allways as $way) { $w = $way; $w[] = $this; $ways[] = $w; @@ -1488,8 +1415,7 @@ class Categorie extends CommonObject if (is_numeric($type)) $type = Categorie::$MAP_ID_TO_CODE[$type]; - if ($type === Categorie::TYPE_BANK_LINE) // TODO Remove this with standard category code - { + if ($type === Categorie::TYPE_BANK_LINE) { // TODO Remove this with standard category code // Load bank groups $sql = "SELECT c.label, c.rowid"; $sql .= " FROM ".MAIN_DB_PREFIX."bank_class as a, ".MAIN_DB_PREFIX."bank_categ as c"; @@ -1497,10 +1423,8 @@ class Categorie extends CommonObject $sql .= " ORDER BY c.label"; $res = $this->db->query($sql); - if ($res) - { - while ($obj = $this->db->fetch_object($res)) - { + if ($res) { + while ($obj = $this->db->fetch_object($res)) { if ($mode == 'id') { $cats[] = $obj->rowid; } elseif ($mode == 'label') { @@ -1523,10 +1447,8 @@ class Categorie extends CommonObject $sql .= " AND c.entity IN (".getEntity('category').")"; $res = $this->db->query($sql); - if ($res) - { - while ($obj = $this->db->fetch_object($res)) - { + if ($res) { + while ($obj = $this->db->fetch_object($res)) { if ($mode == 'id') { $cats[] = $obj->rowid; } elseif ($mode == 'label') { @@ -1579,24 +1501,20 @@ class Categorie extends CommonObject $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; $sql .= " WHERE type = ".$this->MAP_ID[$type]; $sql .= " AND entity IN (".getEntity('category').")"; - if ($nom) - { + if ($nom) { if (!$exact) $nom = '%'.str_replace('*', '%', $nom).'%'; if (!$case) $sql .= " AND label LIKE '".$this->db->escape($nom)."'"; else $sql .= " AND label LIKE BINARY '".$this->db->escape($nom)."'"; } - if ($id) - { + if ($id) { $sql .= " AND rowid = '".$id."'"; } $res = $this->db->query($sql); - if ($res) - { - while ($rec = $this->db->fetch_array($res)) - { + if ($res) { + while ($rec = $this->db->fetch_array($res)) { $cat = new Categorie($this->db); $cat->fetch($rec['rowid']); $cats[] = $cat; @@ -1628,8 +1546,7 @@ class Categorie extends CommonObject // Check contrast with background and correct text color $forced_color = 'categtextwhite'; - if ($this->color) - { + if ($this->color) { if (colorIsLight($this->color)) $forced_color = 'categtextblack'; } @@ -1662,14 +1579,12 @@ class Categorie extends CommonObject $dir = $sdir.'/'.get_exdir($this->id, 2, 0, 0, $this, 'category').$this->id."/"; $dir .= "photos/"; - if (!file_exists($dir)) - { + if (!file_exists($dir)) { dol_mkdir($dir); } if (file_exists($dir)) { - if (is_array($file['name']) && count($file['name']) > 0) - { + if (is_array($file['name']) && count($file['name']) > 0) { $nbfile = count($file['name']); for ($i = 0; $i <= $nbfile; $i++) { $originImage = $dir.$file['name'][$i]; @@ -1714,22 +1629,17 @@ class Categorie extends CommonObject $dirthumb = $dir.'thumbs/'; - if (file_exists($dir)) - { + if (file_exists($dir)) { $handle = opendir($dir); - if (is_resource($handle)) - { - while (($file = readdir($handle)) !== false) - { - if (dol_is_file($dir.$file) && preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) - { + if (is_resource($handle)) { + while (($file = readdir($handle)) !== false) { + if (dol_is_file($dir.$file) && preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) { $nbphoto++; $photo = $file; // On determine nom du fichier vignette $photo_vignette = ''; - if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) - { + if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) { $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0]; } @@ -1773,11 +1683,9 @@ class Categorie extends CommonObject dol_delete_file($file, 1); // Si elle existe, on efface la vignette - if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $filename, $regs)) - { + if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $filename, $regs)) { $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $filename).'_small'.$regs[0]; - if (file_exists($dirthumb.$photo_vignette)) - { + if (file_exists($dirthumb.$photo_vignette)) { dol_delete_file($dirthumb.$photo_vignette, 1); } } @@ -1812,8 +1720,7 @@ class Categorie extends CommonObject $langs_available = $langs->get_available_languages(); $current_lang = $langs->getDefaultLang(); - foreach ($langs_available as $key => $value) - { + foreach ($langs_available as $key => $value) { $sql = "SELECT rowid"; $sql .= " FROM ".MAIN_DB_PREFIX."categorie_lang"; $sql .= " WHERE fk_category=".$this->id; @@ -1821,10 +1728,8 @@ class Categorie extends CommonObject $result = $this->db->query($sql); - if ($key == $current_lang) - { - if ($this->db->num_rows($result)) // si aucune ligne dans la base - { + if ($key == $current_lang) { + if ($this->db->num_rows($result)) { // si aucune ligne dans la base $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang"; $sql2 .= " SET label='".$this->db->escape($this->label)."',"; $sql2 .= " description='".$this->db->escape($this->description)."'"; @@ -1835,15 +1740,12 @@ class Categorie extends CommonObject $sql2 .= "','".$this->db->escape($this->multilangs["$key"]["description"])."')"; } dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG); - if (!$this->db->query($sql2)) - { + if (!$this->db->query($sql2)) { $this->error = $this->db->lasterror(); return -1; } - } elseif (isset($this->multilangs["$key"])) - { - if ($this->db->num_rows($result)) // si aucune ligne dans la base - { + } elseif (isset($this->multilangs["$key"])) { + if ($this->db->num_rows($result)) { // si aucune ligne dans la base $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang"; $sql2 .= " SET label='".$this->db->escape($this->multilangs["$key"]["label"])."',"; $sql2 .= " description='".$this->db->escape($this->multilangs["$key"]["description"])."'"; @@ -1857,8 +1759,7 @@ class Categorie extends CommonObject // on ne sauvegarde pas des champs vides if ($this->multilangs["$key"]["label"] || $this->multilangs["$key"]["description"] || $this->multilangs["$key"]["note"]) dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG); - if (!$this->db->query($sql2)) - { + if (!$this->db->query($sql2)) { $this->error = $this->db->lasterror(); return -1; } @@ -1892,13 +1793,10 @@ class Categorie extends CommonObject $sql .= " WHERE fk_category=".$this->id; $result = $this->db->query($sql); - if ($result) - { - while ($obj = $this->db->fetch_object($result)) - { + if ($result) { + while ($obj = $this->db->fetch_object($result)) { //print 'lang='.$obj->lang.' current='.$current_lang.'
'; - if ($obj->lang == $current_lang) // si on a les traduct. dans la langue courante on les charge en infos principales. - { + if ($obj->lang == $current_lang) { // si on a les traduct. dans la langue courante on les charge en infos principales. $this->label = $obj->label; $this->description = $obj->description; } @@ -1988,26 +1886,21 @@ class Categorie extends CommonObject { if ($type == 'bank_account') $type = 'account'; - if (empty($searchList) && !is_array($searchList)) - { + if (empty($searchList) && !is_array($searchList)) { return ""; } - foreach ($searchList as $searchCategory) - { - if (intval($searchCategory) == -2) - { + foreach ($searchList as $searchCategory) { + if (intval($searchCategory) == -2) { $searchCategorySqlList[] = " cp.fk_categorie IS NULL"; - } elseif (intval($searchCategory) > 0) - { + } elseif (intval($searchCategory) > 0) { $searchCategorySqlList[] = " ".$rowIdName ." IN (SELECT fk_".$type." FROM ".MAIN_DB_PREFIX."categorie_".$type ." WHERE fk_categorie = ".$searchCategory.")"; } } - if (!empty($searchCategorySqlList)) - { + if (!empty($searchCategorySqlList)) { return " AND (".implode(' AND ', $searchCategorySqlList).")"; } else { return ""; diff --git a/htdocs/categories/edit.php b/htdocs/categories/edit.php index 8c05c313987..9f7aebf7fec 100644 --- a/htdocs/categories/edit.php +++ b/htdocs/categories/edit.php @@ -34,7 +34,7 @@ $langs->load("categories"); $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alphanohtml'); -$type = GETPOST('type', 'alphanohtml'); +$type = (int) GETPOST('type', 'int'); $action = (GETPOST('action', 'aZ09') ?GETPOST('action', 'aZ09') : 'edit'); $confirm = GETPOST('confirm'); $cancel = GETPOST('cancel', 'alpha'); @@ -43,11 +43,10 @@ $socid = GETPOST('socid', 'int'); $label = GETPOST('label', 'alphanohtml'); $description = GETPOST('description', 'restricthtml'); $color = preg_replace('/[^0-9a-f#]/i', '', GETPOST('color', 'alphanohtml')); -$visible = GETPOST('visible'); -$parent = GETPOST('parent'); +$visible = (int) GETPOST('visible', 'int'); +$parent = (int) GETPOST('parent', 'int'); -if ($id == "") -{ +if ($id == "") { dol_print_error('', 'Missing parameter id'); exit(); } @@ -56,8 +55,7 @@ if ($id == "") $result = restrictedArea($user, 'categorie', $id, '&category'); $object = new Categorie($db); -if ($id > 0) -{ +if ($id > 0) { $result = $object->fetch($id); } @@ -72,40 +70,32 @@ $hookmanager->initHooks(array('categorycard')); * Actions */ -if ($cancel) -{ +if ($cancel) { header('Location: '.DOL_URL_ROOT.'/categories/viewcat.php?id='.$object->id.'&type='.$type); exit; } // Action mise a jour d'une categorie -if ($action == 'update' && $user->rights->categorie->creer) -{ +if ($action == 'update' && $user->rights->categorie->creer) { $object->oldcopy = dol_clone($object); $object->label = $label; $object->description = dol_htmlcleanlastbr($description); $object->color = $color; $object->socid = ($socid ? $socid : 'null'); $object->visible = $visible; - - if ($parent != "-1") - $object->fk_parent = $parent; - else $object->fk_parent = ""; + $object->fk_parent = $parent != -1 ? $parent : 0; - if (empty($object->label)) - { + if (empty($object->label)) { $error++; $action = 'edit'; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors'); } - if (!$error && empty($object->error)) - { + if (!$error && empty($object->error)) { $ret = $extrafields->setOptionalsFromPost(null, $object); if ($ret < 0) $error++; - if (!$error && $object->update($user) > 0) - { + if (!$error && $object->update($user) > 0) { header('Location: '.DOL_URL_ROOT.'/categories/viewcat.php?id='.$object->id.'&type='.$type); exit; } else { @@ -173,8 +163,7 @@ print ''; $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint; -if (empty($reshook)) -{ +if (empty($reshook)) { print $object->showOptionals($extrafields, 'edit', $parameters); } From a031adf99e670ad498e47d80627d13cb318aa3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sun, 25 Oct 2020 20:07:22 +0100 Subject: [PATCH 2/3] clean code --- htdocs/categories/class/categorie.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 0a7654917a5..302366f6669 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -323,7 +323,7 @@ class Categorie extends CommonObject $sql .= " WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."'"; } else { $sql .= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")"; - if (!is_null($type)) $sql .= " AND type = ".$this->db->escape($type); + if (!is_null($type)) $sql .= " AND type = ".(int) $type; } dol_syslog(get_class($this)."::fetch", LOG_DEBUG); @@ -518,8 +518,8 @@ class Categorie extends CommonObject if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { $sql .= ", fk_soc = ".($this->socid != -1 ? $this->socid : 'null'); } - $sql .= ", visible = '".$this->db->escape($this->visible)."'"; - $sql .= ", fk_parent = ".$this->fk_parent; + $sql .= ", visible = ".(int) $this->visible; + $sql .= ", fk_parent = ".(int) $this->fk_parent; $sql .= ", fk_user_modif = ".(int) $user->id; $sql .= " WHERE rowid = ".$this->id; @@ -1203,7 +1203,7 @@ class Categorie extends CommonObject $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; $sql .= " WHERE entity IN (".getEntity('category').")"; if (!is_null($type)) - $sql .= " AND type = ".$type; + $sql .= " AND type = ".(int) $type; if ($parent) $sql .= " AND fk_parent = 0"; From b2f08e576235a7afa639585bfabd3e1d2c32c1f6 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 26 Oct 2020 19:23:23 +0100 Subject: [PATCH 3/3] Update categorie.class.php --- htdocs/categories/class/categorie.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 302366f6669..8d9b96976cd 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -323,7 +323,7 @@ class Categorie extends CommonObject $sql .= " WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."'"; } else { $sql .= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")"; - if (!is_null($type)) $sql .= " AND type = ".(int) $type; + if (!is_null($type)) $sql .= " AND type = ".((int) $type); } dol_syslog(get_class($this)."::fetch", LOG_DEBUG);