Skip to content

Commit

Permalink
Moves some of the more generic functions back into PluginBsdImporter
Browse files Browse the repository at this point in the history
and cleans up some of the functionality.
Also relaized that each row was *NOT* header::value like original spec
called for - so that is also fixed.
  • Loading branch information
druid628 committed Mar 30, 2012
1 parent 918a675 commit 5f57be8
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 70 deletions.
72 changes: 3 additions & 69 deletions lib/import/PluginBsdImporterCsv.class.php
Expand Up @@ -8,71 +8,11 @@ public function readData($filename)
if (($handle = fopen($filename, "r")) !== FALSE)
{
while (($data = fgetcsv($handle)) !== FALSE) {
$this->DataRows[] = $data;
$dataRow = $data;
}
fclose($handle);
}
}

/**
* validateHeaders() - Examines the header row in given csv
* file and verifies that these headers exist.
*
* @return boolean
*/
public function validateHeaders()
{
foreach($this->getRequiredHeaders() as $key => $header)
{
$headers = $this->getHeaders();
if($headers[$key] != $header)
{
return false;
}
}
return true;
}

/**
* validateRequiredFieldsInRow() - examines row provided
* and determines if the predefined required fields exist
* and returns based of that examination.
*
* @param array $row
* @return boolean
*/
public function validateRequiredFieldsInRow($row)
{
$requiredFields = $this->getRequiredFields();
foreach($requiredFields as $key => $reqdField)
{
$field = $row[$reqdField];
if($field == "" || is_null($field) )
{
return false;
}
}
}

/**
* Validates all rows in csv for the fields set as required
* by passing them through $this->validateRequiredFieldsInRow()
*
* @see validateRequiredFieldsInRow
* @return boolean
*/
public function validateRequiredFields()
{
$requiredFields = $this->getRequiredFields();
foreach($this->getDataRows() as $row => $rowData)
{
$rowTest = $this->validateRequiredFieldsInRow($rowData);
if(!$rowTest)
{
return false;
}
}
return true;
$this->DataRows = $this->genHeaderBasedArray($dataRow);
}

/**
Expand Down Expand Up @@ -106,13 +46,7 @@ public function processImport($dryRun = null)
}
}

if($dryRun)
{
$this->dryRunExecute($rowData);
}
else {
$this->execute($rowData);
}
$this->execute($rowData, $dryRun);
}

return array("success" => true);
Expand Down
131 changes: 130 additions & 1 deletion lib/import/base/PluginBsdImporter.class.php
Expand Up @@ -8,6 +8,7 @@ abstract class PluginBsdImporter implements PluginBsdImporterInterface
const NO_VALIDATION = 13;

protected $requiredHeaders = array();
protected $fileHeaders = array();
protected $requiredFields = array();
protected $DataRows = array();
protected $validation = self::PRE_PROCESS_VALIDATE;
Expand All @@ -17,6 +18,12 @@ public function __construct($fileToProcess)
$this->readData($fileToProcess);
}


/*
* BEGIN Generic Import specific ( get it? :D ) functions
*/


/**
* alias for setRequiredHeaders
* @param array $headers
Expand All @@ -26,6 +33,7 @@ public function setHeaders(array $headers)
$this->requiredHeaders = $headers;
}


/**
*
* alias for getRequiredHeaders
Expand All @@ -36,6 +44,29 @@ public function getHeaders()
return $this->requiredHeaders;
}


/**
*
* @param array $data multideminsional array with the first sub array being headers
* @return array
*/
function genHeaderBasedArray($data)
{
$this->fileHeaders = array_shift($data);
$headerCount = count($this->fileHeaders);
$rowdata = array();
foreach($data as $row)
{
if ($headerCount == count($row)) {
$rowdata[] = array_combine($this->fileHeaders, $row);
} else {
trigger_error("ERROR (0014): Array Combination Epic failed -- Column count does not match!");
}
}
return $rowdata;
}


/**
* defines when to execute validation
* accepts a string of pre, row, or no
Expand All @@ -62,6 +93,10 @@ public function setValidation($validationTime)
}
}

/*
* BEGIN Magic and Misc functions
*/

/**
*
* hooray for magic!!
Expand Down Expand Up @@ -142,6 +177,99 @@ public function lcfirst($string)
return $string;
}


/*
* BEGIN validation
*/

/**
* validateHeaders() - Examines the header row in given csv
* file and verifies that these headers exist.
*
* @return boolean
*/
public function validateHeaders()
{
foreach($this->getRequiredHeaders() as $key => $header)
{
$headers = $this->getHeaders();
if($headers[$key] != $header)
{
return false;
}
}
return true;
}

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

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

/**
* validateRequiredFieldsInRow() - examines row provided
* and determines if the predefined required fields exist
* and returns based of that examination.
*
* @param array $row
* @return boolean
*/
public function validateRequiredFieldsInRow($row)
{
$requiredFields = $this->getRequiredFields();
foreach($requiredFields as $key => $reqdField)
{
$field = $row[$reqdField];
if($field == "" || is_null($field) )
{
return false;
}
}
}

/**
* Validates all rows in csv for the fields set as required
* by passing them through $this->validateRequiredFieldsInRow()
*
* @see validateRequiredFieldsInRow
* @return boolean
*/
public function validateRequiredFields()
{
$requiredFields = $this->getRequiredFields();
foreach($this->getDataRows() as $row => $rowData)
{
$rowTest = $this->validateRequiredFieldsInRow($rowData);
if(!$rowTest)
{
return false;
}
}
return true;
}


/*
* BEGIN import processing
*/

/**
* processImport() - Runs validation and beings import functonality
* dryRun gives you the optional parameter that can be passed
Expand All @@ -156,6 +284,7 @@ public function processImport($dryRun = false)
if ( $this->validation == self::PRE_PROCCESS_VALIDATE )
{
$this->validateRequiredFields();
$this->validateAllRowCount();
}
elseif ( $this->validation == self::BY_ROW_VALIDATE )
{
Expand All @@ -167,7 +296,7 @@ public function processImport($dryRun = false)
{
if ($rowValidation)
{
if (!($this->validateRequiredFieldsInRow($rowData)))
if ( !($this->validateRequiredFieldsInRow($rowData)) && !($this->validateRowCount($row)) )
{
return array("success"=> false, "message" => "Validation Error in row $row. Please check the file and try again.");
}
Expand Down

0 comments on commit 5f57be8

Please sign in to comment.