23require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
34 public $element =
'link';
39 public $table_element =
'links';
80 $langs->load(
"errors");
82 if (empty($this->label)) {
83 $this->label = trim(basename($this->url));
85 if (empty($this->datea)) {
88 $this->url = trim($this->url);
90 dol_syslog(get_class($this).
"::create ".$this->url);
93 if (empty($this->url)) {
94 $this->error = $langs->trans(
"NoUrl");
100 $sql =
"INSERT INTO ".$this->db->prefix().
"links (entity, datea, url, label, objecttype, objectid)";
101 $sql .=
" VALUES (".$conf->entity.
", '".$this->db->idate($this->datea).
"'";
102 $sql .=
", '".$this->db->escape($this->url).
"'";
103 $sql .=
", '".$this->db->escape($this->label).
"'";
104 $sql .=
", '".$this->db->escape($this->objecttype).
"'";
105 $sql .=
", ".((int) $this->objectid).
")";
107 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
108 $result = $this->db->query($sql);
110 $this->
id = $this->db->last_insert_id($this->db->prefix().
"links");
124 dol_syslog(get_class($this).
"::Create success id=".$this->
id);
128 dol_syslog(get_class($this).
"::Create echec update ".$this->error, LOG_ERR);
129 $this->db->rollback();
133 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
134 $this->error = $langs->trans(
"ErrorCompanyNameAlreadyExists", $this->
name);
137 $this->error = $this->db->lasterror();
140 $this->db->rollback();
152 public function update($user =
'', $call_trigger = 1)
154 global $langs, $conf;
155 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
157 $langs->load(
"errors");
160 dol_syslog(get_class($this).
"::Update id = ".$this->
id.
" call_trigger = ".$call_trigger);
163 if (empty($this->url)) {
164 $this->error = $langs->trans(
"NoURL");
170 if (empty($this->label)) {
171 $this->label = basename($this->url);
173 $this->label = trim($this->label);
178 $sql =
"UPDATE ".$this->db->prefix().
"links SET ";
179 $sql .=
"entity = ".$conf->entity;
180 $sql .=
", datea = '".$this->db->idate(
dol_now()).
"'";
181 $sql .=
", url = '".$this->db->escape($this->url).
"'";
182 $sql .=
", label = '".$this->db->escape($this->label).
"'";
183 $sql .=
", objecttype = '".$this->db->escape($this->objecttype).
"'";
184 $sql .=
", objectid = ".$this->objectid;
185 $sql .=
" WHERE rowid = ".((int) $this->
id);
187 dol_syslog(get_class($this).
"::update sql = ".$sql);
188 $resql = $this->db->query($sql);
200 dol_syslog(get_class($this).
"::Update success");
205 $this->db->rollback();
209 if ($this->db->errno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
211 $this->error = $langs->trans(
"ErrorDuplicateField");
214 $this->error = $langs->trans(
"Error sql = ".$sql);
217 $this->db->rollback();
232 public function fetchAll(&$links, $objecttype, $objectid, $sortfield =
null, $sortorder =
null)
236 $sql =
"SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix().
"links";
237 $sql .=
" WHERE objecttype = '".$this->db->escape($objecttype).
"' AND objectid = ".((int) $objectid);
238 if ($conf->entity != 0) {
239 $sql .=
" AND entity = ".$conf->entity;
242 if (empty($sortorder)) {
245 $sql .=
" ORDER BY ".$sortfield.
" ".$sortorder;
248 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
249 $resql = $this->db->query($sql);
251 $num = $this->db->num_rows($resql);
252 dol_syslog(get_class($this).
"::fetchAll num=".((
int) $num), LOG_DEBUG);
254 while ($obj = $this->db->fetch_object($resql)) {
255 $link =
new Link($this->db);
256 $link->id = $obj->rowid;
257 $link->entity = $obj->entity;
258 $link->datea = $this->db->jdate($obj->datea);
259 $link->url = $obj->url;
260 $link->label = $obj->label;
261 $link->objecttype = $obj->objecttype;
262 $link->objectid = $obj->objectid;
282 public static function count($dbs, $objecttype, $objectid)
286 $sql =
"SELECT COUNT(rowid) as nb FROM ".$dbs->prefix().
"links";
287 $sql .=
" WHERE objecttype = '".$dbs->escape($objecttype).
"' AND objectid = ".((int) $objectid);
288 if ($conf->entity != 0) {
289 $sql .=
" AND entity = ".$conf->entity;
292 $resql = $dbs->query($sql);
294 $obj = $dbs->fetch_object($resql);
308 public function fetch($rowid =
null)
316 $sql =
"SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix().
"links";
317 $sql .=
" WHERE rowid = ".((int) $rowid);
318 if ($conf->entity != 0) {
319 $sql .=
" AND entity = ".$conf->entity;
322 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
323 $resql = $this->db->query($sql);
325 if ($this->db->num_rows($resql) > 0) {
326 $obj = $this->db->fetch_object($resql);
328 $this->
id = $obj->rowid;
329 $this->entity = $obj->entity;
330 $this->datea = $this->db->jdate($obj->datea);
331 $this->url = $obj->url;
332 $this->label = $obj->label;
333 $this->objecttype = $obj->objecttype;
334 $this->objectid = $obj->objectid;
340 $this->error = $this->db->lasterror();
351 public function delete($user)
353 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
361 $this->db->rollback();
367 $sql =
"DELETE FROM ".$this->db->prefix().
"links";
368 $sql .=
" WHERE rowid = ".((int) $this->
id);
370 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
371 if (!$this->db->query($sql)) {
373 $this->error = $this->db->lasterror();
381 $this->db->rollback();
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
static count($dbs, $objecttype, $objectid)
Return nb of links.
create($user='')
Create link in database.
fetch($rowid=null)
Loads a link from database.
__construct($db)
Constructor.
fetchAll(&$links, $objecttype, $objectid, $sortfield=null, $sortorder=null)
Loads all links from database.
update($user='', $call_trigger=1)
Update parameters of third party.
clean_url($url, $http=1)
Clean an url string.
dol_now($mode='auto')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.