aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporter.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sfIsisImporter.class.php')
-rw-r--r--lib/sfIsisImporter.class.php333
1 files changed, 333 insertions, 0 deletions
diff --git a/lib/sfIsisImporter.class.php b/lib/sfIsisImporter.class.php
new file mode 100644
index 0000000..9233f64
--- /dev/null
+++ b/lib/sfIsisImporter.class.php
@@ -0,0 +1,333 @@
+<?php
+
+/**
+ * IsisImporter: provides ISIS import methods for importing data into
+ * a Symfony project.
+ *
+ * Importing can be done either on actions or tasks.
+ */
+class sfIsisImporter extends sfIsisImporterBase
+{
+ /**
+ * Execute a mass import of ISIS database entries. This function reads
+ * each database entry and dispatch each field read to a custom
+ * dispatch function.
+ *
+ * @param object $caller Caller object
+ * @param string $section Caller section (whether an action or task)
+ * @param int $entries Number of entries to import (defaults to all)
+ */
+ public function massImport($caller, $section, $entries = NULL)
+ {
+ // We hold arguments for logging purposes.
+ $this->caller = $caller;
+ $this->section = $section;
+
+ foreach ($this->databases() as $database)
+ {
+ // Open database.
+ $this->log('Starting mass import procedure for config '. $database .'.');
+ $this->open($this->config($database));
+
+ // Determine base model and max entries.
+ $base_model = $this->format['db']['base_model'];
+ $this->entries = ($entries != NULL && $entries <= $this->entries) ? $entries : $this->entries;
+
+ if ($base_model) {
+ for ($entry = 1; $entry <= $this->entries; $entry++)
+ {
+ $this->addEntry($base_model, $entry);
+ $this->progress($entry);
+ }
+ }
+
+ $this->afterImport();
+ }
+ }
+
+ /**
+ * Add a single entry from the database.
+ *
+ * @param string $base_model Model to use
+ * @param int $entry Entry number
+ * @todo Warn if repetition occurs for non-repeatable fields
+ * @todo Check if content already exist if a primary key is specified
+ */
+ public function addEntry($base_model, $entry)
+ {
+ // Get data and setup the model.
+ $this->read($entry);
+ $model = new $base_model();
+ $this->setBaseModelId($model);
+
+ $this->log("Importing $base_model $entry...");
+
+ // Dispatch to custom import procedures.
+ foreach (new IsisMethodIterator($this) as $method => $field)
+ {
+ $this->{$method}($model, $field);
+ }
+
+ $model->save();
+ }
+
+ /**
+ * Set the primary key for the model by getting it or saving the model.
+ *
+ * @param object $model Base model
+ */
+ public function setBaseModelId(&$model)
+ {
+ $id = $this->getBaseModelId($model);
+
+ if ($id)
+ {
+ $model->id = $id;
+ }
+ else
+ {
+ $model->save();
+ }
+ }
+
+ /**
+ * Get the primary key for the base model.
+ *
+ * @param object $model Base model
+ * @return int Base model id
+ * @todo Write and test
+ */
+ public function getBaseModelId(&$model)
+ {
+ return false;
+ }
+
+ /**
+ * Import a single field into a model.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param int $row Row number
+ */
+ public function addField(&$model, $field, $row = 0) {
+ $value = $this->filterBrackets($this->getMainItem($field, $row));
+
+ if ($value != null)
+ {
+ $map = $this->getMap($field);
+ $model->{$map}($value);
+ }
+ }
+
+ /**
+ * Import a single subfield into a model.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param string $subfield Subfield name
+ * @param int $row Row number
+ */
+ public function addSubfield(&$model, $field, $subfield, $row = 0) {
+ $value = $this->filterBrackets($this->getSubfield($field, $subfield, $row));
+
+ if ($value != null)
+ {
+ $map = $this->getMap($field, $subfield);
+ $model->{$map}($value);
+ }
+ }
+
+ /**
+ * Import single values into the model.
+ *
+ * Currently undefined mappings for a field/subfield
+ * are not saved as we would need to make sure a corresponding
+ * field exists in the model. This prevents the map
+ * configurations like $map = array('type' => 'value'); to work.
+ *
+ * As we are importing single values, here we don't care with
+ * row numbers as we assume that just the first row should be
+ * imported.
+ *
+ * @param object $model Model object
+ * @param array $field Field data
+ */
+ public function importValues(&$model, array $field)
+ {
+ if ($this->fieldHasMap($field))
+ {
+ $this->addField($model, $field);
+ }
+
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ if ($this->subfieldHasMap($field, $subfield))
+ {
+ $this->addSubfield($model, $field, $subfield);
+ }
+ }
+ }
+
+ /**
+ * Add a new entity into the database if needed, returning
+ * the corresponding object.
+ *
+ * @param string $name Genre name
+ * @return Genre Genre data
+ */
+ public function addEntity($entity, $name)
+ {
+ $name = $this->entityName($name);
+ $data = Doctrine_Core::getTable($entity)->findOneByName($name);
+
+ if (!$data)
+ {
+ $this->log("Adding new $entity $name.");
+ $data = new $entity();
+ $data->name = $name;
+ $data->save();
+ }
+
+ return $data;
+ }
+
+ /**
+ * Import one to one data.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param string $relation Relation name
+ */
+ public function addOneToOne(&$model, array $field, $relation)
+ {
+ foreach (new IsisRowIterator($this, $field) as $row)
+ {
+ $data = new $relation();
+
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ $this->addSubfield($data, $field, $subfield, $row);
+ }
+
+ $data->save();
+ $key = sfInflector::underscore($relation) .'_id';
+ $model->$key = $relation->id;
+ }
+ }
+
+ /**
+ * Import many to many data.
+ *
+ * @param object $model Model
+ * @param array $values Values to be added
+ * @param string $relation Relation name
+ */
+ public function addManyToMany(&$model, array $values, $relation) {
+ $method = 'add'. $relation;
+
+ foreach ($values as $value)
+ {
+ // Populate related data.
+ if (is_callable(array($this, $method)))
+ {
+ $data = $this->{$method}($value);
+ }
+ else
+ {
+ $data = $this->addEntity($relation, $value);
+ }
+
+ // Get model and relation names and id fields.
+ $model_id = $this->getModelId($model);
+ $relation_id = $this->getRelationId($relation);
+ $model_relation = $this->getModelRelation($model, $relation);
+
+ // Make the relation.
+ $model_data = new $model_relation();
+ $model_data->{$model_id} = $model->id;
+ $model_data->{$relation_id} = $data->id;
+ $model_data->save();
+ }
+ }
+
+ /**
+ * Import one to one data.
+ *
+ * @param object $model Model
+ * @param string $relation Relation name
+ * @return object Relation model object
+ */
+ public function addOneToMany(&$model, $relation)
+ {
+ $model_id = $this->getModelId($model);
+ $data = new $relation();
+ $data->{$model_id} = $model->id;
+ $data->save();
+ return $data;
+ }
+
+ /**
+ * Add simple entities data into the model.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ */
+ public function addOneToManyEntities(&$model, array $field, $entity, $key = 'name')
+ {
+ foreach (new IsisMainItemIterator($this, $field) as $row => $value)
+ {
+ $this->log("Entity: $entity; Value: $value", 'debug');
+ $data = $this->addOneToMany($model, $entity);
+ $data->{$key} = $value;
+ $data->save();
+ }
+ }
+
+ /**
+ * Add field values in a many-to-many relation.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param string $relation Relation name
+ */
+ public function addManyToManyField(&$model, array $field, $relation)
+ {
+ foreach (new IsisMainItemIterator($this, $field) as $value)
+ {
+ $this->addManyToMany($model, $this->explodeBrackets($value), $relation);
+ }
+ }
+
+ /**
+ * Add an element into the database if needed, returning
+ * the resulting object.
+ *
+ * @param string $entity Entity name
+ * @param string $value Entity value
+ * @return object Entity data
+ */
+ public function newOrExisting($entity, $value)
+ {
+ // Check for a null value.
+ if ($value == null)
+ {
+ $this->log("Null element value for $entity.", 'debug');
+ return;
+ }
+
+ // Get name.
+ $name = $this->parseName($value);
+
+ // Get existing element.
+ $element = call_user_func(array($entity, 'getByName'), $name);
+
+ // Create new element if needed.
+ if (!$element)
+ {
+ $this->log("Adding new $entity $value.");
+ $element = call_user_func(array($entity, 'addByName'), $name);
+ }
+
+ return $element;
+ }
+}