require_once('core/components/docbuilder/moddocumentbuilder.class.inc.php');
$doc = new modDocumentBuilder();
$doc->set('parent',$folder);
$doc->set('alias','post'.strftime('%Y-%m-%d %H:%M:%S'));
$doc->set('content','document content');
$doc->set('template','GuestBookComments'); // template must be set before TV's
$doc->set('tvComment','post to comment template variable'); // beware: template must be set before TV's
$doc->setResourceGroup('Group',true); // add (in case of 'true') document to resource group Group
// ('false' is used to remove a document from a resource group)
// using the group-id to set the resource group is also permitted
$doc->save(); // document will be created in MODx with indicated values
$idDoc = $doc->get('id'); // to get the id of the document; for new documents this can only be done after save()
$urlDoc = $doc->get('preview_url'); // to get the preview_url (WARNING: will not always deliver right result) $doc = new Document($id); // $id = id of the existing document
$doc->set('content','document content');
$doc->save(); // document will be created with indicated values$doc = new Document($id); // $id = id of the existing document $doc->delete();
$doc = new Document($id); // $id = id of the existing document
$pagetitle=$doc->get('pagetitle');$doc = new Document($id); // $id = id of the existing document $groups=$doc->getResourceGroups(); // resturns array of resource group names
$doc = new Document($id); // $id = id of the existing document
$MyTvValue=$doc->get('tvMyTvName');//add "tv" before the name of your tv.
$doc = new Document($Id_Of_Document_You_Want_duplicate); $doc->duplicate(); $doc->save();
$doc = new Document($Id); // id of existing document $childrenArray = $doc->getChildren();
$default_template = $modx->getOption('default_template');
$r = $modx->newObject('modResource');
$r->set('class_key','modResource');
$r->set('context_key','web');
$r->set('type','document');
$r->set('contentType','text/html');
$r->set('pagetitle','Sample FAQ Page');
$r->set('longtitle','Sample FAQ Page');
$r->set('description','Sample FAQ Page');
$r->set('alias','faq');
$r->set('published','1');
$r->set('parent','0');
$r->set('isfolder','1');
$r->setContent('[[EZfaq]]');
$r->set('richtext','0');
$r->set('menuindex','99');
$r->set('searchable','1');
$r->set('cacheable','1');
$r->set('menutitle','FAQ');
$r->set('donthit','0');
$r->set('hidemenu','0');
$r->set('template',$default_template);
$r->save();in MODx/xPDO:
Example of use (creating a new document):
require_once('core/components/docbuilder/moddocumentbuilder.class.inc.php'); $doc = new modDocumentBuilder(); $doc->set('parent',$folder); $doc->set('alias','post'.strftime('%Y-%m-%d %H:%M:%S')); $doc->setResourceGroup('Group',true); // add (in case of 'true') document to resource group Group // ('false' is used to remove a document from a resource group) // using the group-id to set the resource group is also permitted $doc->set('syncsite', true); // to make sure that the cache is cleared after the document is saved $doc->save(); // document will be created in MODx with indicated values $idDoc = $doc->get('id'); // to get the id of the document; for new documents this can only be done after save() $urlDoc = $doc->get('preview_url'); // to get the preview_url (WARNING: will not always deliver right result)
$doc = $modx->newObject('modDocument');
$doc->set('parent',$folder);
$doc->set('alias',$alias);
$doc->set('published',true);
$doc->save();
$modx->cacheManager->clearCache();
$url = $modx->makeUrl($doc->get('id'));
in MODx/xPDO:
To change (update) an existing document:
$doc = new Document($id); // $id = id of the existing document $doc->set('content','document content'); $doc->save(); // document will be created with indicated values
$doc = $modx->getObject('modResource',$id);
$doc->setContent($content);
$doc->save();
in MODx/xPDO:
To delete an existing document (with all its children!):
$doc = new Document($id); // $id = id of the existing document $doc->delete();
$doc = $modx->getObject('modResource');
$doc->set('deleted',true);
/* or permanently delete it */
$doc->remove();
To get the value of a field of an existing document:in MODx/xPDO:
$doc = new Document($id); // $id = id of the existing document $pagetitle=$doc->get('pagetitle');
$doc = $modx->getObject('modResource',$id);
$pagetitle = $doc->get('pagetitle');
in MODx/xPDO:
To get an overview of the resource groups the resource belongs to:
$doc = new Document($id); // $id = id of the existing document $groups=$doc->getResourceGroups(); // resturns array of resource group names
$doc = $modx->getObject('modResource',$id);
$resourceGroups = $modx->getMany('ResourceGroupResources');
/* returns groups as modResourceGroupResource objects, this can also just get names via: */
$c = $modx->newQuery('modResourceGroup');
$c->innerJoin('modResourceGroupResource','ResourceGroupResources');
$c->where(array(
'ResourceGroupResources.document' => $doc->get('id');
));
$resourceGroups = $modx->getCollection('modResourceGroup',$c);
/* and to get the names */
$names = array();
foreach ($resourceGroups as $resourceGroup) {
$names[] = $resourceGroup->get('name');
}
in MODx/xPDO:
To get a tv value from a document:
$doc = new Document($id); // $id = id of the existing document $MyTvValue=$doc->get('tvMyTvName');//add "tv" before the name of your tv.
$doc = $modx->getObject('modResource',$id);
$value = $doc->getTVValue('MyTvName');
in MODx/xPDO:
To duplicate a document (documents are duplicated with their tv values,
but without their children):
$doc = new Document($Id_Of_Document_You_Want_duplicate); $doc->duplicate(); $doc->save();
$doc = $modx->getObject('modResource',$id);
$newDoc = $modx->newObject('modDocument');
$newDoc->fromArray($doc->toArray());
$newDoc->save();
in MODx/xPDO:
To get an array of the id’s of the (direct) children of a document use the following:
$doc = new Document($Id); // id of existing document $childrenArray = $doc->getChildren();
$doc = $modx->getObject('modResource',$id);
$doc->getMany('Children'); /* returns children as modResource objects */
Neat, but xPDO already does most of this:Ok, clear.
Set basic parameters of documentIf only these kinds of statements are needed, the direct xPDO functions should indeed be used (although there were some extra checks in the processors that I also included in this class...).
in modDocumentBuilder:
$doc = new modDocumentBuilder();
$doc->set(’parent’,$folder);
$doc->set(’alias’,’post’.strftime(’%Y-%m-%d %H:%M:%S’));
in MODx/xPDO:
$doc = $modx->newObject(’modDocument’);
$doc->set(’parent’,$folder);
$doc->set(’alias’,$alias);
$doc->set(’syncsite’, true);Sorry, this should have been removed from my example; to clear the cache is the default behavior in the class.
Get URLEuh, I think that (the current implementation of) makeUrl will not deliver the url if used in this way (right after creating the resource). See the following forum discussion: http://modxcms.com/forums/index.php/topic,46524.0.html.
modDocumentBuilder:
$urlDoc = $doc->get(’preview_url’);
xPDO:
$url = $modx->makeUrl($doc->get(’id’));
Delete document:According to my testing this xPDO statement does not fill in "deletedby" and does not delete the children of the document.
$doc->set(’deleted’,true);
Get simple document parametersI agree, for these simple kind of statements, the xPDO functions should be used.
modDocumentBuilder:
$doc = new Document($id); // $id = id of the existing document
$pagetitle=$doc->get(’pagetitle’);
MODx/xPDO:
$doc = $modx->getObject(’modResource’,$id);
$pagetitle = $doc->get(’pagetitle’);
Get resourcegroupsHere the xPDO code is more complicated; perhaps an extra function could be useful.
modDocumentBuilder:
$doc = new Document($id); // $id = id of the existing document
$groups=$doc->getResourceGroups(); // resturns array of resource group names
MODx/xPDO:
$doc = $modx->getObject(’modResource’,$id);
$resourceGroups = $modx->getMany(’ResourceGroupResources’);
/* returns groups as modResourceGroupResource objects, this can also just get names via: */
$c = $modx->newQuery(’modResourceGroup’);
$c->innerJoin(’modResourceGroupResource’,’ResourceGroupResources’);
$c->where(array(
’ResourceGroupResources.document’ => $doc->get(’id’);
));
$resourceGroups = $modx->getCollection(’modResourceGroup’,$c);
/* and to get the names */
$names = array();
foreach ($resourceGroups as $resourceGroup) {
$names[] = $resourceGroup->get(’name’);
}
Get template variableI must admit that I didn’t know this function yet.
in xPDO:
$doc = $modx->getObject(’modResource’,$id);
$value = $doc->getTVValue(’MyTvName’);
QuoteThe two versions of the example code will not accomplish the same.
To duplicate a document
modDocumemtBuilder:
$doc = new Document($Id_Of_Document_You_Want_duplicate);
$doc->duplicate();
$doc->save();
MODx/xPDO:
$doc = $modx->getObject(’modResource’,$id);
$newDoc = $modx->newObject(’modDocument’);
$newDoc->fromArray($doc->toArray());
$newDoc->save();
I think it might be a bit unnecessary to use an assistance class like you’ve created, as it would add a whole bunch of extra overhead.That would be great!
That said, we can definitely look into discussing extra helper methods for the mod* classes.
Kudos for hacking into MODx Revo!Thanks!
define('MODX_CORE_PATH', 'core/');
define('MODX_CONFIG_KEY', 'config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
require_once('core/components/docbuilder/moddocumentbuilder.class.inc.php');
$modx= new modX();
$modx->initialize('mgr');
$modx->db->query('alter table modx_site_content auto_increment=0');
//Удаление старого каталога
$doc=new modDocumentBuilder('5');
$doc->delete();
Hi, everyone! This code:
define('MODX_CORE_PATH', 'core/'); define('MODX_CONFIG_KEY', 'config'); require_once MODX_CORE_PATH . 'model/modx/modx.class.php'; require_once('core/components/docbuilder/moddocumentbuilder.class.inc.php'); $modx= new modX(); $modx->initialize('mgr'); $modx->db->query('alter table modx_site_content auto_increment=0'); //Удаление старого каталога $doc=new modDocumentBuilder('5'); $doc->delete();
returns Fatal error: Call to undefined function getchildren() in <server>\www\core\components\docbuilder\moddocumentbuilder.class.inc.php on line 406... Any ideas?
$modx->getChildIds($id);
$resource->getMany('Children');
)
if ($loop) $this->_getChildren($child, $ar_children, $loop, $delcheck);