/private/syncObjectGet.php

https://github.com/ciniki/core · PHP · 252 lines · 194 code · 19 blank · 39 comment · 78 complexity · 767d60fb6c4a306007ead908a7f14303 MD5 · raw file

  1. <?php
  2. //
  3. // Description
  4. // -----------
  5. //
  6. // Arguments
  7. // ---------
  8. //
  9. // Returns
  10. // -------
  11. //
  12. function ciniki_core_syncObjectGet($ciniki, &$sync, $tnid, $o, $args) {
  13. //
  14. // Check for custom get function
  15. //
  16. // error_log("SYNC-INFO: [$tnid] Get " . $o['oname'] . "(" . serialize($args) . ")");
  17. ciniki_core_syncLog($ciniki, 4, "Get " . $o['oname'] . "(" . serialize($args) . ")", null);
  18. if( isset($o['get']) && $o['get'] != '' ) {
  19. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectFunction');
  20. return ciniki_core_syncObjectFunction($ciniki, $sync, $tnid, $o['get'], $args);
  21. }
  22. if( isset($o['type']) && $o['type'] == 'settings' ) {
  23. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncSettingGet');
  24. return ciniki_core_syncSettingGet($ciniki, $sync, $tnid, $o, $args);
  25. }
  26. //
  27. // Check the args
  28. //
  29. if( (!isset($args['uuid']) || $args['uuid'] == '')
  30. && (!isset($args['id']) || $args['id'] == '') ) {
  31. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.231', 'msg'=>'No customer specified'));
  32. }
  33. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbQuote');
  34. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashQueryIDTree');
  35. //
  36. // Build the SQL string to get the object details along with history information
  37. //
  38. $table = $o['table'];
  39. $strsql = "SELECT $table.id, $table.uuid AS object_uuid, ";
  40. foreach($o['fields'] as $fid => $finfo) {
  41. $strsql .= "$table.$fid, ";
  42. }
  43. $strsql .= "UNIX_TIMESTAMP($table.date_added) AS date_added, "
  44. . "UNIX_TIMESTAMP($table.last_updated) AS last_updated, ";
  45. $history_table = $o['history_table'];
  46. $strsql .= "$history_table.id AS history_id, "
  47. . "$history_table.uuid AS history_uuid, "
  48. . "ciniki_users.uuid AS user_uuid, "
  49. . "$history_table.session, "
  50. . "$history_table.action, "
  51. . "$history_table.table_field, "
  52. . "$history_table.new_value, "
  53. . "UNIX_TIMESTAMP($history_table.log_date) AS log_date ";
  54. $strsql .= "FROM $table "
  55. . "LEFT JOIN $history_table ON ($table.id = $history_table.table_key "
  56. . "AND $history_table.tnid = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' "
  57. . "AND $history_table.table_name = '$table' "
  58. . ") "
  59. . "LEFT JOIN ciniki_users ON ($history_table.user_id = ciniki_users.id) ";
  60. $strsql .= "WHERE $table.tnid = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' ";
  61. if( isset($args['uuid']) && $args['uuid'] != '' ) {
  62. $strsql .= "AND $table.uuid = '" . ciniki_core_dbQuote($ciniki, $args['uuid']) . "' ";
  63. } elseif( isset($args['id']) && $args['id'] != '' ) {
  64. $strsql .= "AND $table.id = '" . ciniki_core_dbQuote($ciniki, $args['id']) . "' ";
  65. } else {
  66. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.232', 'msg'=>'No ' . $o['name'] . ' specified'));
  67. }
  68. $strsql .= "ORDER BY log_date ";
  69. $fields = array_keys($o['fields']);
  70. $fields['uuid'] = 'object_uuid';
  71. $fields['id'] = 'id';
  72. $fields['date_added'] = 'date_added';
  73. $fields['last_updated'] = 'last_updated';
  74. $rc = ciniki_core_dbHashQueryIDTree($ciniki, $strsql, $o['pmod'], array(
  75. array('container'=>'objects', 'fname'=>'object_uuid',
  76. 'fields'=>$fields),
  77. array('container'=>'history', 'fname'=>'history_uuid',
  78. 'fields'=>array('user'=>'user_uuid', 'session',
  79. 'action', 'table_field', 'new_value', 'log_date')),
  80. ));
  81. if( $rc['stat'] != 'ok' ) {
  82. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.233', 'msg'=>'Error retrieving the ' . $o['name'] . ' information', 'err'=>$rc['err']));
  83. }
  84. //
  85. // Check that one and only one row was returned
  86. //
  87. if( !isset($rc['objects']) ) {
  88. return array('stat'=>'noexist', 'err'=>array('code'=>'ciniki.core.234', 'msg'=>$o['name'] . ' does not exist'));
  89. }
  90. if( count($rc['objects']) > 1 ) {
  91. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.235', 'msg'=>$o['name'] . ' does not exist'));
  92. }
  93. $object = array_pop($rc['objects']);
  94. if( !isset($object['history']) ) {
  95. $object['history'] = array();
  96. }
  97. if( !isset($args['translate']) || $args['translate'] == 'yes' ) {
  98. //
  99. // Translate the table_key
  100. //
  101. if( isset($o['fields']) ) {
  102. foreach($o['fields'] as $fid => $finfo) {
  103. if( isset($finfo['oref']) && $finfo['oref'] != ''
  104. && isset($object[$finfo['oref']]) && $object[$finfo['oref']] != '' && $object[$finfo['oref']] != '0' ) {
  105. // oref is the name of the field that contains the object name referenced eg: ciniki.artcatalog.item
  106. $object_name = $object[$finfo['oref']];
  107. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLoad');
  108. $rc = ciniki_core_syncObjectLoad($ciniki, $sync, $tnid, $object[$finfo['oref']], array());
  109. if( $rc['stat'] != 'ok' ) {
  110. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.236', 'msg'=>'Unable to load object ' . $finfo['oref']));
  111. }
  112. $ref_o = $rc['object'];
  113. //
  114. // Only lookup if not a setting type. The settings are treated like a uuid don't need translation
  115. //
  116. if( !isset($ref_o['type']) || $ref_o['type'] != 'settings' ) {
  117. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLookup');
  118. $rc = ciniki_core_syncObjectLookup($ciniki, $sync, $tnid, $ref_o,
  119. array('local_id'=>$object[$fid]));
  120. if( $rc['stat'] != 'ok' ) {
  121. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.237', 'msg'=>'Unable to find reference for ' . $object_name . '(' . $object[$fid] . ')'));
  122. }
  123. $object[$fid] = $rc['uuid'];
  124. }
  125. }
  126. elseif( isset($finfo['ref']) && $finfo['ref'] != '' && $object[$fid] != '0' ) {
  127. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLoad');
  128. $rc = ciniki_core_syncObjectLoad($ciniki, $sync, $tnid, $finfo['ref'], array());
  129. if( $rc['stat'] != 'ok' ) {
  130. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.238', 'msg'=>'Unable to load object ' . $finfo['ref']));
  131. }
  132. $ref_o = $rc['object'];
  133. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLookup');
  134. $rc = ciniki_core_syncObjectLookup($ciniki, $sync, $tnid, $ref_o,
  135. array('local_id'=>$object[$fid]));
  136. if( $rc['stat'] != 'ok' ) {
  137. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.239', 'msg'=>'Unable to find reference for ' . $ref_o['name'] . '(' . $object[$fid] . ')'));
  138. }
  139. $object[$fid] = $rc['uuid'];
  140. }
  141. }
  142. }
  143. //
  144. // Translate the new_value if required
  145. //
  146. foreach($object['history'] as $uuid => $history) {
  147. if( isset($o['fields'][$history['table_field']]) && isset($o['fields'][$history['table_field']]['oref']) && $history['new_value'] != '0'
  148. && isset($object[$o['fields'][$history['table_field']]['oref']]) && $object[$o['fields'][$history['table_field']]['oref']] != ''
  149. ) {
  150. $ref = $object[$o['fields'][$history['table_field']]['oref']];
  151. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLoad');
  152. $rc = ciniki_core_syncObjectLoad($ciniki, $sync, $tnid, $ref, array());
  153. if( $rc['stat'] != 'ok' ) {
  154. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.240', 'msg'=>"Unable to load object $ref"));
  155. }
  156. $ref_o = $rc['object'];
  157. if( !isset($ref_o['type']) || $ref_o['type'] != 'settings' ) {
  158. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLookup');
  159. $rc = ciniki_core_syncObjectLookup($ciniki, $sync, $tnid, $ref_o,
  160. array('local_id'=>$history['new_value']));
  161. if( $rc['stat'] != 'ok' ) {
  162. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.241', 'msg'=>'Unable to find reference for ' . $ref_o['name'] . '(' . $history['new_value'] . ')', 'err'=>$rc['err']));
  163. }
  164. $object['history'][$uuid]['new_value'] = $rc['uuid'];
  165. }
  166. }
  167. elseif( isset($o['fields'][$history['table_field']]) && isset($o['fields'][$history['table_field']]['ref']) && $history['new_value'] != '0' ) {
  168. $ref = $o['fields'][$history['table_field']]['ref'];
  169. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLoad');
  170. $rc = ciniki_core_syncObjectLoad($ciniki, $sync, $tnid, $ref, array());
  171. if( $rc['stat'] != 'ok' ) {
  172. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.242', 'msg'=>"Unable to load object $ref"));
  173. }
  174. $ref_o = $rc['object'];
  175. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'syncObjectLookup');
  176. $rc = ciniki_core_syncObjectLookup($ciniki, $sync, $tnid, $ref_o,
  177. array('local_id'=>$history['new_value']));
  178. if( $rc['stat'] != 'ok' ) {
  179. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.243', 'msg'=>'Unable to find reference for ' . $ref_o['name'] . '(' . $history['new_value'] . ')', 'err'=>$rc['err']));
  180. }
  181. $object['history'][$uuid]['new_value'] = $rc['uuid'];
  182. }
  183. }
  184. }
  185. //
  186. // Check if there is a details table for this object, and get the details and history
  187. //
  188. if( isset($o['details']) && isset($o['details']['key']) && isset($o['details']['table']) ) {
  189. //
  190. // Build the SQL string to get the object details along with history information
  191. //
  192. $key = $o['details']['key'];
  193. $table = $o['details']['table'];
  194. $strsql = "SELECT $table.detail_key, $table.detail_value, "
  195. . "UNIX_TIMESTAMP($table.date_added) AS date_added, "
  196. . "UNIX_TIMESTAMP($table.last_updated) AS last_updated, ";
  197. $history_table = $o['history_table'];
  198. $strsql .= "$history_table.id AS history_id, "
  199. . "$history_table.uuid AS history_uuid, "
  200. . "ciniki_users.uuid AS user_uuid, "
  201. . "$history_table.session, "
  202. . "$history_table.action, "
  203. . "$history_table.table_field, "
  204. . "$history_table.new_value, "
  205. . "UNIX_TIMESTAMP($history_table.log_date) AS log_date ";
  206. $strsql .= "FROM $table "
  207. . "LEFT JOIN $history_table ON ($table.detail_key = $history_table.table_key "
  208. . "AND $history_table.tnid = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' "
  209. . "AND $history_table.table_name = '$table' "
  210. . ") "
  211. . "LEFT JOIN ciniki_users ON ($history_table.user_id = ciniki_users.id) ";
  212. $strsql .= "WHERE $table.tnid = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' "
  213. . "AND $table.$key = '" . ciniki_core_dbQuote($ciniki, $object['id']) . "' "
  214. . "ORDER BY log_date ";
  215. $rc = ciniki_core_dbHashQueryIDTree($ciniki, $strsql, $o['pmod'], array(
  216. array('container'=>'details', 'fname'=>'detail_key',
  217. 'fields'=>array('detail_value', 'date_added', 'last_updated')),
  218. array('container'=>'history', 'fname'=>'history_uuid',
  219. 'fields'=>array('user'=>'user_uuid', 'session',
  220. 'action', 'table_field', 'new_value', 'log_date')),
  221. ));
  222. if( $rc['stat'] != 'ok' ) {
  223. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.244', 'msg'=>'Error retrieving the ' . $o['name'] . ' information', 'err'=>$rc['err']));
  224. }
  225. if( !isset($rc['details']) ) {
  226. $object['details'] = array();
  227. } else {
  228. $object['details'] = $rc['details'];
  229. }
  230. }
  231. return array('stat'=>'ok', 'object'=>$object);
  232. }
  233. ?>