diff --git a/core/components/formit2db/elements/snippets/db2formit.snippet.php b/core/components/formit2db/elements/snippets/db2formit.snippet.php index a36ddee..5bedfd8 100644 --- a/core/components/formit2db/elements/snippets/db2formit.snippet.php +++ b/core/components/formit2db/elements/snippets/db2formit.snippet.php @@ -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 '
';
+var_dump($joins);
+echo '
'; +*/ + +// 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'; @@ -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'); } @@ -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]); @@ -86,15 +206,11 @@ } } } + $hook->setValues($formFields); } else { if ($notFoundRedirect) { $modx->sendRedirect($modx->makeUrl($notFoundRedirect)); - } - } -} else { - if ($notFoundRedirect) { - $modx->sendRedirect($modx->makeUrl($notFoundRedirect)); } } diff --git a/core/components/formit2db/elements/snippets/formit2db.snippet.php b/core/components/formit2db/elements/snippets/formit2db.snippet.php index bba2ca2..361f7ff 100644 --- a/core/components/formit2db/elements/snippets/formit2db.snippet.php +++ b/core/components/formit2db/elements/snippets/formit2db.snippet.php @@ -12,6 +12,7 @@ * * FormIt2db snippet */ + $prefix = $modx->getOption('prefix', $scriptProperties, $modx->getOption(xPDO::OPT_TABLE_PREFIX), true); $packagename = $modx->getOption('packagename', $scriptProperties, '', true); $classname = $modx->getOption('classname', $scriptProperties, '', true); @@ -27,6 +28,56 @@ $packagepath = $modx->getOption($packagename . '.core_path', null, $modx->getOption('core_path') . 'components/' . $packagename . '/'); $modelpath = $packagepath . 'model/'; +/* TO DO +* +* `file` setting for (multiple) file/s +* `req` setting checking and error handling +* multi file upload - including formit2file +* implement formit2file +* alias-field separator -- currently '_', automatically through naming convention and HTML behavior +*/ + +/* Naming convention for fields: +* Alias prepended to field name, separated by point +* Example name="Alias.field" +*/ + +/* Joins must be JSON notification +* example: +* &joins=`[ +* {"CommentImages" : {"type" : "file" , "req" : "false"} }, +* {"CommentThumbnail" : {"type" : "file"} }, +* ["SingleClass"] +* ]` +*/ + +$joins = $modx->fromJson($modx->getOption('joins', $scriptProperties, '[]', true)); +$aliasSeparator = ''; + +// Debug joins +/* +echo '
';
+var_dump($joins);
+echo '
'; +*/ + +$packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/'); +$modelpath = $packagepath . 'model/'; + +// Set LogLevel for debugging +// $modx->setLogLevel(4); + +/* +$log_target = array( + 'target'=>'FILE', + 'options' => array( + 'filename'=>'my_custom.log' + ) +); +*/ + + + if ($autoPackage) { $schemapath = $modelpath . 'schema/'; $schemafile = $schemapath . $packagename . '.mysql.schema.xml'; @@ -64,6 +115,7 @@ } } + if (is_array($where)) { $dataobject = $modx->getObject($classname, $where); if (empty($dataobject)) { @@ -80,28 +132,186 @@ return false; } + + $formFields = $hook->getValues(); + +/* +echo '
';
+var_dump($formFields);
+echo '
'; + +die(); +*/ + +// Create Array with data for joined Objects +$joinObjs = []; +$joinSettings = []; + +foreach ($joins as $join) { + // Group Join fields in one array + $class = key($join) !== 0 ? key($join) : $join[0]; + $joinSettings[$class] = $join[$class]; + if (!array_key_exists($class, $formFields) && !is_array($formFields[$class]) ) { + $tmpArray = []; + foreach ($formFields as $field => $val) { + $skey = $class.'_'; + if( strpos($field, $skey) !== false) { + $sfield = substr($field, strlen($skey)); + $tmpArray[$sfield] = $val; + unset($formFields[$field]); + } + } + $joinObjs[$class] = $tmpArray; + + } elseif (array_key_exists($class, $formFields) && is_array($formFields[$class]) ) { + $joinObjs[$class] = $formFields[$class]; + unset($formFields[$class]); + } elseif (array_key_exists($class, $formFields) && isJson($formFields[$class]) ) { + $joinObjs[$class] = $modx->fromJson($formFields[$class]); + unset($formFields[$class]); + } +} + + +// Debug $formFields after join field removal +/* +echo '
';
+var_dump($formFields);
+echo '
'; + +echo '
';
+var_dump($joinObjs);
+echo '
'; + + +die(); +*/ + +// create $dataobject foreach ($formFields as $field => $value) { if (!in_array($field, $removeFields)) { if (in_array($field, $arrayFields)) { - switch ($arrayFormat) { - case 'json': - $value = json_encode($value); - break; - case 'csv' : - default : - $value = implode(',', $value); - break; - } + $value = getArrayFormat($value); } $dataobject->set($field, $value); } } + +// add joined classes to $dataobject +foreach ($joinObjs as $className => $classData) { + $relObj = $dataobject->getFKDefinition($className); + + if($joinSettings[$className]['type'] == 'file' && $classData['error'] == 4 ) { + $hasData = false; + } else { + $hasData = array_filter($classData); + } + + if ($relObj && $hasData) { + $relClass = $relObj['class']; + $relCardinality = $relObj['cardinality']; + + $addObj = $modx->newObject($relClass); + + foreach ($classData as $field => $value) { + if (!empty($value) && isJson($value)) { + $addObj->fromJson($value); + } elseif (is_array($value)) { + $value = getArrayFormat($value); + $addObj->set($field, $value); + } else { + $addObj->set($field, $value); + } + + } + + switch ($relCardinality) { + case 'one' : + $dataobject->addOne($addObj); + break; + case 'many' : + $dataobject->addMany($addObj); + break; + } + + /* Debugging + $a = $addObj->toArray(); + echo 'ADD OBJ
';
+        print_r($a); 
+        echo '
'; + */ + } elseif (!$hasData) { + // } elseif (!$hasData && $joinSettings[$className]['req'] == true) { + // $errorMsg = 'Data for a required field is missing!'; + // $hook->addError('error_message', $errorMsg); + $errorMsg = 'No submitted data for required field: ' . $className . '
. Skipping this one.'; + $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'FormIt2db Hook'); + + } else { + $errorMsg = 'Could not find related class: ' . $className . '
Skipping this one.'; + $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'FormIt2db Hook'); + } +} + + +// Debug dataobject +/* +echo 'DATAOBJ'; +$a = $dataobject->toArray(); + echo '
';
+        print_r($a); 
+        echo '
'; + + +die(); + + +$allFormFields = $hook->getValues(); + echo '
';
+        print_r($allFormFields); 
+        echo '
'; + +*/ + + if (!$dataobject->save()) { $errorMsg = 'Failed to save object of type: ' . $classname; $hook->addError('error_message', $errorMsg); $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'FormIt2db Hook'); return false; } -return true; \ No newline at end of file + +return true; + + +/* Functions */ + +function isJson($string) { + json_decode($string); + return (json_last_error() == JSON_ERROR_NONE); +} + +function getArrayFormat($value) { + switch ($arrayFormat) { + case 'json': + $value = json_encode($value); + break; + case 'csv' : + default : + $value = implode(',', $value); + break; + } + return $value; +}; + + +// Not needed +/* +function flatten_array(array $array) { + $return = array(); + array_walk_recursive($array, function($a,$b) use (&$return) { $return[$b] = $a; }); + return $return; +}; +*/ \ No newline at end of file