aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <s1lv10@uol.com.br>2010-08-30 18:29:25 -0300
committerSilvio <s1lv10@uol.com.br>2010-08-30 18:29:25 -0300
commit81cd263f4598115cc90817300f69a869fa5e444a (patch)
tree016145006ea02231629712803978fe18ee4c046f
parent38f4cfb1cdbf7359bb6818167ac3d7ec0d3268f7 (diff)
downloadsf_isis_importer_plugin-81cd263f4598115cc90817300f69a869fa5e444a.tar.gz
sf_isis_importer_plugin-81cd263f4598115cc90817300f69a869fa5e444a.tar.bz2
Breaking sfIsisImporter into more classes
-rw-r--r--lib/sfIsisImporter.class.php267
-rw-r--r--lib/sfIsisImporterEntities.class.php179
-rw-r--r--lib/sfIsisImporterRelations.class.php127
-rw-r--r--package.xml2
4 files changed, 309 insertions, 266 deletions
diff --git a/lib/sfIsisImporter.class.php b/lib/sfIsisImporter.class.php
index 9656cf9..6119d35 100644
--- a/lib/sfIsisImporter.class.php
+++ b/lib/sfIsisImporter.class.php
@@ -4,7 +4,7 @@
* IsisImporter: provides ISIS import methods for importing data into
* a Symfony project.
*/
-class sfIsisImporter extends IsisConnector
+class sfIsisImporter extends sfIsisImporterRelations
{
/**
* Log dispatcher.
@@ -62,17 +62,6 @@ class sfIsisImporter extends IsisConnector
}
/**
- * Get the entity name from a subfield.
- *
- * @param string $subfield Subfield name
- * @return string Genre name
- */
- static function entityName($subfield)
- {
- return ucfirst($subfield);
- }
-
- /**
* Get the model name.
*
* @param object $model Model
@@ -205,260 +194,6 @@ class sfIsisImporter extends IsisConnector
}
/**
- * 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);
- }
- }
- }
-
- /**
- * Get an existing entity.
- *
- * @param string $entity Entity name
- * @param string $value Value to search for
- * @param string $by Field to search $value
- * @return mixed Entity data or false
- */
- public function getEntity($entity, $value, $by = 'name')
- {
- $findby = 'findOneBy'. ucfirst($by);
- $data = Doctrine_Core::getTable($entity)->{$findby}($value);
-
- if ($data)
- {
- return $data;
- }
-
- return false;
- }
-
- /**
- * Add a new entity into the database if needed, returning
- * the corresponding object.
- *
- * @param string $entity Entity name
- * @param string $name Name value
- * @return object Entity data
- */
- public function addEntity($entity, $name)
- {
- $name = $this->entityName($name);
- $data = $this->getEntity($entity, $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 = $data->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;
- }
-
- /**
* Check denied combinations inside a field.
*
* @param string $model Model name
diff --git a/lib/sfIsisImporterEntities.class.php b/lib/sfIsisImporterEntities.class.php
new file mode 100644
index 0000000..56c2883
--- /dev/null
+++ b/lib/sfIsisImporterEntities.class.php
@@ -0,0 +1,179 @@
+<?php
+
+/**
+ * IsisImporterEntities: provides ISIS import methods for importing data
+ * into model entities.
+ */
+class sfIsisImporterEntities extends IsisConnector {
+ /**
+ * Get the entity name from a subfield.
+ *
+ * @param string $subfield Subfield name
+ * @return string Genre name
+ */
+ static function entityName($subfield)
+ {
+ return ucfirst($subfield);
+ }
+
+ /**
+ * 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 addMain(&$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 all subfields from a field row into a model.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param int $row Row number
+ */
+ public function addSubfields(&$model, $field, $row = 0)
+ {
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ $this->addSubfield($model, $field, $subfield, $row);
+ }
+ }
+
+ /**
+ * 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->addMain($model, $field);
+ }
+
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ if ($this->subfieldHasMap($field, $subfield))
+ {
+ $this->addSubfield($model, $field, $subfield);
+ }
+ }
+ }
+
+ /**
+ * Get an existing entity.
+ *
+ * @param string $entity Entity name
+ * @param string $value Value to search for
+ * @param string $by Field to search $value
+ * @return mixed Entity data or false
+ */
+ public function getEntity($entity, $value, $by = 'name')
+ {
+ $findby = 'findOneBy'. ucfirst($by);
+ $data = Doctrine_Core::getTable($entity)->{$findby}($value);
+
+ if ($data)
+ {
+ return $data;
+ }
+
+ return false;
+ }
+
+ /**
+ * Add a new entity into the database if needed, returning
+ * the corresponding object.
+ *
+ * @param string $entity Entity name
+ * @param string $name Name value
+ * @return object Entity data
+ */
+ public function addEntity($entity, $name)
+ {
+ $name = $this->entityName($name);
+ $data = $this->getEntity($entity, $name);
+
+ if (!$data)
+ {
+ $this->log("Adding new $entity $name.");
+ $data = new $entity();
+ $data->name = $name;
+ $data->save();
+ }
+
+ return $data;
+ }
+
+ /**
+ * 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;
+ }
+}
diff --git a/lib/sfIsisImporterRelations.class.php b/lib/sfIsisImporterRelations.class.php
new file mode 100644
index 0000000..8282845
--- /dev/null
+++ b/lib/sfIsisImporterRelations.class.php
@@ -0,0 +1,127 @@
+<?php
+
+/**
+ * IsisImporterRelations: provides ISIS import methods for importing data
+ * into model entities.
+ */
+class sfIsisImporterRelations extends sfIsisImporterEntities {
+ /**
+ * 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();
+
+ $this->addMain($data, $field, $row);
+ $this->addSubfields($data, $field, $row);
+ $data->save();
+
+ $key = sfInflector::underscore($relation) .'_id';
+ $model->$key = $data->id;
+ }
+ }
+
+ /**
+ * Import one to many 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 addOneToManyMain(&$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();
+ }
+ }
+
+ /**
+ * Import a single many to many data.
+ *
+ * @param object $model Model
+ * @param mixed $values Values to be added
+ * @param string $relation Relation name
+ */
+ public function addManyToMany(&$model, $value, $relation)
+ {
+ $method = 'add'. $relation;
+
+ // 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();
+
+ return $model_data;
+ }
+
+ /**
+ * Import many to many data.
+ *
+ * @param object $model Model
+ * @param array $values Values to be added
+ * @param string $relation Relation name
+ */
+ public function addManyToManyEntities(&$model, array $values, $relation)
+ {
+ foreach ($values as $value)
+ {
+ $this->addManyToMany($model, $value, $relation);
+ }
+ }
+
+ /**
+ * 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 addManyToManyMain(&$model, array $field, $relation)
+ {
+ foreach (new IsisMainItemIterator($this, $field) as $value)
+ {
+ $this->addManyToManyEntities($model, $this->explodeBrackets($value), $relation);
+ }
+ }
+}
diff --git a/package.xml b/package.xml
index ef486ed..d95c081 100644
--- a/package.xml
+++ b/package.xml
@@ -31,6 +31,8 @@
<file role="data" name="sfIsisImporterLog.class.php" />
<file role="data" name="sfIsisImporterStats.class.php" />
<file role="data" name="sfIsisImporterManager.class.php" />
+ <file role="data" name="sfIsisImporterEntities.class.php" />
+ <file role="data" name="sfIsisImporterRelations.class.php" />
<dir name="lib">
<file role="data" name="mySfTask.class.php" />
<file role="data" name="isisImportTask.class.php" />