This is coming in 0.9.7; a consistent, object-oriented API that provides this capability to all the various parts of MODx. For now, duplicating those SQL calls and logic in the manager is about your best bet. If you wait a few more days, I can provide an example snippet that uses an xPDO-generated API for the existing MODx database tables, to do exactly what you are referring to. Here’s what the snippet code might look like running in an existing 0.9.5 deployment, as a preview:
<?php
$userId= isset ($userId) ? $userId : $modx->getLoginUserID();
if (!$userId= intval($userId)) {
$userId= 0;
}
$parentId= isset ($parentId) ? $parentId : $modx->documentObject['parent'];
$templateId= isset ($templateId) ? $templateId : $modx->documentObject['template'];
$formTpl= isset ($formTpl) ? $formTpl : 'xPublisher.default';
$dsn= $GLOBALS['database_type'] . ':host=' . $modx->db->config['host'] . ';dbname=' . str_replace('`', '', $modx->db->config['dbase']);
include_once ($modx->config['base_path'] . 'xpdo/xpdo.class.php');
$xpdo= new xPDO($dsn, $modx->db->config['user'], $modx->db->config['pass'], $modx->db->config['table_prefix']);
$xpdo->setPackage('modx095');
$xpdo->setLogLevel(XPDO_LOG_LEVEL_INFO);
$xpdo->setLogTarget('HTML');
$mode= 'insert';
if (isset ($_REQUEST['docId']) && $docId= intval($_REQUEST['docId']) && $document= $xpdo->getObject('modDocument', $docId)) {
$mode= 'update';
} else {
$docId= 0;
$document= $xpdo->newObject('modDocument');
}
$tvCollection= array ();
if (isset ($_POST['xpublish'])) {
$document->fromArray($_POST, 'document:');
if ($document->save()) {
$docId= $document->get('id');
if ($tvCollection= $document->getMany('modTemplateVar')) {
foreach ($tvCollection as $tvId => & $tvObj) {
$tvObj->setValue($docId, $_POST['tv' . $tvObj->get('name') . ':value']);
$tvObj->save();
}
}
$output.= '<p class="success">Your document was successfully saved.</p>';
} else {
$output.= '<p class="error">There was an error while attempting to save the document.</p>';
$xpdo->_log(XPDO_LOG_LEVEL_ERROR, "Error saving new document " . print_r($document->toArray(), true));
}
}
$output.= "\n\n\n<form action=\"[~[*id*]~]\" method=\"post\">\n";
$output.= $modx->parseChunk($formTpl, $document->toArray('document:'), '[+', '+]');
if (empty ($tvCollection)) {
$tvCollection= & $document->getMany('modTemplateVar');
}
if (!empty ($tvCollection)) {
foreach ($tvCollection as $tvId => $tvObj) {
$tvKey= 'tv' . $tvObj->get('name');
if ($tvObj->get('type') == 'richtext' || $tvObj->get('type') == 'htmlarea') {
if (is_array($replace_richtexteditor)) {
$replace_richtexteditor= array_merge($replace_richtexteditor, array ($tvKey));
} else {
$replace_richtexteditor= array ($tvKey);
}
}
$output .= "<label for=\"{$tvKey}\">{$tv['caption']}</label>\n";
$output .= $tvObj->renderInput($docId);
$output .= "<br />";
$output .= "\n\n";
}
}
$output .= "<input type=\"submit\" name=\"xpublish\" value=\"Publish\" />\n";
$output .= "<input type=\"reset\" name=\"xreset\" value=\"Reset Form\" />\n";
$output .= "</form>\n\n\n";
return $output;
In the meantime, there is another snippet that allows publishing with TV’s without the need for the new xPDO layer...I just need to remember what it’s called...