We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19119
    • 22 Posts
    Hi,

    I’ve made a plugin that check if there is not another document with the same pagetitle.

    Plugin code :
    //<?php
    $e = & $modx->Event;
    switch ($e->name) {
    	case "OnBeforeDocFormSave":
    			$id = intval($_POST['id']);
    			if(isset($_POST["pagetitle"]))
    			{
    				$title = $_POST["pagetitle"];
    				$rs = $modx->dbQuery("SELECT * FROM ".$modx->getFullTableName("site_content")." WHERE pagetitle like '$title' AND id != $id");
    				$count = $modx->recordCount($rs);
    				if($count != 0)
    				{
    					$mode = $_POST['mode'];
    					$url = "index.php?a=$mode" . ($mode == '27' ? "&id=" . $id : "");
    					$modx->manager->saveFormValues($mode);
    					$modx->webAlert("Un document avec un titre similaire ($title) existe deja.",$url);
    					die();
    				}
    			}
    		break;
    }
    //?>


    When I’m in mode 27 (editing a document) everything work fine, if there is another document with the same pagetitle, it displays me the message in a javascript alert, and redirect to the editing page with the values I’ve modified.

    The problem is when I’m in mode 4 (creating a new document), if I there is another document with the same pagetitle, it also displays me the message, but when I’m redirected on the previous page, all the values I’ve filled in are still there, except for the parent ID.

    I’ve looked into the file manager/actions/mutate_content.dynamic.php what was happening. First it checks if form values had been saved, and it loads everything in $_POST and then I found this code at line 606 :
    <?php
    if (isset ($_REQUEST['id'])) {
    	if ($content['parent'] == 0) {
    		$parentname = $site_name;
    	} else {
    		$sql = 'SELECT pagetitle FROM '.$tbl_site_content.' WHERE id=\''.$content['parent'].'\'';
    		$rs = mysql_query($sql);
    		$limit = mysql_num_rows($rs);
    		if ($limit != 1) {
    			$e->setError(8);
    			$e->dumpError();
    		}
    		$parentrs = mysql_fetch_assoc($rs);
    		$parentname = $parentrs['pagetitle'];
    	}
    } elseif (isset ($_REQUEST['pid'])) {
    	if ($_REQUEST['pid'] == 0) {
    		$parentname = $site_name;
    	} else {
    		$sql = 'SELECT pagetitle FROM '.$tbl_site_content.' WHERE id=\''.$_REQUEST['pid'].'\'';
    		$rs = mysql_query($sql);
    		$limit = mysql_num_rows($rs);
    		if ($limit != 1) {
    			$e->setError(8);
    			$e->dumpError();
    		}
    		$parentrs = mysql_fetch_assoc($rs);
    		$parentname = $parentrs['pagetitle'];
    	}
    } else {
    	$parentname = $site_name;
    	$content['parent'] = 0;
    }
    ?>


    I’ve looked what was in $_POST after creating and editing a document.
    Editing :
    Array
    (
        [a] => 5
        [id] => 10666
        [mode] => 27
        [MAX_FILE_SIZE] => 4194304
        [refresh_preview] => 0
        [newtemplate] => 
        [stay] => 
        [pagetitle] => LDR 2006/357
        [longtitle] => LDR 2006/358 : Test d'unicité
        [description] => Test d'unicité
        [alias] => 
        [link_attributes] => 
        [introtext] => 
        [template] => 5
        [menutitle] => 
        [menuindex] => 9
        [hidemenucheck] => on
        [hidemenu] => 0
        [parent] => 35
        [isfolder] => 0
        [richtextcheck] => on
        [richtext] => 1
        [donthitcheck] => on
        [donthit] => 0
        [publishedcheck] => on
        [published] => 1
        [pub_date] => 
        [unpub_date] => 
        [searchablecheck] => on
        [searchable] => 1
        [cacheablecheck] => on
        [cacheable] => 1
        [syncsitecheck] => on
        [syncsite] => 1
        [contentType] => text/html
        [content_dispo] => 0
        [type] => document
        [ta] => 
    
    Test d'unicité
    
        [which_editor] => TinyMCE
        [tvtype%5Fldr] => Normal
        [tvdocument%5F1] => 
        [tvdocument%5F2] => 
        [tvdocument%5F3] => 
        [tvdocument%5F4] => 
        [tvdocument%5F5] => 
        [tvdocument%5F6] => 
        [tvdocument%5F7] => 
        [tvdocument%5F8] => 
        [tvdocument%5F9] => 
        [tvdocument%5F10] => 
        [chkalldocs] => on
        [save] => Envoyer
    )
    


    Creating :
    Array
    (
        [a] => 5
        [id] => 
        [mode] => 4
        [MAX_FILE_SIZE] => 4194304
        [refresh_preview] => 0
        [newtemplate] => 
        [stay] => 
        [pagetitle] => LDR 2006/357
        [longtitle] => 
        [description] => 
        [alias] => 
        [link_attributes] => 
        [introtext] => 
        [template] => 5
        [menutitle] => 
        [menuindex] => 9
        [hidemenucheck] => on
        [hidemenu] => 0
        [parent] => 35
        [isfolder] => 0
        [richtextcheck] => on
        [richtext] => 1
        [donthitcheck] => on
        [donthit] => 0
        [publishedcheck] => on
        [published] => 1
        [pub_date] => 
        [unpub_date] => 
        [searchablecheck] => on
        [searchable] => 1
        [cacheablecheck] => on
        [cacheable] => 1
        [syncsitecheck] => on
        [syncsite] => 1
        [contentType] => text/html
        [content_dispo] => 0
        [type] => document
        [ta] => 
        [which_editor] => TinyMCE
        [tvtype%5Fldr] => Normal
        [tvdocument%5F1] => 
        [tvdocument%5F2] => 
        [tvdocument%5F3] => 
        [tvdocument%5F4] => 
        [tvdocument%5F5] => 
        [tvdocument%5F6] => 
        [tvdocument%5F7] => 
        [tvdocument%5F8] => 
        [tvdocument%5F9] => 
        [tvdocument%5F10] => 
        [chkalldocs] => on
        [save] => Envoyer
    )
    


    If we are in mode 27 (editing) the $_REQUEST[’id’] is set so there is no problem. But if we are in mode 4 (creating) the $_REQUEST[’id’] is not set, so it checks for $_REQUEST[’pid’] (I assume that pid = parent id) which doesn’t exist in any case. If I’m not wrong the parent id is ine $_REQUEST[’parent’].

    Is this a bug ? or am I doing something wrong ?

    Thanks for reading wink

    Hope you’ll understand my problem.
      • 19119
      • 22 Posts
      Oups, I forgot, I’m using MODx 0.9.6.3, on Windows server 2003, with WampServer (PHP : 5.2.9-1, MySQL : 5.1.32-community-log, Apache : 2.2.11)
        • 19119
        • 22 Posts
        Nobody can help me huh
          • 19119
          • 22 Posts
          Hi,

          I tried to replace every $_REQUEST[’pid’] by $_REQUEST[’parent’] in manager/actions/mutate_content.dynamic.php but it was not working, and when I tried to create a new document by a right click on a folder the parent was set to 0.

          I added this in my plugin code :
          <?php
          $pid = intval($_POST['parent']);
          ($mode == '4' ? "&pid=" . $pid  : "");
          ?>
          

          And it works great.

          Here is my plugin code :
          //<?php
          $e = & $modx->Event;
          switch ($e->name) {
          	case "OnBeforeDocFormSave":
          			$id = intval($_POST['id']);
          			$pid = intval($_POST['parent']);
          			if(isset($_POST["pagetitle"]))
          			{
          				$title = $_POST["pagetitle"];
          				$rs = $modx->dbQuery("SELECT * FROM ".$modx->getFullTableName("site_content")." WHERE pagetitle like '%$title%' AND id != $id");
          				$count = $modx->recordCount($rs);
          				if($count != 0)
          				{
          					$mode = $_POST['mode'];
          					$url = "index.php?a=$mode" . ($mode == '27' ? "&id=" . $id : "") . ($mode == '4' ? "&pid=" . $pid  : "");
          					$modx->manager->saveFormValues($mode);
          					$modx->webAlert("Un document avec un titre similaire ($title) existe deja.",$url);
          					die();
          				}
          			}
          		break;
          }
          //?>
          
            • 7231
            • 4,205 Posts
            nice, this can be useful...thanks grin
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don&#39;t know what it is.
              Do you, Mr. Jones? - [bob dylan]