|
@@ -49,6 +49,9 @@ protected static $_maps = [];
|
|
|
protected static $_map_val = [];
|
|
|
protected static $_map_fct = [];
|
|
|
|
|
|
+protected static $_p_map_pk = [];
|
|
|
+protected static $_d_map_pk = [];
|
|
|
+
|
|
|
protected static function __init_params()
|
|
|
{
|
|
|
if (empty(static::$_name))
|
|
@@ -72,6 +75,8 @@ protected static function __init_params()
|
|
|
$sql = 'SELECT *
|
|
|
FROM `_map`
|
|
|
WHERE `model_id`='.static::$_id;
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
$q = $db->query($sql);
|
|
|
$maps_ids = [];
|
|
|
static::$_maps = [];
|
|
@@ -82,12 +87,15 @@ protected static function __init_params()
|
|
|
);
|
|
|
$maps_ids[] = $row['id'];
|
|
|
}
|
|
|
+ //var_dump(static::$_maps);
|
|
|
|
|
|
// Tables PRESTA
|
|
|
|
|
|
$sql = 'SELECT *
|
|
|
FROM `_map_p_tables`
|
|
|
WHERE `map_id` IN ('.implode(', ', $maps_ids).')';
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
$q = $db->query($sql);
|
|
|
//var_dump($q); die();
|
|
|
while($row=$q->fetch_assoc()) {
|
|
@@ -109,6 +117,8 @@ protected static function __init_params()
|
|
|
$sql = 'SELECT *
|
|
|
FROM `_map_d_tables`
|
|
|
WHERE `map_id` IN ('.implode(', ', $maps_ids).')';
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
$q = $db->query($sql);
|
|
|
//var_dump($q); die();
|
|
|
while($row=$q->fetch_assoc()) {
|
|
@@ -242,12 +252,18 @@ public static function _maps()
|
|
|
return static::$_maps;
|
|
|
}
|
|
|
|
|
|
+public static function _mapbyobject($type, $otype, $oid=null)
|
|
|
+{
|
|
|
+ return static::_mapbyotype($type, $otype);
|
|
|
+}
|
|
|
public static function _mapbyotype($type, $otype)
|
|
|
{
|
|
|
+ //var_dump(static::$_maps); die();
|
|
|
foreach(static::$_maps as $map_id=>$map) {
|
|
|
//var_dump($map);
|
|
|
- if ($map[$type.'t_main']==$otype)
|
|
|
+ if ($map[$type.'t_main']==$otype) {
|
|
|
return $map;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -278,51 +294,55 @@ public static function _schema_check($update=false)
|
|
|
foreach(static::${'_'.$type.'t'} as $tablename=>$table) {
|
|
|
$classname = 'Table\\'.$type.'_field';
|
|
|
$schema_web = static::_ws_action($type, 'schema_fields', ['tablename'=>$tablename]);
|
|
|
- $schema = $classname::_c_data_get($tablename);
|
|
|
- $diff = static::_data_diff_rows($schema, $schema_web);
|
|
|
- //echo '<p>'.$tablename.'</p>'; var_dump($diff); var_dump($schema_web);
|
|
|
-
|
|
|
- if (!empty($diff)) {
|
|
|
- $tdiff[] = 'Champs sur '.$type.'.'.$tablename;
|
|
|
- //var_dump($diff);
|
|
|
- foreach($diff as $fieldname=>$field) {
|
|
|
- if (!empty($field)) {
|
|
|
- //var_dump($field);
|
|
|
- if ($update) {
|
|
|
- // Insert
|
|
|
- if (empty($field[0]))
|
|
|
- $classname::_c_insert($tablename, $field[1]);
|
|
|
- // Delete
|
|
|
- elseif (empty($field[1]))
|
|
|
- $classname::_c_delete($tablename, $field[0]);
|
|
|
- // Update
|
|
|
- else
|
|
|
- $classname::_c_update($tablename, $field[1]);
|
|
|
+ if (!is_null($schema_web)) {
|
|
|
+ $schema = $classname::_c_data_get($tablename);
|
|
|
+ $diff = static::_data_diff_rows($schema, $schema_web);
|
|
|
+ //echo '<p>'.$tablename.'</p>'; var_dump($diff); var_dump($schema_web);
|
|
|
+
|
|
|
+ if (!empty($diff)) {
|
|
|
+ $tdiff[] = 'Champs sur '.$type.'.'.$tablename;
|
|
|
+ //var_dump($diff);
|
|
|
+ foreach($diff as $fieldname=>$field) {
|
|
|
+ if (!empty($field)) {
|
|
|
+ //var_dump($field);
|
|
|
+ if ($update) {
|
|
|
+ // Insert
|
|
|
+ if (empty($field[0]))
|
|
|
+ $classname::_c_insert($tablename, $field[1]);
|
|
|
+ // Delete
|
|
|
+ elseif (empty($field[1]))
|
|
|
+ $classname::_c_delete($tablename, $field[0]);
|
|
|
+ // Update
|
|
|
+ else
|
|
|
+ $classname::_c_update($tablename, $field[1]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$index_web = static::_ws_action($type, 'schema_index', ['tablename'=>$tablename]);
|
|
|
- $index = $classname::_i_data_get($tablename);
|
|
|
- $diff = static::_data_diff_rows($index, $index_web);
|
|
|
- //echo '<p>'.$tablename.'</p>'; var_dump($diff); //var_dump($index_web);
|
|
|
- if (!empty($diff)) {
|
|
|
- $tdiff[] = 'Index sur '.$type.'.'.$tablename;
|
|
|
- //var_dump($diff);
|
|
|
- foreach($diff as $fieldname=>$field) {
|
|
|
- if (!empty($field)) {
|
|
|
- //var_dump($field);
|
|
|
- if ($update) {
|
|
|
- // Insert
|
|
|
- if (empty($field[0]))
|
|
|
- $classname::_i_insert($tablename, $field[1]);
|
|
|
- // Delete
|
|
|
- elseif (empty($field[1]))
|
|
|
- $classname::_i_delete($tablename, $field[0]);
|
|
|
- // Update
|
|
|
- else
|
|
|
- $classname::_i_update($tablename, $field[1]);
|
|
|
+ if (!is_null($index_web)) {
|
|
|
+ $index = $classname::_i_data_get($tablename);
|
|
|
+ $diff = static::_data_diff_rows($index, $index_web);
|
|
|
+ //echo '<p>'.$tablename.'</p>'; var_dump($diff); //var_dump($index_web);
|
|
|
+ if (!empty($diff)) {
|
|
|
+ $tdiff[] = 'Index sur '.$type.'.'.$tablename;
|
|
|
+ //var_dump($diff);
|
|
|
+ foreach($diff as $fieldname=>$field) {
|
|
|
+ if (!empty($field)) {
|
|
|
+ //var_dump($field);
|
|
|
+ if ($update) {
|
|
|
+ // Insert
|
|
|
+ if (empty($field[0]))
|
|
|
+ $classname::_i_insert($tablename, $field[1]);
|
|
|
+ // Delete
|
|
|
+ elseif (empty($field[1]))
|
|
|
+ $classname::_i_delete($tablename, $field[0]);
|
|
|
+ // Update
|
|
|
+ else
|
|
|
+ $classname::_i_update($tablename, $field[1]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -484,6 +504,8 @@ public static function _d_list($params=null)
|
|
|
$db = static::$__db;
|
|
|
|
|
|
$sql = static::_d_list_sql($params);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
|
|
|
$l = [];
|
|
|
$q = $db->query($sql);
|
|
@@ -574,25 +596,60 @@ public static function _getbyref($ref)
|
|
|
|
|
|
return $o;
|
|
|
}
|
|
|
-public static function _getbyoid($map_id, $type, $otype, $oid)
|
|
|
+public static function _getidbyref($ref)
|
|
|
{
|
|
|
static::__log(__METHOD__);
|
|
|
$db = static::$__db;
|
|
|
|
|
|
- $sql = 'SELECT ot.id
|
|
|
- FROM _objects_'.$type.' ot
|
|
|
- INNER JOIN _objects o ON o.id=ot.id
|
|
|
+ $sql = 'SELECT o.id
|
|
|
+ FROM _objects o
|
|
|
+ WHERE o.ref="'.$ref.'"';
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
+ if (!($q = $db->query($sql))) {
|
|
|
+ var_dump('COUILLE OID');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (list($id)=$q->fetch_row())
|
|
|
+ return $id;
|
|
|
+}
|
|
|
+public static function _getidbyoid($map_id, $type, $otype, $oid)
|
|
|
+{
|
|
|
+ static::__log(__METHOD__);
|
|
|
+ $db = static::$__db;
|
|
|
+
|
|
|
+ $sql = 'SELECT o.id
|
|
|
+ FROM _objects o
|
|
|
+ INNER JOIN _objects_'.$type.' ot ON ot.id=o.id
|
|
|
INNER JOIN _'.$type.'_tables t ON t.id=ot.tid
|
|
|
- WHERE o.map_id='.$map_id.'
|
|
|
- AND ot.oid='.$oid.'
|
|
|
+ WHERE ot.oid='.$oid.'
|
|
|
AND t.ref="'.$otype.'"';
|
|
|
+ // @todo verif que map_id est bien inutile dans tous les cas de figure!
|
|
|
+ // AND o.map_id='.$map_id.'
|
|
|
if (DEBUG_SQL)
|
|
|
var_dump($sql);
|
|
|
- $q = $db->query($sql);
|
|
|
+ if (!($q = $db->query($sql))) {
|
|
|
+ var_dump('COUILLE OID');
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
if (list($id)=$q->fetch_row())
|
|
|
return $id;
|
|
|
}
|
|
|
+public static function _existsbyoid($map_id, $type, $otype, $oid)
|
|
|
+{
|
|
|
+ return is_numeric(static::_getidbyoid($map_id, $type, $otype, $oid));
|
|
|
+}
|
|
|
+public static function _getbyoid($map_id, $type, $otype, $oid)
|
|
|
+{
|
|
|
+ $id = static::_getidbyoid($map_id, $type, $otype, $oid);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($id);
|
|
|
+ if ($id)
|
|
|
+ return static::_get($id);
|
|
|
+ //var_dump($o); die();
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* Récupère Un objet
|
|
@@ -657,6 +714,7 @@ public static function _get_all_d(&$o)
|
|
|
|
|
|
$o[$type.'_tree'] = static::_get_data_tree($map_id, $type, $t_main, $oid);
|
|
|
$o[$type.'_tree_web'] = static::_ws_model($type, $map_id, $oid);
|
|
|
+ //var_dump($o[$type.'_tree_web']); die();
|
|
|
}
|
|
|
/**
|
|
|
* ATTENTION! : params doit être construit en amont
|
|
@@ -702,7 +760,7 @@ public static function _get_data_tree_sub($map, $type, $data, $row)
|
|
|
{
|
|
|
$data['row'] = $row;
|
|
|
// récursion enfants
|
|
|
- if (!empty($map['children'])) foreach($map['children'] as $tname=>$t) {
|
|
|
+ if (!is_null($row) && !empty($map['children'])) foreach($map['children'] as $tname=>$t) {
|
|
|
//$table = static::${'_'.$type.'t'}[$tname];
|
|
|
//echo '<p>'.$tname.'</p>'; var_dump($t);// die();
|
|
|
$params = [
|
|
@@ -745,9 +803,19 @@ public static function _ws_model($type, $map_id, $id)
|
|
|
//var_dump(static::$_maps[$map_id]);
|
|
|
return static::_ws_action($type, 'get', ['id'=>$id, 'map'=>static::$_maps[$map_id]]);
|
|
|
}
|
|
|
-public static function _ws_sql($type, $sql)
|
|
|
+public static function _ws_oexists($type, $tablename, $oid)
|
|
|
{
|
|
|
- return static::_ws_action($type, 'sql', ['sql'=>$sql]);
|
|
|
+ //var_dump(static::$_maps[$map_id]);
|
|
|
+ return static::_ws_action($type, 'exists', ['tablename'=>$tablename, 'id'=>$oid]);
|
|
|
+}
|
|
|
+public static function _ws_oexists_ref($type, $tablename, $ref)
|
|
|
+{
|
|
|
+ //var_dump(static::$_maps[$map_id]);
|
|
|
+ return static::_ws_action($type, 'exists_ref', ['tablename'=>$tablename, 'ref'=>$ref]);
|
|
|
+}
|
|
|
+public static function _ws_sql($type, $sql_type, $sql)
|
|
|
+{
|
|
|
+ return static::_ws_action($type, 'sql', ['type'=>$sql_type, 'sql'=>$sql]);
|
|
|
}
|
|
|
/**
|
|
|
* Webservice action
|
|
@@ -762,6 +830,9 @@ public static function _ws_action($type, $action, $params=null)
|
|
|
public static function _ws_url($type, $url, $post=null)
|
|
|
{
|
|
|
$url = static::${'__'.$type.'_params'}['ws_url'].$url;
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_WS) {
|
|
|
+ var_dump($url);
|
|
|
+ }
|
|
|
|
|
|
$timeout = 30;
|
|
|
$ch = curl_init($url); // initialize curl with given url
|
|
@@ -775,9 +846,8 @@ public static function _ws_url($type, $url, $post=null)
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
|
|
|
}
|
|
|
$raw_data = @curl_exec($ch);
|
|
|
- //var_dump($raw_data);
|
|
|
$data = json_decode($raw_data, true);
|
|
|
- //echo $url; var_dump($data);
|
|
|
+ //var_dump($url); var_dump($post); var_dump($raw_data); var_dump($data);
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
@@ -786,42 +856,65 @@ public static function _ws_url($type, $url, $post=null)
|
|
|
|
|
|
/**
|
|
|
* Requête SQL depuis Tree sur un set de données avec un mapping donné
|
|
|
- * Fonctionne bien pour une synchronisation, moins bien pour un mapping, mais on va s'arranger!
|
|
|
+ * @todo Fonctionne bien pour une synchronisation, moins bien pour un mapping, mais on va s'arranger!
|
|
|
+ *
|
|
|
+ * @param $map Mapping concerné
|
|
|
+ * @param $type p/d
|
|
|
+ * @param $tree Data locale
|
|
|
+ * @param $tree_web Data prod distante
|
|
|
+ * @param $tree_new Data nouvelle (à mettre à jour vers distant)
|
|
|
*/
|
|
|
public static function _sql_update($map, $type, $tree, $tree_web, $tree_new=null)
|
|
|
{
|
|
|
$db = static::$__db;
|
|
|
+ //trigger_error('mm');
|
|
|
|
|
|
+ // On synchronise le distant par màj
|
|
|
if (isset($tree_new)) {
|
|
|
$tree_orig = $tree_new;
|
|
|
$tree_dest = $tree_web;
|
|
|
$dest = 'ws';
|
|
|
}
|
|
|
+ // On synchronise le local avec le distant
|
|
|
else {
|
|
|
$tree_orig = $tree_web;
|
|
|
$tree_dest = $tree;
|
|
|
$dest = 'local';
|
|
|
}
|
|
|
|
|
|
- //var_dump($map); die();
|
|
|
$tablename = $map['name'];
|
|
|
- if (DEBUG_SYNCHRO) {
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL) {
|
|
|
echo '<p>_sql_update : '.$tablename.'</p>';
|
|
|
+ var_dump($map);
|
|
|
+ var_dump($tree_orig);
|
|
|
+ var_dump($tree_dest);
|
|
|
+ var_dump($dest);
|
|
|
+ //die();
|
|
|
}
|
|
|
$table = static::${"_".$type."t"}[$tablename];
|
|
|
|
|
|
if (empty($tree_dest['row'])) {
|
|
|
// Création depuis distant
|
|
|
if (!empty($tree_orig['row'])) {
|
|
|
- $sql = $table->data_insert_sql($tree_orig['row']);
|
|
|
+ $sql = $table->data_insert_sql($tree_orig['row'], $tree_orig['params']);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
if ($dest=='local') {
|
|
|
$q = $db->query($sql);
|
|
|
- var_dump($q);
|
|
|
- $r = ['response'=>true, 'id'=>$db->insert_id];
|
|
|
+ //var_dump($q);
|
|
|
+ $id = $db->insert_id;
|
|
|
+ $sql_notif = 'INSERT INTO _'.$type.'_log (ts, tid, oid, rev, action, fields, details) VALUES (NOW(), '.$map['tid'].', '.$id.', 0, "c", "'.addslashes(json_encode(array_keys($tree_orig['row']))).'", "'.addslashes(json_encode($tree_orig['row'])).'")';
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql_notif);
|
|
|
+ $q = $db->query($sql_notif);
|
|
|
+ $r = ['response'=>!empty($id), 'id'=>$id];
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
}
|
|
|
elseif ($dest=='ws') {
|
|
|
- $r = static::_ws_sql($type, $sql);
|
|
|
- var_dump($r);
|
|
|
+ $r = static::_ws_sql($type, 'c', $sql);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -830,12 +923,23 @@ public static function _sql_update($map, $type, $tree, $tree_web, $tree_new=null
|
|
|
// Suppression du local
|
|
|
if (!empty($tree_dest['row'])) {
|
|
|
$sql = $table->data_delete_sql($tree_dest['params']);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
if ($dest=='local') {
|
|
|
$q = $db->query($sql);
|
|
|
- $r = ['response'=>true];
|
|
|
+ $affected_rows = $db->affected_rows;
|
|
|
+ $sql_notif = 'INSERT INTO _'.$type.'_log (ts, tid, oid, rev, action) VALUES (NOW(), '.$map['tid'].', '.$tree_dest['params'].', 0, "d")';
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql_notif);
|
|
|
+ $q = $db->query($sql_notif);
|
|
|
+ $r = ['response'=>$affected_rows>0];
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
}
|
|
|
elseif ($dest=='ws') {
|
|
|
- $r = static::_ws_sql($type, $sql);
|
|
|
+ $r = static::_ws_sql($type, 'd', $sql);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -845,34 +949,65 @@ public static function _sql_update($map, $type, $tree, $tree_web, $tree_new=null
|
|
|
//var_dump($diff);
|
|
|
if (!empty($diff)) {
|
|
|
$sql = $table->data_update_sql($diff, $tree_orig['params']);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql);
|
|
|
if ($dest=='local') {
|
|
|
$q = $db->query($sql);
|
|
|
- $r = ['response'=>true];
|
|
|
+ $affected_rows = $db->affected_rows;
|
|
|
+ if (is_numeric($tree_orig['params'])) {
|
|
|
+ $sql_notif = 'INSERT INTO _'.$type.'_log (ts, tid, oid, rev, action, fields, details) VALUES (NOW(), '.$map['tid'].', '.$tree_orig['params'].', 0, "u", "'.addslashes(json_encode(array_keys($diff))).'", "'.addslashes(json_encode($diff)).'")';
|
|
|
+ $q = $db->query($sql_notif);
|
|
|
+ if (DEBUG_SQL)
|
|
|
+ var_dump($sql_notif);
|
|
|
+ }
|
|
|
+ $r = ['response'=>$affected_rows>0];
|
|
|
}
|
|
|
elseif ($dest=='ws') {
|
|
|
- $r = static::_ws_sql($type, $sql);
|
|
|
+ $r = static::_ws_sql($type, 'u', $sql);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // BIM
|
|
|
- if (!empty($sql)) {
|
|
|
- if (DEBUG_SQL)
|
|
|
- var_dump($sql);
|
|
|
- }
|
|
|
-
|
|
|
// On va potentiellement utiliser par la suite des id sur les objets nouvellement créés !
|
|
|
|
|
|
//var_dump($map['children']); die();
|
|
|
if (isset($map['children'])) foreach($map['children'] as $tablename=>$tmap) {
|
|
|
- //echo '<p>'.$tablename.'</p>'; //var_dump($tree['children'][$tablename]); die();
|
|
|
- static::_sql_update(
|
|
|
- $tmap,
|
|
|
- $type,
|
|
|
- isset($tree['children'][$tablename]) ?$tree['children'][$tablename] :null,
|
|
|
- isset($tree_web['children'][$tablename]) ?$tree_web['children'][$tablename] :null,
|
|
|
- isset($tree_new['children'][$tablename]) ?$tree_new['children'][$tablename] :null
|
|
|
- );
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ echo '<p>'.$tablename.'</p>';
|
|
|
+ //var_dump($tree['children'][$tablename]);
|
|
|
+ var_dump($tmap);
|
|
|
+ //die();
|
|
|
+ }
|
|
|
+ // Row simple
|
|
|
+ if ($tmap['join_type']=='row') {
|
|
|
+ static::_sql_update(
|
|
|
+ $tmap,
|
|
|
+ $type,
|
|
|
+ isset($tree['children'][$tablename]) ?$tree['children'][$tablename] :null,
|
|
|
+ isset($tree_web['children'][$tablename]) ?$tree_web['children'][$tablename] :null,
|
|
|
+ isset($tree_new['children'][$tablename]) ?$tree_new['children'][$tablename] :null
|
|
|
+ );
|
|
|
+ }
|
|
|
+ // Liste
|
|
|
+ else {//if ($tmap['join_type']=='list') {
|
|
|
+ // @todo key mapping des listes... chaud patate!
|
|
|
+ //foreach($row as $key=>$row)
|
|
|
+ if (empty($tree_new)) {
|
|
|
+ if (!empty($tree_web['children'][$tablename])) {
|
|
|
+ foreach($tree_web['children'][$tablename] as $key=>$row) {
|
|
|
+ static::_sql_update(
|
|
|
+ $tmap,
|
|
|
+ $type,
|
|
|
+ isset($tree['children'][$tablename][$key]) ?$tree['children'][$tablename][$key] :null,
|
|
|
+ $row,
|
|
|
+ null
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return isset($r) ?$r :null;
|
|
@@ -967,7 +1102,9 @@ public static function _pd_update(&$o, &$update) //, $diff
|
|
|
$type = 'd';
|
|
|
|
|
|
$r = static::_sql_update($map[$type.'t'][$map[$type.'t_main']], $type, $o[$type.'_tree'], $o[$type.'_tree_web'], $update['tree']);
|
|
|
- var_dump($r);
|
|
|
+ if (DEBUG_SQL || DEBUG_SYNCHRO) {
|
|
|
+ var_dump($r);
|
|
|
+ }
|
|
|
|
|
|
return $r;
|
|
|
}
|
|
@@ -983,12 +1120,14 @@ public static function _dp_update(&$o, &$update) //, $diff
|
|
|
$type = 'p';
|
|
|
|
|
|
$r = static::_sql_update($map[$type.'t'][$map[$type.'t_main']], $type, $o[$type.'_tree'], $o[$type.'_tree_web'], $update['tree']);
|
|
|
- var_dump($r);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
|
|
|
return $r;
|
|
|
|
|
|
$sqls = static::_diff_sql($o['map_id'], 'p', $diff);
|
|
|
- var_dump($sqls);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($sqls);
|
|
|
return $sqls;
|
|
|
}
|
|
|
|
|
@@ -1030,7 +1169,8 @@ public static function _sync($sens, $id)
|
|
|
if (!empty($r['id']))
|
|
|
static::_t_assoc($dest, $id, $update['tid'], $r['id']);
|
|
|
|
|
|
- var_dump($r);
|
|
|
+ if (DEBUG_SYNCHRO || DEBUG_SQL)
|
|
|
+ var_dump($r);
|
|
|
|
|
|
// Synchronisation destination après miseà jour
|
|
|
$fct = '_'.$dest.'_update';
|
|
@@ -1073,6 +1213,21 @@ public static function _map_val_set($type)
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+public static function _map_fk($type, $modelname, $map_id, $tablename, $oid)
|
|
|
+{
|
|
|
+ $orig = $type;
|
|
|
+ $dest = $orig=='p' ?'d' :'p';
|
|
|
+ require_once MODEL_PATH.'/'.$modelname.'.php';
|
|
|
+ $classname = '\\Model\\'.$modelname;
|
|
|
+ $o = $classname::_getbyoid($map_id, $orig, $tablename, $oid);
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ var_dump("$classname::_getbyoid($map_id, '$orig', $tablename, $oid)");
|
|
|
+ var_dump($o);
|
|
|
+ }
|
|
|
+ if (!empty($o[$dest]['oid']))
|
|
|
+ return $o[$dest]['oid'];
|
|
|
+}
|
|
|
+
|
|
|
// @todo mettre ailleurs, c'est un helper bien plus général
|
|
|
public static function _array_merge_recursive(&$map, $tmap)
|
|
|
{
|
|
@@ -1138,6 +1293,12 @@ protected static $_tmap = [
|
|
|
'product_lang' => [
|
|
|
'name' => 'product_lang',
|
|
|
],
|
|
|
+ // Association fournisseur/produit avec prix fournisseur
|
|
|
+ 'product_supplier' => [
|
|
|
+ 'name' => 'product_supplier',
|
|
|
+ 'map_main' => 'product_fournisseur_price',
|
|
|
+ 'map_type' => 'list', // mapping list=>list
|
|
|
+ ],
|
|
|
],
|
|
|
],
|
|
|
],
|
|
@@ -1163,6 +1324,13 @@ protected static $_tmap = [
|
|
|
// Quelles infos à utiliser ?
|
|
|
'map_main' => 'supplier', // main/main en fait
|
|
|
'map_join' => ['supplier_lang', 'supplier_shop'],
|
|
|
+ 'children' => [
|
|
|
+ // Adresse client
|
|
|
+ 'socpeople' => [
|
|
|
+ 'name' => 'socpeople',
|
|
|
+ 'map_main' => 'address',
|
|
|
+ ],
|
|
|
+ ],
|
|
|
],
|
|
|
'pt' => [
|
|
|
'name' => 'supplier',
|
|
@@ -1179,6 +1347,12 @@ protected static $_tmap = [
|
|
|
'supplier_lang' => [
|
|
|
'name' => 'supplier_lang',
|
|
|
],
|
|
|
+ // Adresses
|
|
|
+ 'address' => [
|
|
|
+ 'name' => 'address',
|
|
|
+ 'map_type' => 'list', // mapping list=>list
|
|
|
+ 'map_main' => 'socpeople',
|
|
|
+ ],
|
|
|
],
|
|
|
],
|
|
|
],
|
|
@@ -1205,11 +1379,42 @@ protected static $_tmap = [
|
|
|
// Adresse client
|
|
|
'address' => [
|
|
|
'name' => 'address',
|
|
|
+ 'map_type' => 'list', // mapping list=>list
|
|
|
'map_main' => 'socpeople',
|
|
|
],
|
|
|
],
|
|
|
],
|
|
|
],
|
|
|
+ 8 => [ // Commande / Order
|
|
|
+ 'dt' => [
|
|
|
+ 'name' => 'commande',
|
|
|
+ // Quelles infos à utiliser ?
|
|
|
+ 'map_main' => 'orders', // main/main en fait
|
|
|
+ 'children' => [
|
|
|
+ // Adresse client
|
|
|
+ 'commandedet' => [
|
|
|
+ 'name' => 'commandedet',
|
|
|
+ 'map_type' => 'list', // mapping list=>list
|
|
|
+ 'map_main' => 'order_detail',
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ 'pt' => [
|
|
|
+ 'name' => 'orders',
|
|
|
+ // Quelles infos à utiliser ?
|
|
|
+ 'map_main' => 'commande', // main/main en fait
|
|
|
+ //'map_join' => ['supplier_lang', 'supplier_shop'],
|
|
|
+ // Construction des champs enfant
|
|
|
+ 'children' => [
|
|
|
+ // Adresse client
|
|
|
+ 'order_detail' => [
|
|
|
+ 'name' => 'order_detail',
|
|
|
+ 'map_type' => 'list', // mapping list=>list
|
|
|
+ 'map_main' => 'commandedet',
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
];
|
|
|
|
|
|
/**
|
|
@@ -1322,7 +1527,7 @@ public static function _map_data_tree($sens, $map_dest, $tree_dest, $tree_orig,
|
|
|
|
|
|
$tablename = $map_dest['name'];
|
|
|
|
|
|
- //var_dump($tree_orig); die();
|
|
|
+ //var_dump($tablename); var_dump($tree_orig); die();
|
|
|
|
|
|
// Construction/Récupération données de base
|
|
|
$datainit = [
|
|
@@ -1332,6 +1537,12 @@ public static function _map_data_tree($sens, $map_dest, $tree_dest, $tree_orig,
|
|
|
'children' => [],
|
|
|
];
|
|
|
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ echo '<p>Données sources (data_from) '.$tablename.' :</p>';
|
|
|
+ var_dump($map_dest);
|
|
|
+ //die();
|
|
|
+ }
|
|
|
+
|
|
|
// Une ligne de donnée
|
|
|
if ($map_dest['join_type']=='row') {
|
|
|
// construction/augmentation $data_from
|
|
@@ -1344,7 +1555,10 @@ public static function _map_data_tree($sens, $map_dest, $tree_dest, $tree_orig,
|
|
|
$data_from[$tablename_new] = $tree_orig['children'][$tablename_new]['row'];
|
|
|
}
|
|
|
if (DEBUG_SYNCHRO) {
|
|
|
- echo '<p>Données sources (data_from) '.$tablename.' :</p>'; var_dump($data_from); var_dump($tree_orig); //die();
|
|
|
+ echo '<p>ROW:Données sources (data_from) '.$tablename.' :</p>';
|
|
|
+ var_dump($data_from);
|
|
|
+ var_dump($tree_orig);
|
|
|
+ //die();
|
|
|
}
|
|
|
|
|
|
//echo '<p>'.$tablename.'</p>';
|
|
@@ -1360,13 +1574,26 @@ public static function _map_data_tree($sens, $map_dest, $tree_dest, $tree_orig,
|
|
|
// construction/augmentation $data_from
|
|
|
foreach($tree_orig as $key=>$tree_data) {
|
|
|
$data_from_sub = array_merge($data_from, [$map_dest['map_main'] => $tree_data['row']]);
|
|
|
+ // Sous-tables de la table de liaison
|
|
|
+ // @todo
|
|
|
if (false && !empty($map_dest['map_join'])) foreach($map_dest['map_join'] as $tablename_new) {
|
|
|
if (isset($tree_orig['children'][$tablename_new]))
|
|
|
$data_from_sub[$tablename_new] = $tree_orig['children'][$tablename_new]['row'];
|
|
|
}
|
|
|
+
|
|
|
$row = static::_map_table_row($sens, $tablename, $map_dest, $tree_dest, $data_from_sub);
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ echo '<p>LIST:Données sources (data_from) '.$tablename.' :</p>';
|
|
|
+ var_dump($key);
|
|
|
+ var_dump($data_from);
|
|
|
+ var_dump($data_from_sub);
|
|
|
+ var_dump($tree_data);
|
|
|
+ var_dump($row);
|
|
|
+ //die();
|
|
|
+ }
|
|
|
+ //$row = [];
|
|
|
|
|
|
- //@todo : map key
|
|
|
+ //@todo : map key selon row ? chaud patate...
|
|
|
$key_mapped = $key;
|
|
|
//$rows = static::${'_'.$type.'t'}[$tablename]->select($params);
|
|
|
$datas[$key_mapped] = static::_map_data_tree_sub($sens, $map_dest, $tree_dest, $tree_data, $data_from_sub, $datainit, $row);
|
|
@@ -1377,6 +1604,14 @@ public static function _map_data_tree($sens, $map_dest, $tree_dest, $tree_orig,
|
|
|
/**
|
|
|
* Sous-requête
|
|
|
* Juste histoire de factoriser et simplifier le code
|
|
|
+ *
|
|
|
+ * @param $sens
|
|
|
+ * @param $map_dest mapping de l'objet à construire
|
|
|
+ * @param $tree_dest arbre de donnée final/destination à titre de repère/comparaison
|
|
|
+ * @param $tree_orig arbre de donnée source/origine
|
|
|
+ * @param $data_from tableau de données source/origine
|
|
|
+ * @param $data donnée qu'on va modifier/construire et renvoyer
|
|
|
+ * @param $row ligne de donnée de l'objet en cours
|
|
|
*/
|
|
|
public static function _map_data_tree_sub($sens, $map_dest, $tree_dest, $tree_orig, $data_from, &$data, $row)
|
|
|
{
|
|
@@ -1385,19 +1620,20 @@ public static function _map_data_tree_sub($sens, $map_dest, $tree_dest, $tree_or
|
|
|
|
|
|
$data['row'] = $row;
|
|
|
// récursion enfants
|
|
|
- if (!empty($map_dest['children'])) foreach($map_dest['children'] as $tablename=>$map_dest_child) {
|
|
|
- if (DEBUG_SYNCHRO) {
|
|
|
- var_dump($map_dest); echo '<p>'.$tablename.'</p>'; var_dump($map_dest_child); //die();
|
|
|
- }
|
|
|
+ if (!is_null($row) && !empty($map_dest['children'])) foreach($map_dest['children'] as $tablename=>$map_dest_child) {
|
|
|
$vname = $map_dest_child['join']['from_fieldname'];
|
|
|
- //var_dump($map_dest_child['join']['fieldname']); var_dump($vname); die();
|
|
|
|
|
|
- //echo '<p>tree dest '.$tablename.'</p>'; var_dump($tree_dest['children'][$tablename]); //die();
|
|
|
$tree_dest_child = isset($tree_dest['children'][$tablename]) ?$tree_dest['children'][$tablename] :[];
|
|
|
- //var_dump($tree_dest_child); die();
|
|
|
|
|
|
if (DEBUG_SYNCHRO) {
|
|
|
- var_dump($tree_dest);
|
|
|
+ var_dump($map_dest);
|
|
|
+ echo '<p>'.$tablename.'</p>';
|
|
|
+ var_dump($map_dest_child);
|
|
|
+ var_dump($map_dest_child['join']['fieldname']);
|
|
|
+ var_dump($vname);
|
|
|
+ echo '<p>tree dest '.$tablename.'</p>';
|
|
|
+ var_dump($tree_dest_child);
|
|
|
+ //die();
|
|
|
}
|
|
|
// Passage/Mapping de paramètre pour la mise à jour
|
|
|
// Identique à l'objet existant
|
|
@@ -1431,7 +1667,10 @@ public static function _map_data_tree_sub($sens, $map_dest, $tree_dest, $tree_or
|
|
|
$tree_orig_child = $tree_orig;
|
|
|
}
|
|
|
else {//if ($map_dest_child['join_type']=='list') {
|
|
|
- //var_dump($tree_orig['children'][$map_dest_child['map_main']]); var_dump($map_dest_child['map_main']); die();
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ var_dump(isset($map_dest_child['map_main']));
|
|
|
+ //die();
|
|
|
+ }
|
|
|
if (!isset($map_dest_child['map_main']))
|
|
|
continue;
|
|
|
//var_dump($tree_orig['children']);
|
|
@@ -1452,15 +1691,28 @@ public static function _map_data_tree_sub($sens, $map_dest, $tree_dest, $tree_or
|
|
|
* - valeur par défaut
|
|
|
* - récupération/modification d'une valeur
|
|
|
* - récupération/mappage d'une clé à partir de la table de mappage de clés
|
|
|
+ *
|
|
|
+ * @param $sens
|
|
|
+ * @param $tablename
|
|
|
+ * @param $map_dest Mapping que l'on construit
|
|
|
+ * @param $tree_dest Arbre de donnée actuel sur lequel on peut se baser/comparer
|
|
|
+ * @param $data_from Données source permettant de construre la donnée à retourner
|
|
|
*/
|
|
|
public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest, $data_from)
|
|
|
{
|
|
|
$type = $dest = ($sens=='pd') ?'d' :'p';
|
|
|
$orig = ($sens=='pd') ?'p' :'d';
|
|
|
$table = static::${'_'.$dest.'t'}[$tablename];
|
|
|
- //var_dump($tree_dest); die();
|
|
|
- //var_dump($map_dest); die();
|
|
|
+ //var_dump($tree_dest); var_dump($map_dest); die();
|
|
|
$u = [];
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ echo '<p>map table row :</p>';
|
|
|
+ var_dump($tablename);
|
|
|
+ var_dump($map_dest);
|
|
|
+ var_dump($tree_dest);
|
|
|
+ var_dump($data_from);
|
|
|
+ //var_dump($table);
|
|
|
+ }
|
|
|
//var_dump($p_diff); die();
|
|
|
//var_dump($p_data[$tablename]);
|
|
|
//var_dump($d_data); die();
|
|
@@ -1468,11 +1720,15 @@ public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest,
|
|
|
// Bouclage sur les champs présent dans la tables du modèle
|
|
|
foreach($table->map_fields($map_dest['map_id']) as $fieldname=>$field) {
|
|
|
|
|
|
- //var_dump($field); die();
|
|
|
+ if (DEBUG_SYNCHRO) {
|
|
|
+ echo '<p>map_fieldname ('.$tablename.'.'.$fieldname.')</p>';
|
|
|
+ var_dump($field);
|
|
|
+ //die();
|
|
|
+ }
|
|
|
// Si mapping update
|
|
|
if (!empty($field['update_fct'])) {
|
|
|
if (DEBUG_SYNCHRO) {
|
|
|
- echo '<p>map_fieldname ('.$tablename.'.'.$fieldname.') : '.$field['update_fct'].'</p>';
|
|
|
+ echo '<p>field:update_fct : '.$field['update_fct'].'</p>';
|
|
|
//var_dump($fieldname);
|
|
|
}
|
|
|
|
|
@@ -1494,7 +1750,7 @@ public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest,
|
|
|
}
|
|
|
}
|
|
|
// Fonctions paramétrées
|
|
|
- if ($map_tablename=='fct') {
|
|
|
+ elseif ($map_tablename=='fct') {
|
|
|
if (isset(static::$_map_fct[$dest][$map_fieldname])) {
|
|
|
$fct = '_'.$dest.'_map_'.$map_fieldname;
|
|
|
//$u[1][$fieldname] = static::$fct($data_from);
|
|
@@ -1506,6 +1762,11 @@ public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest,
|
|
|
// et utiliser le mapping associé, qui n'est pas collé à la définition du champ
|
|
|
elseif($map_tablename=='fk') {
|
|
|
// Foreign key
|
|
|
+ if (isset(static::$_map_fk[$map_fieldname])) {
|
|
|
+ $fct = '_'.$dest.'_map_fk';
|
|
|
+ //$u[1][$fieldname] = static::$fct($data_from);
|
|
|
+ $u[$fieldname] = static::$fct($map_fieldname, $data_from, $tree_dest);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
elseif(isset(static::${'_'.$orig.'t_alias'}[$map_tablename])) {
|
|
@@ -1555,7 +1816,7 @@ public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest,
|
|
|
if (false && empty($d_data)) {
|
|
|
// Gestion dépendance des données si on créé toute la chaine...
|
|
|
// Par ex, pour un produit décliné, on devrai créer dans l'ordre et utiliser par la suite :
|
|
|
- // - iproduct => id_product
|
|
|
+ // - product => id_product
|
|
|
// - product_shop <= id_product
|
|
|
// - product_lang <= id_product
|
|
|
// - product_attribute <= id_product => id_product_attribute
|
|
@@ -1569,7 +1830,7 @@ public static function _map_table_row($sens, $tablename, $map_dest, $tree_dest,
|
|
|
$u['keys'][$fieldname] = $a_data[$tablename][$fieldname];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $u;
|
|
|
}
|
|
|
|