static function updateSociete(&$PDOdb, &$soc, $fk_category = null, $not_in_category = null)
 {
     global $langs, $conf, $user, $db;
     $TUserIdAffected = TCommercialCategory::getAllUserForSociete($PDOdb, $soc->id, $fk_category, $not_in_category);
     if (!empty($TUserIdAffected)) {
         foreach ($TUserIdAffected as $idcomm) {
             //	print "Ajout $idcomm dans ".$soc->id."<br>";
             $res = $soc->add_commercial($user, $idcomm);
         }
     }
     $listsalesrepresentatives = $soc->getSalesRepresentatives($user);
     foreach ($listsalesrepresentatives as &$comm) {
         if (!in_array($comm['id'], $TUserIdAffected)) {
             $soc->del_commercial($user, $comm['id']);
         }
     }
 }
function _fiche(&$PDOdb, $id)
{
    global $conf, $db, $langs, $user, $form;
    $object = new Categorie($db);
    $result = $object->fetch($id);
    $object->fetch_optionals($id, $extralabels);
    if ($result <= 0) {
        dol_print_error($db, $object->error);
        exit;
    }
    llxHeader("", "", $langs->trans("Categories"));
    $title = $langs->trans("CustomersCategoryShort");
    $head = categories_prepare_head($object, Categorie::TYPE_CUSTOMER);
    dol_fiche_head($head, 'commercial', $title, 0, 'category');
    print '<table class="border" width="100%">';
    print '<tr><td width="20%" class="notopnoleft">';
    $ways = $object->print_all_ways();
    print $langs->trans("Ref") . '</td><td>';
    print '<a href="' . DOL_URL_ROOT . '/categories/index.php?leftmenu=cat&type=' . $type . '">' . $langs->trans("Root") . '</a> >> ';
    foreach ($ways as $way) {
        print $way . "<br>\n";
    }
    print '</td></tr>';
    // Description
    print '<tr><td width="20%" class="notopnoleft">';
    print $langs->trans("Description") . '</td><td>';
    print dol_htmlentitiesbr($object->description);
    print '</td></tr>';
    print '<tr><td>' . $langs->trans("SalesRepresentatives") . '</td>';
    print '<td>';
    $TUser = TCommercialCategory::getUser($PDOdb, $object->id);
    if (!empty($TUser)) {
        foreach ($TUser as &$u) {
            echo $u->getNomUrl(1);
            if ($user->rights->societe->creer) {
                print '<a href="?id=' . $object->id . '&commid=' . $u->id . '&action=delete">';
                print img_delete();
                print '</a>';
            }
            print '<br />';
        }
    }
    print '</td></tr>';
    print '</table>';
    dol_fiche_end();
    if ($user->rights->societe->creer && $user->rights->societe->client->voir) {
        /*
         * Copier... Coller... Jobi... Joba...
         */
        $langs->load("users");
        $title = $langs->trans("ListOfUsers");
        $sql = "SELECT u.rowid, u.lastname, u.firstname, u.login";
        $sql .= " FROM " . MAIN_DB_PREFIX . "user as u LEFT JOIN " . MAIN_DB_PREFIX . "commercial_category cc ON (cc.fk_user = u .rowid AND cc.fk_category=" . $object->id . ")";
        $sql .= " WHERE u.entity IN (0," . $conf->entity . ")";
        if (!empty($conf->global->USER_HIDE_INACTIVE_IN_COMBOBOX)) {
            $sql .= " AND u.statut<>0 ";
        }
        $sql .= " AND cc.rowid IS NULL ";
        $sql .= " ORDER BY u.lastname ASC ";
        //var_dump($sql);
        $resql = $db->query($sql);
        if ($resql) {
            $num = $db->num_rows($resql);
            $i = 0;
            print load_fiche_titre($title);
            // Lignes des titres
            print '<table class="noborder" width="100%">';
            print '<tr class="liste_titre">';
            print '<td>' . $langs->trans("Name") . '</td>';
            print '<td>' . $langs->trans("Login") . '</td>';
            print '<td>&nbsp;</td>';
            print "</tr>\n";
            $var = True;
            while ($i < $num) {
                $obj = $db->fetch_object($resql);
                $var = !$var;
                print "<tr " . $bc[$var] . "><td>";
                print '<a href="' . DOL_URL_ROOT . '/user/card.php?id=' . $obj->rowid . '">';
                print img_object($langs->trans("ShowUser"), "user") . ' ';
                print dolGetFirstLastname($obj->firstname, $obj->lastname) . "\n";
                print '</a>';
                print '</td><td>' . $obj->login . '</td>';
                print '<td><a href="?id=' . $object->id . '&commid=' . $obj->rowid . '&action=add">' . $langs->trans("Add") . '</a></td>';
                print '</tr>' . "\n";
                $i++;
            }
            print "</table>";
            $db->free($resql);
        } else {
            dol_print_error($db);
        }
    }
    llxFooter();
}
 /**
  * Function called when a Dolibarrr business event is done.
  * All functions "run_trigger" are triggered if file
  * is inside directory core/triggers
  *
  * 	@param		string		$action		Event action code
  * 	@param		Object		$object		Object
  * 	@param		User		$user		Object user
  * 	@param		Translate	$langs		Object langs
  * 	@param		conf		$conf		Object conf
  * 	@return		int						<0 if KO, 0 if no triggered ran, >0 if OK
  */
 public function run_trigger($action, $object, $user, $langs, $conf)
 {
     // Put here code you want to execute when a Dolibarr business events occurs.
     // Data and type of action are stored into $object and $action
     // Users
     if (($action === 'CATEGORY_LINK' || $action === 'CATEGORY_UNLINK') && get_class($object) == 'Societe') {
         //var_dump($object->id, $object->linkto);exit;
         define('INC_FROM_DOLIBARR', true);
         dol_include_once('/commercialbycategory/config.php');
         dol_include_once('/commercialbycategory/class/commercialcategory.class.php');
         $PDOdb = new TPDOdb();
         if ($action === 'CATEGORY_LINK') {
             TCommercialCategory::updateSociete($PDOdb, $object->linkto, $object->id);
         } else {
             TCommercialCategory::updateSociete($PDOdb, $object->unlinkoff, null, $object->id);
         }
         //exit('!');
         dol_syslog("Trigger '" . $this->name . "' for action '{$action}' launched by " . __FILE__ . ". id=" . $object->id);
         return 1;
     }
     return 0;
 }
<?php

/*
 * Script créant et vérifiant que les champs requis s'ajoutent bien
 */
if (!defined('INC_FROM_DOLIBARR')) {
    define('INC_FROM_CRON_SCRIPT', true);
    require '../config.php';
}
dol_include_once('/commercialbycategory/class/commercialcategory.class.php');
$PDOdb = new TPDOdb();
$o = new TCommercialCategory($db);
$o->init_db_by_vars($PDOdb);