Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 125 additions & 9 deletions core/components/formit2db/elements/snippets/db2formit.snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@
$packagepath = $modx->getOption($packagename . '.core_path', null, $modx->getOption('core_path') . 'components/' . $packagename . '/');
$modelpath = $packagepath . 'model/';

// $joins = $modx->getOption('joins', $scriptProperties, '[]', true);
$joins = $modx->fromJson($modx->getOption('joins', $scriptProperties, '[]', true));

// Debug joins
/*
echo '<pre>';
var_dump($joins);
echo '</pre>';
*/

// Clear join Array and just maintain names
$joinCriteria = [];

foreach ($joins as $join) {
$className = key($join) !== 0 ? key($join) : $join[0];
$joinCriteria[$className] = [];
}


// AutoPackage
if ($autoPackage) {
$schemapath = $modelpath . 'schema/';
$schemafile = $schemapath . $packagename . '.mysql.schema.xml';
Expand All @@ -44,7 +64,7 @@
if (!is_dir($schemapath)) {
mkdir($schemapath, 0777);
}
// Use this to create a schema from an existing database
//Use this to create a schema from an existing database
if (!$generator->writeSchema($schemafile, $packagename, 'xPDOObject', $prefix, true)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not generate XML schema', '', 'db2FormIt Hook');
}
Expand All @@ -65,9 +85,109 @@
}
}

if (is_array($where)) {
if ($dataobject = $modx->getObject($classname, $where)) {
$formFields = $dataobject->toArray();


// if (is_array($where)) {

if(!$joins) {
$dataobject = $modx->getObject($classname, $where);
} else {
$dataobject = $modx->getCollectionGraph($classname, $joinCriteria, $where);
}



if ($dataobject) {

if ($joins && empty($dataobject)) {
$errorMsg = 'Failed to create object of type: ' . $classname;
$hook->addError('error_message', $errorMsg);
$modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'db2FormIt Hook');
return false;
} elseif ( !$joins && (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) ) {
$errorMsg = 'Failed to create object of type: ' . $classname;
$hook->addError('error_message', $errorMsg);
$modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'db2FormIt Hook');
return false;
}

if (empty($dataobject) && $notFoundRedirect) {
$modx->sendRedirect($modx->makeUrl($notFoundRedirect));
}


// -- handle joins
if($joins)
{
/* V1 - getCollectionGraph */
foreach ($dataobject as $obj)
{
if (is_object($obj))
{
$formFields = $obj->toArray();

foreach($joinCriteria as $jClassName => $value) {
$relObj = $obj->getFKDefinition($jClassName);
$relCardinality = $relObj['cardinality'];

// One
switch($relCardinality) {
case 'one':
$formFields = array_merge($formFields, $obj->toArray($jClassName.'.'));
break;
case 'many':
$i = 0;
foreach($obj->$jClassName as $subObj)
{
//print_r($rl->toArray()) ;
$formFields = array_merge($formFields, $subObj->toArray($jClassName.'.'.$i.'.'));
}
break;
}
}
}
}


/* V2 - loop individually */

/*
$joinObjs = [];
$joinSettings = [];
*/

/*
foreach ($joins as $join) {
// key => value or single value?
$className = key($join) !== 0 ? key($join) : $join[0];
$relObj = $dataobject->getFKDefinition($className);

if($relObj) {
$relClass = $relObj['class'];
$relCardinality = $relObj['cardinality'];

$joinObj = [];

switch ($relCardinality) {
case 'one' :
$joinObj = $dataobject->getOne($relClass,$paramname);
$joinObj = $joinObj->toArray($relClass.'.');
break;
case 'many' :
$joinObj = $dataobject->getMany($relClass);
break;
}


$formFields = array_merge($formFields, $joinObj);
}
}

*/

}
// END handle joins --

foreach ($formFields as $field => $value) {
if (in_array($field, $ignoreFields)) {
unset($formFields[$field]);
Expand All @@ -86,15 +206,11 @@
}
}
}

$hook->setValues($formFields);
} else {
if ($notFoundRedirect) {
$modx->sendRedirect($modx->makeUrl($notFoundRedirect));
}
}
} else {
if ($notFoundRedirect) {
$modx->sendRedirect($modx->makeUrl($notFoundRedirect));
}
}

Expand Down
Loading