example with formit:
[[!FormIt?
&hooks=`spam,formit2resource,email`
&preHooks=`resource2formit`
&resource2formitfields=`parent,pagetitle,strasse,content`
&emailTpl=`myEmailChunk`
&emailTo=`[email protected]`]]
<h2>Resource Form</h2>
<p>[[+fi.error.error_message]]</p>
<form class="form" action="[[~[[*id]]]]" method="post">
<input name="nospam:blank" type="hidden" />
<input name="resource_id" type="hidden" value="[[+fi.id]]" />
<label for="parent">
Parent:
<span class="error">[[+fi.error.parent]]</span>
</label>
<input id="parent" name="parent:required" type="text" value="[[+fi.parent]]" />
<br/>
<label for="pagetitle">
Pagetitle:
<span class="error">[[+fi.error.pagetitle]]</span>
</label>
<input id="pagetitle" name="pagetitle:required" type="text" value="[[+fi.pagetitle]]" />
<br/>
<label for="content">
Content:
<span class="error">[[+fi.error.content]]</span>
</label>
<textarea id="content" cols="55" rows="7" name="content:required:stripTags">[[+fi.content]]</textarea>
<br class="clear" />
<div class="form-buttons">
<input type="submit" value="Create Resource" />
</div>
</form>
code for snippet formit2resource:
<?php
$doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
if (empty($doc)){
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
}
else{
$doc->set('editedby', $modx->user->get('id'));
}
$allFormFields = $hook->getValues();
foreach ($allFormFields as $field=>$value)
{
if ($field !== 'spam' && $field !== 'resource_id'){
$doc->set($field, $value);
}
}
$doc->set('template', '1');
$doc->save();
foreach ($allFormFields as $field=>$value)
{
if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
{
/* handles checkboxes & multiple selects elements */
if (is_array($value)) {
$featureInsert = array();
while (list($featureValue, $featureItem) = each($value)) {
$featureInsert[count($featureInsert)] = $featureItem;
}
$value = implode('||',$featureInsert);
}
$tv->setValue($doc->get('id'), $value);
$tv->save();
}
}
return true;
?>
resource2formit:
<?php
if (isset($_GET['resId'])){
if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
$docarray=$doc->toArray();
$fields = explode(',',$scriptProperties['resource2formitfields']);
$fields[] = 'id';
foreach ($fields as $field){
if ($doc->getFieldName($field) === null) {
/* if field isnt defined, look for TV value */
$tvValue = $doc->getTVValue($field);
if ($tvValue !== null) {
$hook->setValue($field,$tvValue);
}
} else {
/* otherwise get field value */
$hook->setValue($field,$docarray[$field]);
}
}
}
//$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';
//$hook->addError('error_message',$errorMsg);
}
return true;
[ed. note: Bruno17 last edited this post 9 years, 3 months ago.]