Skip to content

Commit

Permalink
FIX Issue 32460 db prefix module builder
Browse files Browse the repository at this point in the history
  • Loading branch information
atm-quentin committed Jan 15, 2025
1 parent 9ee4a03 commit 01be602
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 42 deletions.
22 changes: 11 additions & 11 deletions htdocs/core/modules/mailings/modules_mailings.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ public function update_nb($mailing_id)
{
// phpcs:enable
// Mise a jour nombre de destinataire dans table des mailings
$sql = "SELECT COUNT(*) nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
$sql = "SELECT COUNT(*) nb FROM ".$this->db->prefix()."mailing_cibles";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id);
$result = $this->db->query($sql);
if ($result) {
$obj = $this->db->fetch_object($result);
$nb = $obj->nb;

$sql = "UPDATE ".MAIN_DB_PREFIX."mailing";
$sql = "UPDATE ".$this->db->prefix()."mailing";
$sql .= " SET nbemail = ".((int) $nb)." WHERE rowid = ".((int) $mailing_id);
if (!$this->db->query($sql)) {
dol_syslog($this->db->error());
Expand Down Expand Up @@ -208,7 +208,7 @@ public function addTargetsToDatabase($mailing_id, $cibles)
$num = count($cibles);
foreach ($cibles as $targetarray) {
if (!empty($targetarray['email'])) { // avoid empty email address
$sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_cibles";
$sql = "INSERT INTO ".$this->db->prefix()."mailing_cibles";
$sql .= " (fk_mailing,";
$sql .= " fk_contact,";
$sql .= " lastname, firstname, email, other, source_url, source_id,";
Expand Down Expand Up @@ -244,27 +244,27 @@ public function addTargetsToDatabase($mailing_id, $cibles)

/*
//Update the status to show thirdparty mail that don't want to be contacted anymore'
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
$sql = "UPDATE ".$this->db->prefix()."mailing_cibles";
$sql .= " SET statut=3";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND email in (SELECT email FROM ".MAIN_DB_PREFIX."societe where fk_stcomm=-1)";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND email in (SELECT email FROM ".$this->db->prefix()."societe where fk_stcomm=-1)";
$sql .= " AND source_type='thirdparty'";
dol_syslog(__METHOD__.": mailing update status to display thirdparty mail that do not want to be contacted");
$result=$this->db->query($sql);
//Update the status to show contact mail that don't want to be contacted anymore'
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
$sql = "UPDATE ".$this->db->prefix()."mailing_cibles";
$sql .= " SET statut=3";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND source_type='contact' AND (email in (SELECT sc.email FROM ".MAIN_DB_PREFIX."socpeople AS sc ";
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id)." AND source_type='contact' AND (email in (SELECT sc.email FROM ".$this->db->prefix()."socpeople AS sc ";
$sql .= " INNER JOIN ".$this->db->prefix()."societe s ON s.rowid=sc.fk_soc WHERE s.fk_stcomm=-1 OR no_email=1))";
dol_syslog(__METHOD__.": mailing update status to display contact mail that do not want to be contacted",LOG_DEBUG);
$result=$this->db->query($sql);
*/

if (empty($this->evenunsubscribe)) {
$sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles as mc";
$sql = "UPDATE ".$this->db->prefix()."mailing_cibles as mc";
$sql .= " SET mc.statut = 3";
$sql .= " WHERE mc.fk_mailing = ".((int) $mailing_id);
$sql .= " AND EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX."mailing_unsubscribe as mu WHERE mu.email = mc.email and mu.entity = ".((int) $conf->entity).")";
$sql .= " AND EXISTS (SELECT rowid FROM ".$this->db->prefix()."mailing_unsubscribe as mu WHERE mu.email = mc.email and mu.entity = ".((int) $conf->entity).")";

dol_syslog(__METHOD__.":mailing update status to display emails that do not want to be contacted anymore", LOG_DEBUG);
$result = $this->db->query($sql);
Expand All @@ -291,7 +291,7 @@ public function addTargetsToDatabase($mailing_id, $cibles)
public function clear_target($mailing_id)
{
// phpcs:enable
$sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
$sql = "DELETE FROM ".$this->db->prefix()."mailing_cibles";
$sql .= " WHERE fk_mailing = ".((int) $mailing_id);

if (!$this->db->query($sql)) {
Expand Down
2 changes: 1 addition & 1 deletion htdocs/modulebuilder/template/admin/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@
// Load array def with activated templates
$def = array();
$sql = "SELECT nom";
$sql .= " FROM ".MAIN_DB_PREFIX."document_model";
$sql .= " FROM ".$db->prefix()."document_model";
$sql .= " WHERE type = '".$db->escape($type)."'";
$sql .= " AND entity = ".$conf->entity;
$resql = $db->query($sql);
Expand Down
8 changes: 4 additions & 4 deletions htdocs/modulebuilder/template/class/api_mymodule.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,
}

$sql = "SELECT t.rowid";
$sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
$sql .= " FROM ".$this->db->prefix().$tmpobject->table_element." AS t";
$sql .= " LEFT JOIN ".$this->db->prefix().$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
$sql .= " WHERE 1 = 1";
if ($tmpobject->ismultientitymanaged) {
$sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
Expand All @@ -148,9 +148,9 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,
// Search on sale representative
if ($search_sale && $search_sale != '-1') {
if ($search_sale == -2) {
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".$this->db->prefix()."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
} elseif ($search_sale > 0) {
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".$this->db->prefix()."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
}
}
if ($sqlfilters) {
Expand Down
8 changes: 4 additions & 4 deletions htdocs/modulebuilder/template/class/myobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public function validate($user, $notrigger = 0)

if (!empty($num)) {
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql = "UPDATE ".$this->db->prefix().$this->table_element;
$sql .= " SET ";
if (!empty($this->fields['ref'])) {
$sql .= " ref = '".$this->db->escape($num)."',";
Expand Down Expand Up @@ -625,14 +625,14 @@ public function validate($user, $notrigger = 0)
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref)) {
// Now we rename also files into index
$sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'myobject/".$this->db->escape($this->newref)."'";
$sql = 'UPDATE '.$this->db->prefix()."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'myobject/".$this->db->escape($this->newref)."'";
$sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'myobject/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->error = $this->db->lasterror();
}
$sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'myobject/".$this->db->escape($this->newref)."'";
$sql = 'UPDATE '.$this->db->prefix()."ecm_files set filepath = 'myobject/".$this->db->escape($this->newref)."'";
$sql .= " WHERE filepath = 'myobject/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
$resql = $this->db->query($sql);
if (!$resql) {
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public function info($id)
if (!empty($this->fields['fk_user_valid'])) {
$sql .= ", fk_user_valid";
}
$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
$sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
$sql .= " WHERE t.rowid = ".((int) $id);

$result = $this->db->query($sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function add_to_target($mailing_id)
$j = 0;

$sql = "SELECT rowid as id, firstname, lastname, email";
$sql .= " FROM ".MAIN_DB_PREFIX."myobject";
$sql .= " FROM ".$this->db->prefix()."myobject";
$sql .= " WHERE email IS NOT NULL AND email <> ''";
if (GETPOSTISSET('filter') && GETPOST('filter', 'alphanohtml') != 'none') {
$sql .= " AND status = '".$this->db->escape(GETPOST('filter', 'alphanohtml'))."'";
Expand Down Expand Up @@ -195,7 +195,7 @@ public function getSqlArrayForStats()
public function getNbOfRecipients($sql = '')
{
$sql = "SELECT COUNT(DISTINCT(email)) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."myobject as p";
$sql .= " FROM ".$this->db->prefix()."myobject as p";
$sql .= " WHERE email IS NOT NULL AND email <> ''";

$a = parent::getNbOfRecipients($sql);
Expand Down
18 changes: 9 additions & 9 deletions htdocs/modulebuilder/template/core/modules/modMyModule.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function __construct($db)
// Label of tables
'tablib' => array("Table1", "Table2", "Table3"),
// Request to select fields
'tabsql' => array('SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table1 as f', 'SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table2 as f', 'SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.MAIN_DB_PREFIX.'table3 as f'),
'tabsql' => array('SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.$this->db->prefix().'table1 as f', 'SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.$this->db->prefix().'table2 as f', 'SELECT f.rowid as rowid, f.code, f.label, f.active FROM '.$this->db->prefix().'table3 as f'),
// Sort order
'tabsqlsort' => array("label ASC", "label ASC", "label ASC"),
// List of fields (result of select to show dictionary)
Expand Down Expand Up @@ -411,8 +411,8 @@ public function __construct($db)
//$this->export_examplevalues_array[$r] = array('t.field' => 'Example');
//$this->export_help_array[$r] = array('t.field' => 'FieldDescHelp');
$this->export_sql_start[$r]='SELECT DISTINCT ';
$this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'mymodule_myobject as t';
//$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'mymodule_myobject_line as tl ON tl.fk_myobject = t.rowid';
$this->export_sql_end[$r] =' FROM '.$this->db->prefix().'mymodule_myobject as t';
//$this->export_sql_end[$r] .=' LEFT JOIN '.$this->db->prefix().'mymodule_myobject_line as tl ON tl.fk_myobject = t.rowid';
$this->export_sql_end[$r] .=' WHERE 1 = 1';
$this->export_sql_end[$r] .=' AND t.entity IN ('.getEntity('myobject').')';
$r++; */
Expand All @@ -426,15 +426,15 @@ public function __construct($db)
$this->import_code[$r] = $this->rights_class.'_'.$r;
$this->import_label[$r] = 'MyObjectLines'; // Translation key (used only if key ExportDataset_xxx_z not found)
$this->import_icon[$r] = $this->picto;
$this->import_tables_array[$r] = array('t' => MAIN_DB_PREFIX.'mymodule_myobject', 'extra' => MAIN_DB_PREFIX.'mymodule_myobject_extrafields');
$this->import_tables_array[$r] = array('t' => $this->db->prefix().'mymodule_myobject', 'extra' => $this->db->prefix().'mymodule_myobject_extrafields');
$this->import_tables_creator_array[$r] = array('t' => 'fk_user_author'); // Fields to store import user id
$import_sample = array();
$keyforclass = 'MyObject'; $keyforclassfile='/mymodule/class/myobject.class.php'; $keyforelement='myobject@mymodule';
include DOL_DOCUMENT_ROOT.'/core/commonfieldsinimport.inc.php';
$import_extrafield_sample = array();
$keyforselect='myobject'; $keyforaliasextra='extra'; $keyforelement='myobject@mymodule';
include DOL_DOCUMENT_ROOT.'/core/extrafieldsinimport.inc.php';
$this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.MAIN_DB_PREFIX.'mymodule_myobject');
$this->import_fieldshidden_array[$r] = array('extra.fk_object' => 'lastrowid-'.$this->db->prefix().'mymodule_myobject');
$this->import_regex_array[$r] = array();
$this->import_examplevalues_array[$r] = array_merge($import_sample, $import_extrafield_sample);
$this->import_updatekeys_array[$r] = array('t.ref' => 'Ref');
Expand Down Expand Up @@ -512,10 +512,10 @@ public function init($options = '')
}

$sql = array_merge($sql, array(
"DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'standard_".strtolower($myTmpObjectKey)."' AND type = '".$this->db->escape(strtolower($myTmpObjectKey))."' AND entity = ".((int) $conf->entity),
"INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('standard_".strtolower($myTmpObjectKey)."', '".$this->db->escape(strtolower($myTmpObjectKey))."', ".((int) $conf->entity).")",
"DELETE FROM ".MAIN_DB_PREFIX."document_model WHERE nom = 'generic_".strtolower($myTmpObjectKey)."_odt' AND type = '".$this->db->escape(strtolower($myTmpObjectKey))."' AND entity = ".((int) $conf->entity),
"INSERT INTO ".MAIN_DB_PREFIX."document_model (nom, type, entity) VALUES('generic_".strtolower($myTmpObjectKey)."_odt', '".$this->db->escape(strtolower($myTmpObjectKey))."', ".((int) $conf->entity).")"
"DELETE FROM ".$this->db->prefix()."document_model WHERE nom = 'standard_".strtolower($myTmpObjectKey)."' AND type = '".$this->db->escape(strtolower($myTmpObjectKey))."' AND entity = ".((int) $conf->entity),
"INSERT INTO ".$this->db->prefix()."document_model (nom, type, entity) VALUES('standard_".strtolower($myTmpObjectKey)."', '".$this->db->escape(strtolower($myTmpObjectKey))."', ".((int) $conf->entity).")",
"DELETE FROM ".$this->db->prefix()."document_model WHERE nom = 'generic_".strtolower($myTmpObjectKey)."_odt' AND type = '".$this->db->escape(strtolower($myTmpObjectKey))."' AND entity = ".((int) $conf->entity),
"INSERT INTO ".$this->db->prefix()."document_model (nom, type, entity) VALUES('generic_".strtolower($myTmpObjectKey)."_odt', '".$this->db->escape(strtolower($myTmpObjectKey))."', ".((int) $conf->entity).")"
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function canBeActivated($object)

$posindice = strlen($this->prefix) + 6;
$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
$sql .= " FROM ".MAIN_DB_PREFIX."mymodule_myobject";
$sql .= " FROM ".$db->prefix()."mymodule_myobject";
$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
if ($object->ismultientitymanaged == 1) {
$sql .= " AND entity = ".$conf->entity;
Expand Down Expand Up @@ -131,7 +131,7 @@ public function getNextValue($object)
// first we get the max value
$posindice = strlen($this->prefix) + 6;
$sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max";
$sql .= " FROM ".MAIN_DB_PREFIX."mymodule_myobject";
$sql .= " FROM ".$db->prefix()."mymodule_myobject";
$sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'";
if ($object->ismultientitymanaged == 1) {
$sql .= " AND entity = ".$conf->entity;
Expand Down
6 changes: 3 additions & 3 deletions htdocs/modulebuilder/template/mymoduleindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@
$sql = "SELECT c.rowid, c.ref, c.ref_client, c.total_ht, c.tva as total_tva, c.total_ttc, s.rowid as socid, s.nom as name, s.client, s.canvas";
$sql.= ", s.code_client";
$sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."societe as s";
$sql.= " FROM ".$db->prefix()."commande as c";
$sql.= ", ".$db->prefix()."societe as s";
$sql.= " WHERE c.fk_soc = s.rowid";
$sql.= " AND c.fk_statut = 0";
$sql.= " AND c.entity IN (".getEntity('commande').")";
Expand Down Expand Up @@ -204,7 +204,7 @@
// Last modified myobject
if (isModEnabled('mymodule') && $user->hasRight('mymodule', 'read')) {
$sql = "SELECT s.rowid, s.ref, s.label, s.date_creation, s.tms";
$sql.= " FROM ".MAIN_DB_PREFIX."mymodule_myobject as s";
$sql.= " FROM ".$db->prefix()."mymodule_myobject as s";
$sql.= " WHERE s.entity IN (".getEntity($myobjectstatic->element).")";
//if ($socid) $sql.= " AND s.rowid = $socid";
$sql .= " ORDER BY s.tms DESC";
Expand Down
10 changes: 5 additions & 5 deletions htdocs/modulebuilder/template/myobject_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@

$sqlfields = $sql; // $sql fields to remove for count total

$sql .= " FROM ".MAIN_DB_PREFIX.$object->table_element." as t";
//$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."anothertable as rc ON rc.parent = t.rowid";
$sql .= " FROM ".$db->prefix().$object->table_element." as t";
//$sql .= " LEFT JOIN ".$db->prefix()."anothertable as rc ON rc.parent = t.rowid";
if (isset($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (t.rowid = ef.fk_object)";
$sql .= " LEFT JOIN ".$db->prefix().$object->table_element."_extrafields as ef on (t.rowid = ef.fk_object)";
}
// Add table from hooks
$parameters = array();
Expand Down Expand Up @@ -378,9 +378,9 @@
// Search on sale representative
if ($search_sale && $search_sale != '-1') {
if ($search_sale == -2) {
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".$db->prefix()."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
} elseif ($search_sale > 0) {
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
$sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".$db->prefix()."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
}
}
// Search on socid
Expand Down
2 changes: 1 addition & 1 deletion htdocs/modulebuilder/template/scripts/mymodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
// An example of a direct SQL read without using the fetch method
/*
$sql = "SELECT field1, field2";
$sql.= " FROM ".MAIN_DB_PREFIX."myobject";
$sql.= " FROM ".$db->prefix()."myobject";
$sql.= " WHERE field3 = 'xxx'";
$sql.= " ORDER BY field1 ASC";
Expand Down

0 comments on commit 01be602

Please sign in to comment.