From d547587ac6c0f765a983a746adb03490a9e74502 Mon Sep 17 00:00:00 2001 From: Silvio Date: Wed, 4 Aug 2010 14:36:20 -0300 Subject: Checking for denied combinations --- lib/sfIsisImporter.class.php | 101 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) (limited to 'lib/sfIsisImporter.class.php') diff --git a/lib/sfIsisImporter.class.php b/lib/sfIsisImporter.class.php index 32fde7d..4b46455 100644 --- a/lib/sfIsisImporter.class.php +++ b/lib/sfIsisImporter.class.php @@ -99,6 +99,7 @@ class sfIsisImporter extends IsisConnector * * @param string $base_model Model to use * @param int $entry Entry number + * @todo Audit: which field/subfields combination are denied? */ public function addEntry($base_model, $entry) { @@ -113,7 +114,10 @@ class sfIsisImporter extends IsisConnector // Dispatch to custom import procedures. foreach (new IsisMethodIterator($this) as $method => $field) { - $this->{$method}($model, $field); + if (!$this->hasDeniedCombinations($base_model, $field)) + { + $this->{$method}($model, $field); + } } $model->save(); @@ -431,4 +435,99 @@ class sfIsisImporter extends IsisConnector return $element; } + + /** + * Check denied combinations inside a field. + * + * @param string $model Model name + * @param array $field Field data from ISIS database schema + * @return boolean True if has a denied combination, false otherwise + * @todo Test + */ + public function hasDeniedCombinations($model, $field) + { + if (method_exists($this, 'getDeniedCombinations')) + { + foreach ($this->getDeniedCombinations($model, $field) as $combination) + { + $has = true; + + foreach ($combination as $item) + { + foreach (new IsisRowIterator($this, $field) as $row) + { + if (!$this->hasDeniedItem($field, $item, $row)) + { + $has = false; + break 2; + } + } + } + + if ($has) + { + $denied[$row] = $combination; + } + } + } + + if (isset($denied)) + { + foreach ($denied as $row => $combination) + { + $combination = implode(',', $combination); + $this->log("Found denied combination for row $row: $combination"); + } + + return true; + } + + return false; + } + + /** + * Check if a field has a denied item. + * + * @param array $field Field data from ISIS database schema + * @param string $item Item code + * @param int $row Row number + * @return boolean True if has a denied combination, false otherwise + * @todo Test + */ + public function hasDeniedItem($field, $item, $row) + { + // Default condition: presence requirement for an item. + $condition = true; + + // Check if item has the negation (absence) requirement for an item. + if (substr($item, 0, 1) == '!') + { + $item = substr($item, 1); + $condition = false; + } + + // Check if the item exists. + $has = $this->hasItem($field, $item, $row); + + if ($condition == true) + { + // If the condition is true, a denied combination won't be fulfilled if + // the item is not present. + if (!$has) + { + return false; + } + } + else + { + // If the condition is false, a denied combination won't be fulfilled if + // the item is present. + if ($has) + { + return false; + } + } + + return true; + } } -- cgit v1.2.3