Skip to content

Commit

Permalink
updates to allow executeAll() and validation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
druid628 committed Apr 2, 2012
1 parent 1920b2e commit e00bc1d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 18 deletions.
40 changes: 40 additions & 0 deletions lib/import/PluginBsdImporterCsv.class.php
Expand Up @@ -33,6 +33,26 @@ public function readData($filename)
$this->DataRows = $this->genHeaderBasedArray($dataRow);
}

/**
* validationFailed() - User defined function so you can say what
* to do with validation errors.
*
* @param mixed $validationErrorCode - should be constant from for
* validation error
*/
public function validationFailed($validationErrorCode)
{
/**
* This is the point when your extended class should kick in.
* The execute function is executed on all rows instead of
* individual rows and do with the data what you will.
*
* You have all the weapons you need.
* Now fight.
*
* @uses subclass::validationFailed($validationErrorCode)
*/
}
/**
* execute() - Programmer defined logic for what should be done
* with each row as it's processed.
Expand Down Expand Up @@ -64,5 +84,25 @@ public function execute($row, $dryRun = false)
*/
}

/**
* executeAll() - Programmer defined logic for what should be done
* with each row as it's processed.
*
* @see processImport()
* @param boolean $dryRun
*/
public function executeAll($dryRun = false)
{
/**
* This is the point when your extended class should kick in.
* The execute function is executed on all rows instead of
* individual rows and do with the data what you will.
*
* You have all the weapons you need.
* Now fight.
*
* @uses subclass::executeAll($dryRun = false)
*/
}

}
106 changes: 88 additions & 18 deletions lib/import/base/PluginBsdImporter.class.php
Expand Up @@ -19,6 +19,10 @@ abstract class PluginBsdImporter implements PluginBsdImporterInterface
const PRE_PROCCESS_VALIDATE = 1;
const BY_ROW_VALIDATE = 2;
const NO_VALIDATION = 13;

const INVALID_COLUMN_HEADERS = 'Z';
const INVALID_COLUMN_COUNT = 'Y';
const INVALID_REQUIRED_FIELDS = 'X';

protected $requiredHeaders = array();
protected $fileHeaders = array();
Expand All @@ -40,6 +44,10 @@ public function __construct($fileToProcess)
$this->readData($fileToProcess);
}

public function getInvalidsArray()
{
return array( self::INVALID_COLUMN_COUNT, self::INVALID_COLUMN_HEADERS, self::INVALID_REQUIRED_FIELDS);
}

/*
* BEGIN Generic Import specific ( get it? :D ) functions
Expand Down Expand Up @@ -218,31 +226,35 @@ public function validateHeaders()
if($headers[$key] != $header)
{
return false;
//return self::INVALID_COLUMN_HEADERS;

}
}
return true;
}

public function validateAllRowCount()
public function validateAllColumnCount()
{
foreach($this->getDataRows() as $row => $rowData)
{
$rowTest = $this->validateRowCount($rowData);
$rowTest = $this->validateColumnCount($rowData);
if(!$rowTest)
{
return false;
return false;
// return self::INVALID_COLUMN_COUNT;
}
return true;
}
}

public function validateRowCount($row)
public function validateColumnCount($row)
{
if (count($this->fileHeaders) == count($row))
{
return true;
}
return false;
// return self::INVALID_COLUMN_COUNT;
}

/**
Expand Down Expand Up @@ -288,6 +300,37 @@ public function validateRequiredFields()
}


/**
* preProcessValidation() checks validation if validation IS set to occur before processing
* execute validation and return bool (obviously true if valid or false if not)
*
* if it is not set for preProcess validation then return the code for when to execute
*
* return mixed int|char|boolean
*/
protected function preProcessValidation()
{
if ( $this->validation == self::PRE_PROCCESS_VALIDATE )
{
$reqdFields = $this->validateRequiredFields();
$validColumnCount = $this->validateAllColumnCount();
if(!$reqdFields)
{
return self::INVALID_REQUIRED_FIELDS;
}
if(!$validColumnCount)
{
return self::INVALID_COLUMN_COUNT;
}
}
else
{
return $this->validation;
}


}

/*
* BEGIN import processing
*/
Expand All @@ -301,31 +344,38 @@ public function validateRequiredFields()
* @param boolean $dryRun
* @return array
*/
public function processImport($dryRun = false)
public function processImport($dryRun = false, $allAsOne = false)
{
if ( $this->validation == self::PRE_PROCCESS_VALIDATE )

$preValidation = $this->preProcessValidation();
if ( in_array($preValidation, $this->getInvalidsArray()) )
{
$this->validateRequiredFields();
$this->validateAllRowCount();
$this->validationFailed($preValidation); // userDefined function
}
elseif ( $this->validation == self::BY_ROW_VALIDATE )
elseif ($preValidation === self::BY_ROW_VALIDATE)
{
$rowValidation = true;
}


foreach ($this->DataRows as $row => $rowData)

if(!$allAsOne)
{
if ($rowValidation)
foreach ($this->DataRows as $row => $rowData)
{
if ( !($this->validateRequiredFieldsInRow($rowData)) && !($this->validateRowCount($row)) )
if ($rowValidation)
{
return array("success"=> false, "message" => "Validation Error in row $row. Please check the file and try again.");
if ( !($this->validateRequiredFieldsInRow($rowData)) && !($this->validateColumnCount($row)) )
{
return array("success"=> false, "message" => "Validation Error in row $row. Please check the file and try again.");
}
}
}

$this->execute($rowData, $dryRun);

$this->execute($rowData, $dryRun);

}
}
else
{
$this->executeAll($dryRun);
}
return array("success" => true);
}
Expand All @@ -349,4 +399,24 @@ public function execute($row, $dryRun = false)
*
*/
}

/**
* executeAll() - Programmer defined logic for what should be done
* with all rows.
*
* @see processImport()
* @param boolean $dryRun
*/
public function executeAll($dryRun = false)
{
/*
*
* does nothing here
* all the magic is handled in the user's class's executeAll()
* I don't know what you want to do with your imports...
* I'm not psychic :D
*
*/

}
}

0 comments on commit e00bc1d

Please sign in to comment.