Skip to content

Commit

Permalink
Fix bug with initFrom & ModuleOfferingAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyceBrady committed Dec 18, 2013
1 parent fec8838 commit c2d3b8d
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 97 deletions.
118 changes: 82 additions & 36 deletions application/controllers/TableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,16 @@ public function addAction()
// Determine fields being added. Remove null fields.
$addValues = $this->_fillInitValues($setTable,
$form->getFieldValues());
$meaningfulData = $this->_getFilledFields($addValues);
$meaningfulData = $setTable->removeImports($meaningfulData);
if ( $addValues != null )
{
$meaningfulData = $this->_getFilledFields($addValues);
$meaningfulData = $setTable->removeImports($meaningfulData);

// Update the database and redisplay the record.
$setTable->addTableEntry($meaningfulData);
$this->_executeSearch($setTable, $meaningfulData, null,
self::DISPLAY_ALL);
// Update the database and redisplay the record.
$setTable->addTableEntry($meaningfulData);
$this->_executeSearch($setTable, $meaningfulData, null,
self::DISPLAY_ALL);
}
}
else
{
Expand Down Expand Up @@ -1128,50 +1131,40 @@ protected function _isUnaryComparator($field, $comparators)

/**
* Fill in initial values based on information in setting.
* TODO: Is there any need to consider cases where the
* "external table" is this table, so one field is being
* initialized from another that is not in the database yet?
*
* @param Application_Model_SetTable table: setting & db info
* @param array $data Column-value pairs representing provided data
*/
protected function _fillInitValues($setTable, array $data)
{
$storedSourceRecs = array();
$searchData = $data;
$initializedData = $data;

// Loop through fields in this table to see if any should be
// initialized from values in another table.
$inputFieldNames = array_keys($data);
$relevantFields = $this->view->tableInfo->getExternallyInitFields();
// Why wasn't this written as the simpler: ?
// $relevantFields = $setTable->getExternallyInitFields();
$relevantFields = $setTable->getExternallyInitFields();
foreach ( $relevantFields as $newFieldName => $newField )
{
// Initialize from another table if data not already provided.
if ( $data[$newFieldName] == null )
// Get table reference used by this field.
$sourceTblName = $newField->getInitTableName();

// Has the relevant record been read from that table?
if ( ! isset($storedSourceRecs[$sourceTblName]) )
{
// Determine initializing table; see if source record
// has already been retrieved.
$sourceTblName = $newField->getInitTableName();
if ( ! isset($storedSourceRecs[$sourceTblName]) )
// Use viewing sequence to search for appropriate record.
$sourceRecord = $this->_getSourceRecord($setTable,
$sourceTblName, $data);
if ( $sourceRecord == null )
{
$sourceRecord =
$this->_getSourceRecord($setTable, $sourceTblName,
$data);
if ( $sourceRecord == null )
{
continue;
}
else
{
$storedSourceRecs[$sourceTblName] = $sourceRecord;
}
return null;
}
else
{
$storedSourceRecs[$sourceTblName] = $sourceRecord;
}
$sourceRecord = $storedSourceRecs[$sourceTblName];
$initializedData[$newFieldName] = $sourceRecord[$newFieldName];
}
$sourceRecord = $storedSourceRecs[$sourceTblName];
$initializedData[$newFieldName] = $sourceRecord[$newFieldName];
}

return $initializedData;
Expand All @@ -1189,6 +1182,13 @@ protected function _getSourceRecord($setTable, $sourceTblName, $userData)
// Need to retrieve initializing record; check that
// user provided enough information to find it.
$initRef = $setTable->getInitRefInfo($sourceTblName);
if ( $initRef == null )
{
$this->view->errMsgs[] =
"Cannot initialize fields from $sourceTblName -- " .
"no 'initTableRef` information provided.";
return null;
}
$sourceTbl = $initRef->getViewingSeq()->getSetTableForAdding();
$searchKeys = $initRef->xlFieldValuePairs($userData);
$matches = $sourceTbl->getTableEntries($searchKeys);
Expand All @@ -1197,9 +1197,9 @@ protected function _getSourceRecord($setTable, $sourceTblName, $userData)
// Cannot initialize with info provided; proceed
// directly to next field.
$this->view->errMsgs[] =
"Cannot initialize $newFieldName from " .
"$sourceTblName -- insufficient primary " .
"key information to find source record.";
"Cannot initialize fields from $sourceTblName -- " .
"insufficient primary key information to find unique " .
"source record.";
return null;
}
else
Expand All @@ -1208,6 +1208,52 @@ protected function _getSourceRecord($setTable, $sourceTblName, $userData)
}
}

/**
* Fill in initial values based on information in setting.
*
* @param Application_Model_SetTable table: setting & db info
* @param array $data Column-value pairs representing provided data
protected function _fillInitValues($setTable, array $data)
{
$storedSourceRecs = array();
$searchData = $data;
$initializedData = $data;
// Loop through fields in this table to see if any should be
// initialized from values in another table.
$relevantFields = $setTable->getExternallyInitFields();
foreach ( $relevantFields as $newFieldName => $newField )
{
// Initialize from another table if data not already provided.
if ( $data[$newFieldName] == null )
{
// Determine initializing table; see if source record
// has already been retrieved.
$sourceTblName = $newField->getInitTableName();
if ( ! isset($storedSourceRecs[$sourceTblName]) )
{
throw new Exception("Wanting to initialize fields from " . print_r($sourceTblName, true) . " using set table " . $setTable->getSettingName() . " and data " . print_r($data, true));
$sourceRecord =
$this->_getSourceRecord($setTable, $sourceTblName,
$data);
if ( $sourceRecord == null )
{
continue;
}
else
{
$storedSourceRecs[$sourceTblName] = $sourceRecord;
}
}
$sourceRecord = $storedSourceRecs[$sourceTblName];
$initializedData[$newFieldName] = $sourceRecord[$newFieldName];
}
}
return $initializedData;
}
*/

/**
* Acquires the lock for the given record in the specified table.
*
Expand Down
11 changes: 10 additions & 1 deletion application/devSettings/Smart/Curriculum/ModuleOfferingAdd.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ initTableRef.Modules.viewingSequence = Smart/Curriculum/Modules
initTableRef.Modules.match1.localField = "moduleID"
initTableRef.Modules.match1.externalField = "moduleID"

initTableRef.Terms.viewingSequence = Smart/Term/Terms
initTableRef.Terms.match1.localField = "term"
initTableRef.Terms.match1.externalField = "term"

field.term.label = "Term"
field.term.selectFrom = "Terms.term"

field.moduleID.label = "Module ID"
field.moduleID.selectUsing = "Smart/Curriculum/Modules"
field.section.label = "Section Number"


field.modCode.label = "Code"
field.modCode.initFrom = "Modules"
field.modNumber.label = "Number"
field.modNumber.initFrom = "Modules"

field.section.label = "Section Number"

field.shortTitle.label = "Short Title"
field.shortTitle.initFrom = "Modules"
field.longTitle.label = "Long Title"
Expand All @@ -31,6 +39,7 @@ field.capacity.label = "Capacity"
field.capacity.initFrom = "Modules"
field.type.label = "Module Type"
field.type.initFrom = "Modules"

field.startDate.label = "Start Date"
field.startDate.initFrom = "Terms"
field.endDate.label = "End Date"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ sequence.searchResultsSetting = Smart/Curriculum/ModuleOfferingSelection
[ View ]

tableName = "ModuleOfferings"
tableConnection.Modules = "ModuleOfferings.moduleID = Modules.moduleID"

tableTitle = "Module Offerings"
tableDescription = "Details about Specific Module Offerings/Sections"
Expand Down
Loading

0 comments on commit c2d3b8d

Please sign in to comment.