Merge pull request #3626 from GPCsolutions/3.8-3607

FIX #3607 Better categories setting and unsetting
This commit is contained in:
Laurent Destailleur
2015-10-01 13:59:05 +02:00
9 changed files with 205 additions and 108 deletions

View File

@@ -326,25 +326,8 @@ if (empty($reshook))
if ($result >= 0 && ! count($object->errors))
{
// Categories association
// First we delete all categories association
$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'categorie_member';
$sql .= ' WHERE fk_member = ' . $object->id;
$resql = $db->query($sql);
if (! $resql) dol_print_error($db);
// Then we add the associated categories
$categories = GETPOST('memcats', 'array');
if (! empty($categories))
{
$cat = new Categorie($db);
foreach ($categories as $id_category)
{
$cat->fetch($id_category);
$cat->add_type($object, 'member');
}
}
$object->setCategories($categories);
// Logo/Photo save
$dir= $conf->adherent->dir_output . '/' . get_exdir($object->id,2,0,1,$object,'member').'/photos';
@@ -560,15 +543,7 @@ if (empty($reshook))
{
// Categories association
$memcats = GETPOST('memcats', 'array');
if (! empty($memcats))
{
$cat = new Categorie($db);
foreach ($memcats as $id_category)
{
$cat->fetch($id_category);
$cat->add_type($object, 'member');
}
}
$object->setCategories($memcats);
$db->commit();
$rowid=$object->id;

View File

@@ -1959,6 +1959,49 @@ class Adherent extends CommonObject
}
}
/**
* Sets object to supplied categories.
*
* Deletes object from existing categories not supplied.
* Adds it to non existing supplied categories.
* Existing categories are left untouch.
*
* @param int[]|int $categories Category or categories IDs
*/
public function setCategories($categories)
{
// Handle single category
if (!is_array($categories)) {
$categories = array($categories);
}
// Get current categories
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
$c = new Categorie($this->db);
$existing = $c->containing($this->id, Categorie::TYPE_MEMBER, 'id');
// Diff
if (is_array($existing)) {
$to_del = array_diff($existing, $categories);
$to_add = array_diff($categories, $existing);
} else {
$to_del = array(); // Nothing to delete
$to_add = $categories;
}
// Process
foreach ($to_del as $del) {
$c->fetch($del);
$c->del_type($this, 'member');
}
foreach ($to_add as $add) {
$c->fetch($add);
$c->add_type($this, 'member');
}
return;
}
/**
* Function used to replace a thirdparty id with another one.
*