From 81cd263f4598115cc90817300f69a869fa5e444a Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 30 Aug 2010 18:29:25 -0300 Subject: Breaking sfIsisImporter into more classes --- lib/sfIsisImporterEntities.class.php | 179 +++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 lib/sfIsisImporterEntities.class.php (limited to 'lib/sfIsisImporterEntities.class.php') 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 @@ +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; + } +} -- cgit v1.2.3