<?php $modx->event->alert("Help!"); ?>


<?php
/*
This plugin makes select TVs blank for the document that is created when another document is duplicated.
Then when the parent document that was just created is saved, for the first time, the select TVs are
populated with the parent document's TV values.
*/
/* CHANGE THESE */
$select_tv_id = SELECT_TV_ID;// SELECT_TV_ID
$folder_doc_id = FOLDER_DOC_ID;// SPECIFIC_FOLDER_DOC_ID
$keywords_tv_id = KEYWORDS_TV_ID;// KEYWORDS_TV_ID
// Doc id of the document that is going to be duplicated
$duplicate_doc_id = $id;
// Doc id of the document that was just saved
$save_doc_id = $id;
$e = & $modx->Event;
$highest_doc_id = '';
$empty = '';
$temp = 'TEMP';
$new_parent_id = '';
$new_children_ids = array();
switch ($e->name)
{
case "OnBeforeDocDuplicate":
// IF the doc has the select TV ($select_tv_id) and if the doc's parent is Specific_Folder ($folder_doc_id): Get the highest Doc ID and store it
$is_in_specific_folder = false;
$has_select_tv = false;
// so it only does this for the first document being duplicated
if( !isset($_REQUEST['first_time']) )
{
// checks if it is a child of the Specific_Folder, it isn't a grandchild, great-grandchild, but is a child
$sql = 'SELECT *
FROM `modx_site_content`
WHERE `id` = \''. $duplicate_doc_id .'\'
AND `parent` = \''. $folder_doc_id .'\' ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$is_in_specific_folder = true;
// checks if it has the Select_TV
$sql = 'SELECT *
FROM `modx_site_tmplvar_contentvalues`
WHERE `contentid` = \''. $duplicate_doc_id .'\'
AND `tmplvarid` = \''. $select_tv_id .'\' ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$has_select_tv = true;
// If it has the Select_TV and is a child of Specific_Folder
if($is_in_specific_folder == true && $has_select_tv == true)
{
// gets the highest Doc id for the site, because the current highest Doc id will be just less than the docs that will be created
$sql = 'SELECT *
FROM `modx_site_content`
ORDER BY `id` DESC
LIMIT 1 ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$highest_doc_id = $row['id'];
// store it
$_REQUEST['highest_doc_id'] = $highest_doc_id;
// gets the number of children that will be duplicated
$holder_arr = array();
$_REQUEST['number_duplicate_children'] = 0;
$holder_arr = $modx->getChildIds($duplicate_doc_id, 15);
foreach($holder_arr as $key => $value)
{
$_REQUEST['number_duplicate_children'] = $_REQUEST['number_duplicate_children'] + 1;
}
// count down is used to count down to the last page that is being created
// on the last page that is created, do the changes
$_REQUEST['count_down'] = $_REQUEST['number_duplicate_children'];
}
$_REQUEST['first_time'] = true;
}
else
$_REQUEST['first_time'] = false;
// just for testing
//$modx->event->alert( 'Highest Doc ID: '. $highest_doc_id );
break;
case "OnDocDuplicate":
// just for testing
$holder_var = '';
// Get the highest Doc ID if it was stored, if it wasn't stored don't do anything
if( isset($_REQUEST['highest_doc_id']) )
{
$highest_doc_id = $_REQUEST['highest_doc_id'];
// if it's the first time
if($_REQUEST['first_time'] == true)
{
// Get the new Parent ID, it will be the Doc id just above the $highest_doc_id
$sql = 'SELECT *
FROM `modx_site_content`
WHERE `id` > \''. $highest_doc_id .'\'
LIMIT 1 ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$new_parent_id = $row['id'];
// store it
$_REQUEST['new_parent_id'] = $new_parent_id;
}
else
$new_parent_id = $_REQUEST['new_parent_id'];
// If it is the after the last doc was created from the duplicate and there is a $new_parent_id
if($_REQUEST['count_down'] == 0 && isset($_REQUEST['new_parent_id']) )
{
// Get the new children's IDs
$sql = 'SELECT *
FROM `modx_site_content`
WHERE `id` > \''. $new_parent_id .'\' ';
$results = mysql_query($sql);
while( $row = mysql_fetch_assoc($results) )
{
$new_children_ids[] = $row['id'];
}
/* start changing things for the docs that were created from the duplicate */
$errors = '';
$where_description = 'WHERE ';// parent and children
$where_tv_keywords = 'WHERE ';// parent and children
$where_select_tv_children = 'WHERE ';// just children
// just for testing
foreach($new_children_ids as $key => $value)
{
$holder_var .= $value .' ';
}
// creates the WHERE clause of the sql statement
$i = 0;
foreach($new_children_ids as $key => $value)
{
if($i != 0)
{
$where_description .= 'OR ';
$where_tv_keywords .= 'OR ';
$where_select_tv_children .= 'OR ';
}
$where_description .= '`id` = \''. $value .'\' ';
$where_tv_keywords .= '`id` = \''. $value .'\' ';
$where_select_tv_children .= '`id` = \''. $value .'\' ';
$i++;
}
// adds the $new_parent_id to the WHERE clause
if($i != 0)
{
$where_description .= 'OR ';
$where_tv_keywords .= 'OR ';
}
$where_description .= '`id` = \''. $new_parent_id .'\' ';
$where_tv_keywords .= '`id` = \''. $new_parent_id .'\' ';
// makes the description empty for the created docs
$sql_description = 'UPDATE `modx_site_content`
SET `description` = \''. $empty .'\'
'. $where_description .' ';
// makes the keywords_tv empty for the create docs
$sql_tv_keywords = 'UPDATE `modx_site_tmplvar_contentvalues`
SET `value` = \''. $empty .'\'
WHERE `tmplvarid` = \''. $keywords_tv_id .'\'
AND `contentid` IN (SELECT `id` FROM `modx_site_content` '. $where_tv_keywords .') ';
// makes the Select_TV empty just for the parent doc that was created
$sql_select_tv_id_parent = 'UPDATE `modx_site_tmplvar_contentvalues`
SET `value` = \''. $empty .'\'
WHERE `tmplvarid` = \''. $select_tv_id .'\'
AND `contentid` = \''. $new_parent_id .'\' ';
// makes the Select_TV empty just for the children docs that were created
$sql_select_tv_id_children = 'UPDATE `modx_site_tmplvar_contentvalues`
SET `value` = \''. $temp .'\'
WHERE `tmplvarid` = \''. $select_tv_id .'\'
AND `contentid` IN (SELECT `id` FROM `modx_site_content` '. $where_select_tv_children .') ';
// just for testing
$holder_var .= '<br />'. $sql_description .'<br /><br />'. $sql_tv_keywords .'<br /><br />'. $sql_select_tv_id_parent;
// execute the queries
if(mysql_query($sql_description) == false)
{
$errors .= mysql_error();
}
if(mysql_query($sql_tv_keywords) == false)
{
$errors .= mysql_error();
}
if(mysql_query($sql_select_tv_id_parent) == false)
{
$errors .= mysql_error();
}
// if $where_select_tv_children == 'WHERE ', there are NO children, so do NOT do the query because if you do, it will change every single value for the Select_TV!!!
if($where_select_tv_children != 'WHERE ')
{
$holder_var .= '<br /><br />'. $sql_select_tv_id_children;
if(mysql_query($sql_select_tv_id_children) == false)
{
$errors .= mysql_error();
}
}
// show any errors
if($errors != '')
$modx->event->alert( $errors );
}
$_REQUEST['count_down'] = $_REQUEST['count_down'] - 1;
// just for testing
//$modx->event->alert( 'New Parent Doc ID: '. $new_parent_id .' Highest Doc ID: '. $highest_doc_id .'<br />Children:<br />'. $holder_var );
}
break;
case "OnDocFormSave":
// IF Doc has Select_TV ($select_tv_id) and if Doc's parent is $folder_doc_id:
// check if has children
$parent_select_tv_value = false;
$is_in_specific_folder = false;
// checks if it has the Select_TV and gets the value if it is
$sql = 'SELECT *
FROM `modx_site_tmplvar_contentvalues`
WHERE `contentid` = \''. $save_doc_id .'\'
AND `tmplvarid` = \''. $select_tv_id .'\' ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$parent_select_tv_value = $row['value'];
// checks if it is a child of the Specific_Folder, it isn't a grandchild, great-grandchild, but is a child
$sql = 'SELECT *
FROM `modx_site_content`
WHERE `id` = \''. $duplicate_doc_id .'\'
AND `parent` = \''. $folder_doc_id .'\' ';
$results = mysql_query($sql);
if( $row = mysql_fetch_assoc($results) )
$is_in_specific_folder = true;
// makes empty just for testing
$sql = '';
if($parent_select_tv_value != false && $is_in_specific_folder == true)
{
// get the children ids
$holder_child_ids = '';
$new_children_ids = $modx->getChildIds($save_doc_id, 15);
// assign the children ids to $holder_child_ids, to be used in the sql query
$i = 0;
foreach($new_children_ids as $key => $value)
{
if($i != 0)
$holder_child_ids .= ', ';
$holder_child_ids .= $value;
$i++;
}
// only do update to Select_TV if the doc that was created from the duplicate has children
if($holder_child_ids != '')
{
$errors = '';
/* I thought it would be best to only change the Select_TV of the children on the first save, instead of everytime */
$sql = 'UPDATE `modx_site_tmplvar_contentvalues`
SET `value` = \''. $parent_select_tv_value .'\'
WHERE `tmplvarid` = \''. $select_tv_id .'\'
AND `contentid` IN ('. $holder_child_ids .')
AND `value` = \''. $temp .'\' ';
/* you can take out
"AND `value` = \''. $temp .'\'"
to change the children Select_TV everytime, but I only want to change it the first time, see note above */
// execute query
if(mysql_query($sql) == false)
{
$errors .= mysql_error();
}
// show any errors
if($errors != '')
$modx->event->alert( $errors );
}
}
// just for testing
//$modx->event->alert( 'Saving Doc ID: '. $save_doc_id .'<br />Website ID: '. $parent_select_tv_value .'<br />Child IDs: '. $holder_child_ids .'<br />SQL: '. $sql );
break;
}
return;

<?php
/*
++++WARNING++++ In order for this to be fool proof, make the select TV required.
This plugin makes select TVs blank for the resource that is created when another resource is duplicated.
Then when the parent resource that was just created is saved, for the first time, the select TVs for the
children are populated with the parent resource's TV values.
*/
/* CHANGE THESE */
$select_tv_id = SELECT_TV_ID;// SELECT_TV_ID (the id of the TV that you want to perpetuate the value down to child)
$folder_doc_id = SPECIFIC_FOLDER_DOC_ID;// SPECIFIC_FOLDER_DOC_ID, set to 0 (zero) if not worried about limiting to a specific folder (the id of the folder the resources are in that you want to limit the perpetuating of the TV)
$keywords_tv_id = KEYWORDS_TV_ID;// KEYWORDS_TV_ID (the id of the TV that is used for keywords)
$e_name = $modx->event->name;
$empty = '';
$temp = 'TEMP';
switch ($e_name)
{
case 'OnResourceDuplicate':
$childIds = array();
// gets the children ids because $modx->getChildIds doesn't work in OnResourceDuplicate
$sql = 'SELECT *
FROM `z_modx_site_content`
WHERE `id` >= \''. $newResource->get('id') .'\' ';
$results = $modx->db->query($sql);
$number_resources = $modx->db->getRecordCount($results);
while( $row = $modx->db->getRow($results) )
{
if($row['id'] != $newResource->get('id'))
$childIds[] = $row['id'];
else
$new_parent_id = $row['parent'];// gets the parent id because $modx->getParentIds doesn't work in OnResourceDuplicate
}
if($folder_doc_id == 0 || $new_parent_id == $folder_doc_id)
{
$valid = true;
}
else
$valid = false;
if($valid == true)
{
// set switch
$_SESSION['valid_duplicate'] = true;
$_SESSION['new_id'] = $newResource->get('id'); // in case person in manager decides to edit another resource inbetween duplicating and editing the newly created resource
$_SESSION['children_ids'] = $childIds; // saved for OnDocFormSave
// 1 = makes the select_TV empty just for the parent resource that was created
//
// 2 = makes the select_TV the same as $temp just for the children resources that were created
//
// 3 = makes the keywords_tv empty for the created resources
//
// 4 = makes the description empty for the created resources
$select_tv = $modx->getObject('modTemplateVar', $select_tv_id);
$keywords_tv = $modx->getObject('modTemplateVar', $keywords_tv_id);
$select_tv->setValue( $newResource->get('id'), $empty);// 1
$keywords_tv->setValue( $newResource->get('id'), $empty);// 3
$select_tv->save();
$keywords_tv->save();
foreach($childIds as $key => $childId)
{
$select_tv->setValue($childId, $temp);// 2
$keywords_tv->setValue($childId, $empty);// 3
// need to save each time
$select_tv->save();
$keywords_tv->save();
}
// 4
$sql = 'UPDATE `z_modx_site_content`
SET `description` = \''. $empty .'\'
WHERE `id` >= \''. $newResource->get('id') .'\' ';
$results = $modx->db->query($sql);
}
else
$_SESSION['valid_duplicate'] = false;
break;
case 'OnDocFormSave':
if( isset($_SESSION['valid_duplicate']) && $_SESSION['valid_duplicate'] == true && $_SESSION['new_id'] == $id)
{
// 1 = makes the select_TV the same as $new_tv_value just for the children resources that were created
$select_tv = $modx->getObject('modTemplateVar', $select_tv_id);
$new_tv_value = $resource->getTVValue($select_tv_id);// the value of select TV to give to all the children
if( isset($_SESSION['children_ids']) )
{
$childIds = $_SESSION['children_ids'];
foreach($childIds as $childId)
{
$select_tv->setValue($childId, $new_tv_value);// 1
// must save everytime
$select_tv->save();
}
}
}
if($_SESSION['new_id'] == $id)
{
// unset switch, so only perpetuates select TV down to children the first time
if( isset($_SESSION['valid_duplicate']) )
{
unset( $_SESSION['valid_duplicate'] );
unset( $_SESSION['children_ids'] );
unset( $_SESSION['new_id'] );
}
}
break;
}
return;<?php
/*
++++WARNING++++ In order for this to be fool proof, make the select TV required and have the default value set to @INHERIT.
This plugin makes select TVs blank for the resource that is created when another resource is duplicated.
Then when the parent resource that was just created is saved, for the first time, the select TVs for the
children are populated with the parent resource's TV values.
*/
/* CHANGE THESE */
$select_tv_id = SELECT_TV_ID;// SELECT_TV_ID (the id of the TV that you want to perpetuate the value down to child)
$folder_doc_id = SPECIFIC_FOLDER_DOC_ID;// SPECIFIC_FOLDER_DOC_ID, set to 0 (zero) if not worried about limiting to a specific folder (the id of the folder the resources are in that you want to limit the perpetuating of the TV)
$keywords_tv_id = KEYWORDS_TV_ID;// KEYWORDS_TV_ID (the id of the TV that is used for keywords)
$e_name = $modx->event->name;
$empty = '';
$temp = 'TEMP';
$inherit = '@INHERIT';
switch ($e_name)
{
case 'OnResourceDuplicate':
$childIds = array();
// gets the children ids because $modx->getChildIds doesn't work in OnResourceDuplicate
$sql = 'SELECT *
FROM `z_modx_site_content`
WHERE `id` >= \''. $newResource->get('id') .'\' ';
$results = $modx->db->query($sql);
$number_resources = $modx->db->getRecordCount($results);
while( $row = $modx->db->getRow($results) )
{
if($row['id'] != $newResource->get('id'))
$childIds[] = $row['id'];
else
$new_parent_id = $row['parent'];// gets the parent id because $modx->getParentIds doesn't work in OnResourceDuplicate
}
if($folder_doc_id == 0 || $new_parent_id == $folder_doc_id)
{
$valid = true;
}
else
$valid = false;
if($valid == true)
{
// set switch
$_SESSION['valid_duplicate'] = true;
$_SESSION['new_id'] = $newResource->get('id'); // in case person in manager decides to edit another resource inbetween duplicating and editing the newly created resource
$_SESSION['children_ids'] = $childIds; // saved for OnDocFormSave
// 1 = makes the select_TV empty just for the parent resource that was created
//
// 2 = makes the select_TV the same as $temp just for the children resources that were created
// 2.1 = makes the select_TV the same as $inherit just for the children resources that were created
//
// 3 = makes the keywords_tv empty for the created resources
//
// 4 = makes the description empty for the created resources
$select_tv = $modx->getObject('modTemplateVar', $select_tv_id);
$keywords_tv = $modx->getObject('modTemplateVar', $keywords_tv_id);
$select_tv->setValue( $newResource->get('id'), $empty);// 1
$keywords_tv->setValue( $newResource->get('id'), $empty);// 3
$select_tv->save();
$keywords_tv->save();
foreach($childIds as $key => $childId)
{
//$select_tv->setValue($childId, $temp);// 2
$select_tv->setValue($childId, $inherit);// 2.1
$keywords_tv->setValue($childId, $empty);// 3
// need to save each time
$select_tv->save();
$keywords_tv->save();
}
// 4
$sql = 'UPDATE `z_modx_site_content`
SET `description` = \''. $empty .'\'
WHERE `id` >= \''. $newResource->get('id') .'\' ';
$results = $modx->db->query($sql);
}
else
$_SESSION['valid_duplicate'] = false;
break;
}
return;