We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36533
    • 65 Posts
    I'm having trouble allowing web users to delete a resource they have created through a spin-off of eForm2db.

    Just for testing purposes, I've got my basic snippets and chunks created for eForm to parse the input.

    My snippet "DeleteBlogPost":
    <?php
    function DeleteBlogPost( &$fields )
        {
            global $modx;
    		
    	$table = $modx->getFullTableName("site_content");
            $id = $modx->db->escape($fields[id]);
            if (is_int($id)) {
    		$rows_affected = $modx->db->delete($table, "id = $id"); 
            }
            return true;
        }
    return $html;
    ?>
    


    My Chunk "eFormBlogDeleteTpl":
    <form method="post" action="[~[*id*]~]" id="blogPostDelete" name="blogPostDelete">
    <input name="formid" type="hidden" value="blogPostDelete" />
            <label>ID:</label>
            <input name="id" class="form" size="2" type="text" eform="::0::" />
            <input type="submit" name="submit" value="Delete" /> 
    </form>
    


    My Chunk "eFormConfirmDelete":
    <p>This resource has been deleted.</p>


    My Snippet Call:
    [!DeleteBlogPost?!] 
    [!eForm? &noemail=`true` &formid=`blogPostDelete` &eFormOnBeforeMailSent=`DeleteBlogPost` &tpl=`eFormBlogDeleteTpl` &thankyou=`eFormConfirmDelete`!]
    


    When inputting a specific ID and submitting, I get a confirmation from the Thank You chunk, but the resource is not deleted.

    My goal is to output a list of blogs a specific web user has created and give them the ability to update/delete their specific posts. Another concern here is redeclaring the function multiple times with ditto and working around that?

    I'm not sure if this is the best approach. Any tips here?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Use an AJAX grid.

      Here's a useful one http://www.trirand.com/blog/



      [ed. note: sottwell last edited this post 14 years, 6 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 36533
        • 65 Posts
        That is definitely my end goal.

        I do need to get a better feel for the basic three (add, edit, delete) from the front-end, so I can get there. I can't figure out why my above example isn't working. From other examples I've seen, it seems to be pretty straight-forward. Yet, the difference might be that those were using custom tables through eForm2db, whereas this is accessing the main site_content table. Is there any authentication limits for update/delete by web users I'm missing?
          • 16278
          • 928 Posts
          You should be able to simply delete from the site_content table. I use ur001's Document class (attached) to handle creation, update and deletion in PubKit. The relevant lines of MySQL code are:
          	function Delete(){
          		if($this->isNew) return;
          		global $modx;
          		$id=$this->fields['id'];
          		$modx->db->delete($modx->getFullTableName('site_content'),"id=$id");
          		$modx->db->delete($modx->getFullTableName('site_tmplvar_contentvalues'),"contentid=$id");
          		$this->isNew=true;
          	}
          


          NB this also deletes associated TV values. Have you checked that your data is actually making it to the database query? Also, you need to clear the cache after deleting, e.g.
          function emptyCache() {
          	global $modx;
          	include_once $modx->config['base_path']."manager/processors/cache_sync.class.processor.php";
          	$sync = new synccache();
          	$sync->setCachepath("assets/cache/");
          	$sync->setReport(false);
          	$sync->emptyCache();
          }


          :) KP
            • 36533
            • 65 Posts
            Thanks KP.

            I tried this and I get an error:

            Using $this when not in object context 


            I tried wrapping this in a class and I get a new error:

            Call to undefined function DeleteBlogPost() 


            I'm missing something, but not sure what.
              • 36533
              • 65 Posts
              Hmm, any other ideas here? I'm still hung up on this one.