25require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 public $element =
'link';
41 public $table_element =
'links';
46 public $isextrafieldmanaged = 1;
107 global $langs,
$conf;
110 $langs->loadLangs(array(
"errors",
"admin"));
112 if (empty($this->label)) {
113 $this->label = trim(basename($this->url));
115 if (empty($this->datea)) {
118 $this->url = trim($this->url);
120 dol_syslog(get_class($this).
"::create ".$this->url);
123 if (empty($this->url)) {
124 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"URL"));
130 $sql =
"INSERT INTO ".$this->db->prefix().
"links (entity, datea, url, label, objecttype, objectid, share,share_pass)";
131 $sql .=
" VALUES (".((int)
$conf->entity).
", '".$this->db->idate($this->datea).
"'";
132 $sql .=
", '".$this->db->escape($this->url).
"'";
133 $sql .=
", '".$this->db->escape($this->label).
"'";
134 $sql .=
", '".$this->db->escape($this->objecttype).
"'";
135 $sql .=
", ".((int) $this->objectid);
136 $sql .=
', '.(!empty($this->share) ?
"'".$this->db->escape($this->share).
"'" :
"null");
137 $sql .=
', '.(!empty($this->share_pass) ?
"'".$this->db->escape($this->share_pass).
"'" :
"null").
")";
139 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
140 $result = $this->db->query($sql);
142 $this->
id = $this->db->last_insert_id($this->db->prefix().
"links");
153 $result = $this->call_trigger(
'LINK_CREATE', $user);
164 dol_syslog(get_class($this).
"::Create success id=".$this->
id);
168 dol_syslog(get_class($this).
"::Create echec update ".$this->error, LOG_ERR);
169 $this->db->rollback();
173 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
174 $this->error = $langs->trans(
"ErrorDuplicateField");
177 $this->error = $this->db->lasterror();
180 $this->db->rollback();
194 global $langs,
$conf;
195 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
197 $langs->loadLangs(array(
"errors",
"admin"));
200 dol_syslog(get_class($this).
"::Update id = ".$this->
id.
" call_trigger = ".$call_trigger);
203 if (empty($this->url)) {
204 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"URL"));
210 if (empty($this->label)) {
211 $this->label = basename($this->url);
213 $this->label = trim($this->label);
218 $sql =
"UPDATE ".$this->db->prefix().
"links SET ";
219 $sql .=
"entity = ".((int)
$conf->entity);
220 $sql .=
", datea = '".$this->db->idate(
dol_now()).
"'";
221 $sql .=
", url = '".$this->db->escape($this->url).
"'";
222 $sql .=
", label = '".$this->db->escape($this->label).
"'";
223 $sql .=
", objecttype = '".$this->db->escape($this->objecttype).
"'";
224 $sql .=
", objectid = ".((int) $this->objectid);
225 $sql .=
', share = '.(!empty($this->share) ?
"'".$this->db->escape($this->share).
"'" :
"null");
226 $sql .=
', share_pass = '.(!empty($this->share_pass) ?
"'".$this->db->escape($this->share_pass).
"'" :
"null");
227 $sql .=
" WHERE rowid = ".((int) $this->
id);
229 dol_syslog(get_class($this).
"::update sql = ".$sql);
230 $resql = $this->db->query($sql);
238 if (!$error && $call_trigger) {
240 $result = $this->call_trigger(
'LINK_MODIFY', $user);
248 dol_syslog(get_class($this).
"::Update success");
253 $this->db->rollback();
257 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
259 $this->error = $langs->trans(
"ErrorDuplicateField");
262 $this->error = $langs->trans(
"Error sql").
"= $sql";
265 $this->db->rollback();
280 public function fetchAll(&$links, $objecttype, $objectid, $sortfield =
null, $sortorder =
null)
284 $sql =
"SELECT rowid, entity, datea, url, label, objecttype, objectid, share,share_pass FROM ".$this->db->prefix().
"links";
285 $sql .=
" WHERE objecttype = '".$this->db->escape($objecttype).
"' AND objectid = ".((int) $objectid);
286 if (
$conf->entity != 0) {
287 $sql .=
" AND entity = ".((int)
$conf->entity);
290 if (empty($sortorder)) {
293 $sql .= $this->db->order($sortfield, $sortorder);
296 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
297 $resql = $this->db->query($sql);
299 $num = $this->db->num_rows($resql);
300 dol_syslog(get_class($this).
"::fetchAll num=".((
int) $num), LOG_DEBUG);
302 while ($obj = $this->db->fetch_object($resql)) {
303 $link =
new Link($this->db);
304 $link->id = (int) $obj->rowid;
305 $link->entity = $obj->entity;
306 $link->datea = $this->db->jdate($obj->datea);
307 $link->url = $obj->url;
308 $link->label = $obj->label;
309 $link->objecttype = $obj->objecttype;
310 $link->objectid = $obj->objectid;
311 $link->share = $obj->share;
312 $link->share_pass = $obj->share_pass;
315 $link->fetch_optionals();
336 public static function count($dbs, $objecttype, $objectid)
340 $sql =
"SELECT COUNT(rowid) as nb FROM ".$dbs->prefix().
"links";
341 $sql .=
" WHERE objecttype = '".$dbs->escape($objecttype).
"' AND objectid = ".((int) $objectid);
342 if (
$conf->entity != 0) {
343 $sql .=
" AND entity = ".((int)
$conf->entity);
346 $resql = $dbs->query($sql);
348 $obj = $dbs->fetch_object($resql);
363 public function fetch($rowid =
null, $hashforshare =
'')
371 if (empty($rowid) && empty($hashforshare)) {
372 $this->error =
'ErrorBadParameters';
378 $sql =
"SELECT rowid, entity, datea, url, label, objecttype, objectid, share, share_pass FROM ".$this->db->prefix().
"links";
379 if (!empty((
int) $rowid)) {
380 $sqlwhere[] =
" rowid = ".((int) $rowid);
382 if (!empty($hashforshare)) {
383 $sqlwhere[] =
" share = '".$this->db->escape($hashforshare).
"'";
386 if (
$conf->entity != 0) {
387 $sqlwhere[] =
" entity = ".((int)
$conf->entity);
389 if (
count($sqlwhere) > 0) {
390 $sql .=
' WHERE '.implode(
' AND ', $sqlwhere);
393 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
394 $resql = $this->db->query($sql);
396 if ($this->db->num_rows($resql) > 0) {
397 $obj = $this->db->fetch_object($resql);
399 $this->
id = $obj->rowid;
400 $this->entity = $obj->entity;
401 $this->datea = $this->db->jdate($obj->datea);
402 $this->url = $obj->url;
403 $this->label = $obj->label;
404 $this->objecttype = $obj->objecttype;
405 $this->objectid = $obj->objectid;
406 $this->share = $obj->share;
407 $this->share_pass = $obj->share_pass;
417 $this->error = $this->db->lasterror();
429 public function delete(
User $user, $notrigger = 0)
431 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
438 $result = $this->call_trigger(
'LINK_DELETE', $user);
440 $this->db->rollback();
447 $sql =
"DELETE FROM ".$this->db->prefix().
"links";
448 $sql .=
" WHERE rowid = ".((int) $this->
id);
450 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
451 if (!$this->db->query($sql)) {
453 $this->error = $this->db->lasterror();
469 $this->db->rollback();
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
static count($dbs, $objecttype, $objectid)
Return nb of links.
update(User $user, $call_trigger=1)
Update parameters of link.
__construct($db)
Constructor.
fetchAll(&$links, $objecttype, $objectid, $sortfield=null, $sortorder=null)
Loads all links from database.
create(User $user)
Create link in database.
fetch($rowid=null, $hashforshare='')
Loads a link from database.
Class to manage Dolibarr users.
clean_url($url, $http=1)
Clean an url string.
dol_now($mode='gmt')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.