CMS  Version 3.9
DataSyncManager Class Reference

Provides a central management class for event handlers and common functionality for the data_sync component. More...

Public Member Functions

 DataSyncManager ()
 

Static Public Member Functions

static registerSyncHelper ($class, $helper)
 
static registerDataSyncAdapter ($format, $adapter)
 
static registerCSVAdapter ()
 
static getDataAdapter ($format=null)
 
static getAvailableDataAdapters ()
 
static setDefaults ()
 
static registerSerializationHandler ()
 
static getImportTabs ()
 
static getAdminImportTabs ()
 
static countRecords ($file="", $format=null)
 
static skipToOffset ($fp, $offset=-1)
 
static getHelper ($class)
 
static getImportColumns ($class, $file="", $format=null)
 
static extendForm ($form)
 
static getFieldMappings ($class, $profile_id=0)
 
static getFieldMapping ($class, $import_profile_id)
 
static fieldMappingHasMatchField ($fieldMappings)
 
static getActiveFieldMapping ($class)
 
static generateFilter ($fieldMapping)
 
static createImportItems ($class, $import_profile_id=0, $file="", $format=null)
 
static batchImportItems ($class, $import_profile_id, $file, $importMatching, $importNew, $process, $format=null)
 
static onItemRow ($item)
 
static isRowSelected ($row)
 
static getSyncTargets ()
 
static upgradeComponent ($version)
 

Static Public Attributes

static $helpers = null
 
static $totalRecords = 0
 
static $fieldMappings = array()
 
static $import_profile_id = 0
 
static $adapters = null
 

Detailed Description

Provides a central management class for event handlers and common functionality for the data_sync component.

Definition at line 181 of file data_sync_manager.inc.

Member Function Documentation

◆ batchImportItems()

static DataSyncManager::batchImportItems (   $class,
  $import_profile_id,
  $file,
  $importMatching,
  $importNew,
  $process,
  $format = null 
)
static

Definition at line 683 of file data_sync_manager.inc.

684  {
685  trace("Batching importing $class profile $import_profile_id from $file", 3);
686 
687  global $user;
688 
691 
692  if (!$process)
693  {
694  throw new FakoliException("DataSyncManager::batchImportItems cannot be used within a browser request");
695  }
696 
697  try
698  {
699  ComponentManager::fireEvent("RegisterSyncHelpers");
700 
702 
703  $columns = array();
704 
705  $process->setProgress("Running", "Scanning file", 0);
706 
707  $numRecords = DataSyncManager::countRecords($file);
708 
709  $process->setProgress("Running", pluralize("$numRecords record")." found", 0);
710 
713 
714  $matchingFields = Query::create(DataImportFieldMapping, "WHERE class=:cl AND import_profile_id=:pr AND matching=1")
715  ->bind(":cl", $class, ":pr", $import_profile_id)
716  ->execute();
717 
718  $c = 1;
719  $matchingConstraint = "";
720 
721  foreach($matchingFields as $mf)
722  {
723  if ($matchingConstraint) $matchingConstraint .= " AND ";
724  $matchingConstraint .= "{$mf->client_field}=:{$c}";
725  $c++;
726  }
727 
728  trace("Matching Constraint: $matchingConstraint", 3);
729 
730  $item = new $class();
731 
732  $fp = $adapter->openImportFile($file, $columns);
733 
734  $lastItem = null;
735 
736  $imported = 0;
737  $skipped = 0;
738 
739  $matchingQuery = Query::create($class, "WHERE $matchingConstraint");
740  $matchingQuery->filter = new InclusionFilter($item->getPrimaryKey());
741 
742  while($record = DataSyncManager::getRecord($adapter, $fp, $columns, $helper))
743  {
744 
745  $item = new $class();
746  $skip = false;
747 
748  $item->filter = $filter;
749 
750  $skip = DataSyncManager::populateItem($item, $record, $fieldMapping, $lastItem);
751 
752  if (!$skip)
753  {
754  $c = 1;
755 
756  foreach($matchingFields as $mf)
757  {
758  trace("Matching: {$mf->client_field} = ".$item->get($mf->client_field), 3);
759  $matchingQuery->bind(":{$c}", $item->get($mf->client_field));
760  $c++;
761  }
762 
763  $match = null;
764  try
765  {
766  $match = $matchingQuery->executeSingle();
767 
768  $item->set($item->getPrimaryKey(), $match->get($match->getPrimaryKey()));
769  }
770  catch(DataNotFoundException $e)
771  {
772  // Data not found
773  }
774  catch(FakoliException $fe)
775  {
776  // Ambiguous singleton, most likely
777  }
778 
779  if (($match && !$importMatching) || (!$match && !$importNew))
780  {
781  $skipped++;
782  }
783  else
784  {
785  $item->save();
786  if (method_exists($helper, 'postProcess'))
787  {
788  $helper->postProcess($item, $record);
789  }
790  $imported++;
791  $lastItem = $item;
792  }
793  }
794  else
795  {
796  $skipped++;
797  }
798 
799  $counter = $skipped + $imported;
800 
801  $percentage = intval((100 * $counter) / $numRecords);
802 
803  $process->setProgress("Running", "Processed record $counter of $numRecords", $percentage);
804 
805  }
806 
807  fclose($fp);
808 
809  $process->setProgress("Completed", "$imported records imported, $skipped records skipped", 100);
810 
811  }
812  catch(Exception $e)
813  {
814  $process->setProgress("Error", $e->getMessage(), $percentage);
815  }
816  }
$filter
Definition: update.inc:44
$file
Definition: delete.inc:47
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static getFieldMapping($class, $import_profile_id)
static generateFilter($fieldMapping)
static getHelper($class)
static getDataAdapter($format=null)
static countRecords($file="", $format=null)
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
global $user
if(!checkRole("admin")) $c
$process
Definition: run.php:54

◆ countRecords()

static DataSyncManager::countRecords (   $file = "",
  $format = null 
)
static

Definition at line 283 of file data_sync_manager.inc.

284  {
285  if (!$file)
286  {
287  $file = $_SESSION["data_import_file"];
288  }
289 
291  return $adapter->countRecords($file);
292  }
$_SESSION["useMobile"]
Definition: override.inc:7

◆ createImportItems()

static DataSyncManager::createImportItems (   $class,
  $import_profile_id = 0,
  $file = "",
  $format = null 
)
static

Definition at line 575 of file data_sync_manager.inc.

576  {
577  global $user;
578 
580 
581  $item = new $class();
582 
583  ComponentManager::fireEvent("RegisterSyncHelpers");
584 
586 
588 
589  $columns = array();
590 
593 
594  $fp = $adapter->openImportFile($file, $columns);
595 
596  $items = array();
597 
598  //DataSyncManager::skipToOffset($fp);
599  $isChunked = Settings::getValue("data_sync", "import_in_chunks");
600  $counter = Settings::getValue("data_sync", "records_per_page");
601  $offset = $isChunked ? checkNumeric($_GET["offset"]) : 0;
602 
603  $lastItem = null;
604 
605  $matchingFields = Query::create(DataImportFieldMapping, "WHERE class=:cl AND import_profile_id=:pr AND matching=1")
606  ->bind(":cl", $class, ":pr", $import_profile_id)
607  ->execute();
608 
609  $c = 1;
610  $matchingConstraint = "";
611 
612  foreach($matchingFields as $mf)
613  {
614  if ($matchingConstraint) $matchingConstraint .= " AND ";
615  $matchingConstraint .= "{$mf->client_field}=:{$c}";
616  $c++;
617  }
618 
619  $matchingQuery = Query::create($class, "WHERE $matchingConstraint");
620  $matchingQuery->filter = new InclusionFilter($item->getPrimaryKey());
621 
622  while($record = DataSyncManager::getRecord($adapter, $fp, $columns, $helper, ((!$isChunked) || $counter >= 0)))
623  {
624  $item = new $class();
625  $skip = false;
626 
627  $item->filter = $filter;
628 
629  $skip = DataSyncManager::populateItem($item, $record, $fieldMapping, $lastItem);
630 
631  if (!$skip)
632  {
634 
635  if ($isChunked && $offset > 0)
636  {
637  $offset--;
638  continue;
639  }
640 
641  if ($counter <= 0)
642  {
643  continue;
644  }
645 
646  if ($item->hasField("owner_id")) $item->set("owner_id", $user->get($user->getPrimaryKey()));
647 
648  $c = 1;
649 
650  foreach($matchingFields as $mf)
651  {
652  $matchingQuery->bind(":{$c}", $item->get($mf->client_field));
653  $c++;
654  }
655 
656  $match = null;
657  try
658  {
659  $match = $matchingQuery->executeSingle();
660 
661  $item->set($item->getPrimaryKey(), $match->get($match->getPrimaryKey()));
662  }
663  catch(DataNotFoundException $e)
664  {
665  }
666  catch(FakoliException $e)
667  {
668  }
669 
670  $items[] = $item;
671  $lastItem = $item;
672  $counter--;
673  }
674 
675  }
676 
677  fclose($fp);
678 
679  return $items;
680  }
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104

◆ DataSyncManager()

DataSyncManager::DataSyncManager ( )

Definition at line 191 of file data_sync_manager.inc.

192  {
193  }

◆ extendForm()

static DataSyncManager::extendForm (   $form)
static

Definition at line 338 of file data_sync_manager.inc.

339  {
340  $class = get_class($form->data);
342 
343  if ($helper)
344  {
345  $helper->extendForm($form);
346  }
347  }
$form

◆ fieldMappingHasMatchField()

static DataSyncManager::fieldMappingHasMatchField (   $fieldMappings)
static

Definition at line 475 of file data_sync_manager.inc.

476  {
477  foreach($fieldMappings as $fieldMapping)
478  {
479  if ($fieldMapping->matching) return true;
480  }
481 
482  return false;
483  }

◆ generateFilter()

static DataSyncManager::generateFilter (   $fieldMapping)
static

Definition at line 490 of file data_sync_manager.inc.

491  {
492  $filter = new InclusionFilter();
493 
494  foreach($fieldMapping as $field => $mapping)
495  {
496  $filter->add($field);
497  }
498 
499  return $filter;
500  }

◆ getActiveFieldMapping()

static DataSyncManager::getActiveFieldMapping (   $class)
static

Definition at line 485 of file data_sync_manager.inc.

◆ getAdminImportTabs()

static DataSyncManager::getAdminImportTabs ( )
static

Definition at line 268 of file data_sync_manager.inc.

269  {
270  $tabs = array(
271  "Upload File" => "/admin/data_import",
272  "Select Target" => "/admin/data_import_select_target",
273  "Choose Fields" => "/admin/data_import_field_mapping",
274  "Select Records" => "/admin/data_import_select"
275  );
276 
277  $tabBar = new TabBar("tabs", $tabs);
278  $tabBar->useQueryString = false;
279  return $tabBar;
280 
281  }
$tabs

◆ getAvailableDataAdapters()

static DataSyncManager::getAvailableDataAdapters ( )
static

Definition at line 231 of file data_sync_manager.inc.

232  {
233  if (DataSyncManager::$adapters == null)
234  {
235  ComponentManager::fireEvent("RegisterDataSyncAdapter");
236  }
237  return array_keys(DataSyncManager::$adapters);
238  }

◆ getDataAdapter()

static DataSyncManager::getDataAdapter (   $format = null)
static

Definition at line 210 of file data_sync_manager.inc.

211  {
212  if (!$format)
213  {
214  $format = $_SESSION["data_import_format"];
215  if (!$format) $format = "CSV";
216  }
217 
218  if (DataSyncManager::$adapters == null)
219  {
220  ComponentManager::fireEvent("RegisterDataSyncAdapter");
221  }
222 
223  if (!array_key_exists($format, DataSyncManager::$adapters))
224  {
225  throw new FakoliException("Unknown Data Format");
226  }
227 
229  }

◆ getFieldMapping()

static DataSyncManager::getFieldMapping (   $class,
  $import_profile_id 
)
static

Definition at line 461 of file data_sync_manager.inc.

462  {
464 
465  trace("Retrieving field mapping for $class profile $import_profile_id", 3);
466 
468  IndexedQuery::create(DataImportFieldMapping, "WHERE class=:cl AND import_profile_id=:pr AND import_column != ''", "client_field")
469  ->bind(":cl", $class, ":pr", $import_profile_id)
470  ->execute();
471 
473  }

◆ getFieldMappings()

static DataSyncManager::getFieldMappings (   $class,
  $profile_id = 0 
)
static

Definition at line 349 of file data_sync_manager.inc.

350  {
351  if (!$class) return null;
352 
354 
355  $fieldMappings = IndexedQuery::create(DataImportFieldMapping, "WHERE class=:cl AND import_profile_id=:pr", "client_field")
356  ->bind(":cl", $class, ":pr", $profile_id)
357  ->execute();
358 
359  // Check for missing fields and create default entries
360 
361  $obj = new $class();
362  foreach($obj->getFields() as $field => $type)
363  {
364  if ($field == $obj->getPrimaryKey()) continue;
365  if (array_key_exists($field, $fieldMappings)) continue;
366 
368  $mapping->class = $class;
369  $mapping->client_field = $field;
370  $mapping->import_column = "";
371  $mapping->import_profile_id = $profile_id;
372 
373  $mapping->save();
374  }
375 
376  $fieldMappings = IndexedQuery::create(DataImportFieldMapping, "WHERE class=:cl AND import_profile_id=:pr", "client_field")
377  ->bind(":cl", $class, ":pr", $profile_id)
378  ->execute();
379 
380  // Sort field mappings
381  $filled = array();
382  $empty = array();
383  foreach($obj->getFields() as $field => $type)
384  {
385  if ($field == $obj->getPrimaryKey()) continue;
386  if (!array_key_exists($field, $fieldMappings)) continue;
387 
388  if ($fieldMappings[$field]->import_column)
389  {
390  $filled[] = $fieldMappings[$field];
391  }
392  else
393  {
394  $empty[] = $fieldMappings[$field];
395  }
396  }
397 
398  $fieldMappings = array_merge($filled, $empty);
399 
400  return $fieldMappings;
401  }

◆ getHelper()

static DataSyncManager::getHelper (   $class)
static

Definition at line 307 of file data_sync_manager.inc.

308  {
309  if (array_key_exists($class, DataSyncManager::$helpers))
310  {
312  }
313 
314  return $helper;
315  }

◆ getImportColumns()

static DataSyncManager::getImportColumns (   $class,
  $file = "",
  $format = null 
)
static

Definition at line 317 of file data_sync_manager.inc.

318  {
319  ComponentManager::fireEvent("RegisterSyncHelpers");
321 
322  if (!$file)
323  {
324  $file = $_SESSION["data_import_file"];
325  }
326 
327  $columns = $adapter->getImportColumns($file);
328 
330  if ($helper)
331  {
332  $columns = $helper->getAdditionalColumns($columns);
333  }
334 
335  return $columns;
336  }

◆ getImportTabs()

static DataSyncManager::getImportTabs ( )
static

Definition at line 253 of file data_sync_manager.inc.

254  {
255  $tabs = array(
256  "Upload File" => "data_import",
257  "Select Target" => "data_import_select_target",
258  "Choose Fields" => "data_import_field_mapping",
259  "Select Records" => "data_import_select"
260  );
261 
262  $tabBar = new TabBar("tabs", $tabs);
263  $tabBar->useQueryString = false;
264  return $tabBar;
265 
266  }

◆ getSyncTargets()

static DataSyncManager::getSyncTargets ( )
static

Definition at line 829 of file data_sync_manager.inc.

830  {
831  $targets = array();
832  $targets = ComponentManager::fireEvent("RegisterSyncTargets", $targets);
833 
834  $syncTargets = array();
835  foreach($targets as $index => $classes)
836  {
837  if (!is_array($classes))
838  {
839  $syncTargets[] = $classes;
840  }
841 
842  foreach($classes as $class)
843  {
844  $syncTargets[] = $class;
845  }
846  }
847 
848  return $syncTargets;
849  }

◆ isRowSelected()

static DataSyncManager::isRowSelected (   $row)
static

Definition at line 824 of file data_sync_manager.inc.

825  {
826  return false;
827  }

◆ onItemRow()

static DataSyncManager::onItemRow (   $item)
static

Definition at line 818 of file data_sync_manager.inc.

819  {
820  $val = $item->get($item->getPrimaryKey());
821  return ($val) ? "matching" : "new";
822  }

◆ registerCSVAdapter()

static DataSyncManager::registerCSVAdapter ( )
static

Definition at line 205 of file data_sync_manager.inc.

206  {
208  }
static registerDataSyncAdapter($format, $adapter)

◆ registerDataSyncAdapter()

static DataSyncManager::registerDataSyncAdapter (   $format,
  $adapter 
)
static

Definition at line 200 of file data_sync_manager.inc.

201  {
203  }

◆ registerSerializationHandler()

static DataSyncManager::registerSerializationHandler ( )
static

Definition at line 247 of file data_sync_manager.inc.

248  {
249  SerializationManager::registerHandler("data_sync", "Data Import Profiles and Field Mappings", new DataSyncSerializationHandler());
250  return true;
251  }
Serialization handler for Link Libraries and Link Records.
registerHandler($component, $title, $handler)
Registers a serialization handler for a component.

◆ registerSyncHelper()

static DataSyncManager::registerSyncHelper (   $class,
  $helper 
)
static

Definition at line 195 of file data_sync_manager.inc.

196  {
198  }

◆ setDefaults()

static DataSyncManager::setDefaults ( )
static

Definition at line 240 of file data_sync_manager.inc.

241  {
242  Settings::setDefaultValue("data_sync", "import_in_chunks", false, Boolean, "Specifies whether to limit the number of records being imported at one time.");
243  Settings::setDefaultValue("data_sync", "records_per_page", 500, Number, "The number of records to import at one time when chunked import is enabled.");
244  Settings::setDefaultValue("data_sync", "multiple_import_profiles", false, Boolean, "Turn on support for configuring multiple import profile for each sync target.");
245  }
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174

◆ skipToOffset()

static DataSyncManager::skipToOffset (   $fp,
  $offset = -1 
)
static

Definition at line 294 of file data_sync_manager.inc.

295  {
296  if ($offset == -1)
297  {
298  $offset = checkNumeric($_GET["offset"]);
299  }
300 
301  while($offset-- > 0)
302  {
303  fgetcsv($fp);
304  }
305  }

◆ upgradeComponent()

static DataSyncManager::upgradeComponent (   $version)
static

Definition at line 851 of file data_sync_manager.inc.

852  {
854  $mgr->upgrade($version);
855  }

Member Data Documentation

◆ $adapters

DataSyncManager::$adapters = null
static

Definition at line 189 of file data_sync_manager.inc.

◆ $fieldMappings

DataSyncManager::$fieldMappings = array()
static

Definition at line 185 of file data_sync_manager.inc.

◆ $helpers

DataSyncManager::$helpers = null
static

Definition at line 183 of file data_sync_manager.inc.

◆ $import_profile_id

DataSyncManager::$import_profile_id = 0
static

Definition at line 187 of file data_sync_manager.inc.

◆ $totalRecords

DataSyncManager::$totalRecords = 0
static

Definition at line 184 of file data_sync_manager.inc.


The documentation for this class was generated from the following file: