Applying Document Groups to new Documents
I added some Code to ’fdm.db.class.inc.php’ which I got from the ’NewsPublisher’ Snippet.
With it the newly created Pages inherit their parents’ Document Groups.
Can anyone validate that this is actually happening

and maybe if the code is ok that way?
Here’s the code, I changed in fdm.db.class.inc.php:
[...]
/***************************************************
Saving/Updating FdmDocument (escape fields added)
****************************************************/
function Save(){ //escape added
global $modx;
$this->fields['editedon']=time();
$tablename=$modx->getFullTableName('site_content');
$escapedFields = array_map(create_function('$n', 'global $modx;if (get_magic_quotes_gpc()) { $n = stripslashes($n); }return $modx->db->escape($n);'), $this->fields);
if($this->isNew){
$this->fields['id']=$modx->db->insert($escapedFields, $tablename);
$this->isNew = false;
} else {
$id=$this->fields['id'];
$modx->db->update($escapedFields, $tablename, "id=$id");
}
unset($escapedFields);// free the array
//
// ###########################
// a little hack from end if
// ###########################
// Add the documents parent Document Groups to the document
//
$folder = $this->fields['parent'];
//
// got the following code from Newseditor 1.4
//
// Doc group thing
// look in save_content.processor.php for tips (or below)
$lastInsertId = $modx->db->getInsertId();
// Get doc groups based on $folder (parent id)
$parentDocGroupsSql = "SELECT * FROM " . $modx->getFullTableName('document_groups'). " where document=".$folder;
$parentDocGroupsRs = $modx->db->query($parentDocGroupsSql);
$parentDocGroupsLimit = $modx->db->getRecordCount($parentDocGroupsRs);
for ($pdgi = 0; $pdgi < $parentDocGroupsLimit; $pdgi++) {
$currentDocGroup = $modx->db->getRow($parentDocGroupsRs);
$parentDocGroupsArray[$pdgi] = $currentDocGroup['document_group'];
}
// put the document in the document_groups it should be in
// check that up_perms are switched on!
if($modx->config['use_udperms']==1) {
if(is_array($parentDocGroupsArray)) {
foreach ($parentDocGroupsArray as $dgKey=>$dgValue) {
$insertDocSql = "INSERT INTO ".$modx->getFullTableName('document_groups')."(document_group, document) values(".stripslashes($dgValue).", $lastInsertId)";
$insertDocRs = $modx->db->query($insertDocSql);
if(!$insertDocRs){
exit;
}
} // end foreach
} // end if doc group array exists
} // end if perms are used
//
// Handle privateweb
$modx->db->query("UPDATE ".$modx->getFullTableName("site_content")." SET privateweb = 0 WHERE id='$lastInsertId';");
$privatewebSql = "
SELECT DISTINCT ".$modx->getFullTableName('document_groups').".document_group
FROM ".$modx->getFullTableName('document_groups').", ".$modx->getFullTableName('webgroup_access')."
WHERE
".$modx->getFullTableName('document_groups').".document_group = ".$modx->getFullTableName('webgroup_access').".documentgroup
AND
".$modx->getFullTableName('document_groups').".document = $lastInsertId;";
$privatewebIds = $modx->db->getColumn("document_group",$privatewebSql);
if(count($privatewebIds)>0) {
$modx->db->query("UPDATE ".$modx->getFullTableName("site_content")." SET privateweb = 1 WHERE id = $lastInsertId;");
}
// And privatemgr
$modx->db->query("UPDATE ".$modx->getFullTableName("site_content")." SET privatemgr = 0 WHERE id='$lastInsertId';");
$privatemgrSql = "
SELECT DISTINCT ".$modx->getFullTableName('document_groups').".document_group
FROM ".$modx->getFullTableName('document_groups').", ".$modx->getFullTableName('membergroup_access')."
WHERE
".$modx->getFullTableName('document_groups').".document_group = ".$modx->getFullTableName('membergroup_access')." .documentgroup
AND
".$modx->getFullTableName('document_groups').".document = $lastInsertId;";
$privatemgrIds = $modx->db->getColumn("document_group",$privatemgrSql);
if(count($privatemgrIds)>0) {
$modx->db->query("UPDATE ".$modx->getFullTableName("site_content")." SET privatemgr = 1 WHERE id = $lastInsertId;");
}
// end of document_groups stuff!
//
// ###########################
// / hack
// ###########################
//
if(is_array($this->tvs)) $this->saveTVs();
}
/************************************************************
Saving TV values, maintenance function. Only $tvNames values are saved,
If a TV exists in oldTVs, then updating, else inserting
*************************************************************/
[...]
Cheers,
Sascha