We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26931
    • 2,314 Posts
      • 10165
      • 129 Posts
      I’ve had a go, but can only get this to work in a snippet, and by adding an id manually to test - but at least it works:
      <?php
      //Get the id of the deleted documents
      $docID = "115";
      
      //Set the associated template variable ids that need to be deleted
      $tvID1 = "26";
      $tvID2 = "27";
      
          //Get the filepath of the first tv (tvID1)
          $result = $modx->db->query( 'SELECT id, value FROM `modx_site_tmplvar_contentvalues` WHERE contentid = \'' .$modx->db->escape($docID). '\' AND tmplvarid = \'' .$modx->db->escape($tvID1). '\' ORDER BY id ASC' );
          while( $row = $modx->db->getRow( $result ) ) {
      	$output = $row['value'];
      
      //check that the file is not open already and then delete it
      $fh = fopen($output, 'w') or die("can't open file");
      fclose($fh);
      unlink($output);
      }
      echo "this file has been deleted: ".$output;
      ?>
      


      then I tried doing it with a plugin set to OnDocFormDelete (also tried onEmptyTrash) - but can’t seem to get it to work - probably got this completely wrong so please feel free to give me a slap!..

      //Set the associated template variable ids that need to be deleted
      $tvID1 = "26";
      $deletedids = $modx->event->params['ids'];
      $countid = count($deletedids);
      $i = 0;
      while ($i <= $countid-1):
      
          //Get the filepath of the tv1 (tvID1)
          $result = $modx->db->query( 'SELECT id, value FROM `modx_site_tmplvar_contentvalues` WHERE contentid = \'' .$modx->db->escape($deletedids). '\' AND tmplvarid = \'' .$modx->db->escape($tvID1). '\' ORDER BY id ASC' );
          while( $row = $modx->db->getRow( $result ) ) {
      	$output = $row['value'];
          }
      //check that the file is not open already and then delete it
      $fh = fopen($output, 'w') or die("can't open file");
      fclose($fh);
      unlink($output);
      }
      
      $i++;
      endwhile;
      return;
      


      thanks all..
        • 10165
        • 129 Posts
        thanks for the pointer Sharkbait
        I’ve tried and tried to work my way through the maxigallery directory delete and adapt it, but the php is a bit beyond me so can’t get it to work on just files.
          • 10165
          • 129 Posts
          I’m still trying to get this to work, getting a php fatal error Allowed memory size exceeded - I guess my php is probably bad and causing this - i.e. nothing to do with needing more memory allocating.

          Really hoping that someone will be able to point me in the right direction as I’ve spent ages trying to get to grips with this learning along the way...

          
          if(!function_exists('delete_files')) {
            function delete_files($filename) {
            unlink($filename);
            return true;
            }
          }
          $modx->invokeEvent("OnEmptyTrash",
              array(
          	"ids"=>$ids
          ));
          global $modx;
          $deletedids = $modx->event->params['ids'];
          $countid = count($deletedids);
          $i = 0;
          while ($i <= $countid-1):
          
          $table = $modx->getFullTableName("site_tmplvar_contentvalues");
          $rows_affected = $modx->db->delete($table, "contentid = $deletedids[$i] AND tmplvarid = 26");
          
          $dir = $modx->$deletedids[$i];
          delete_files($dir);
          $i++;
          endwhile;
          return;
          
          
          

            • 3749
            • 24,544 Posts
            I think you’re misunderstanding invokeEvent(). The name is somewhat confusing. It’s something MODx calls at certain points to provide hooks so you can write custom code that will be executed at that point.

            Callling invokeEvent() does nothing unless some plugin has been attached to the named event. If you call it in a plugin that’s hooked to the named event, it will execute that plugin’s code over and over until memory runs out. Every time it’s called, MODx looks for a plugin that’s attached to that event, and executes it, which invokes the event again ... ad infinitum.

            Your code looks like it might work if you take out the invokeEvent() code, assuming that the code is in a plugin that is attached to the OnEmptyTrash event. It will execute when you manually empty the trash in the Manager. When that happens, *MODX* executes InvokeEvent(’OnEmptyTrash’) and your code will execute. $ids will be set by MODx to an array of the ids of documents it just deleted.

            Remove this:

            $modx->invokeEvent("OnEmptyTrash",
                array(
            	"ids"=>$ids
            ));


              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 10165
              • 129 Posts
              thanks BobRay, that sorted the memory problem. It still won’t delete the associated file though so must be getting something wrong..
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Try putting in some echo statements into your plugin, followed by exit(), to see what exactly it thinks it’s doing. For example, have it echo the path to the file to be removed before actually running the unlink() function. It may be illuminating to see what it is actually generating for a path and filename.
                  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
                  • 3749
                  • 24,544 Posts
                  Quote from: freshwaterwest at Mar 23, 2010, 10:31 AM

                  I’ve had a go, but can only get this to work in a snippet, and by adding an id manually to test - but at least it works:
                  then I tried doing it with a plugin set to OnDocFormDelete (also tried onEmptyTrash) - but can’t seem to get it to work - probably got this completely wrong so please feel free to give me a slap!..

                  //Set the associated template variable ids that need to be deleted
                  $tvID1 = "26";
                  $deletedids = $modx->event->params['ids'];
                  
                  foreach ($deleteids as $key=>$value) {
                  
                      //Get the filepath of the tv1 (tvID1)
                      $result = $modx->db->query( 'SELECT id, value FROM `modx_site_tmplvar_contentvalues` WHERE contentid = \'' .$modx->db->escape($value). '\' AND tmplvarid = \'' .$modx->db->escape($tvID1). '\' ORDER BY id ASC' );
                      while( $row = $modx->db->getRow( $result ) ) {
                  	$output = $row['value'];
                      }
                  //check that the file is not open already and then delete it
                  $fh = fopen($output, 'w') or die("can't open file");
                  fclose($fh);
                  unlink($output);
                  }
                  return;
                  


                  I think I can see one thing that’s wrong, in the snippet version, you’re passing an individual directory name, in the plugin version, you’re passing the whole id array.

                  I can see you’re coming from a background other than PHP. I think the PHP foreach loop would save you a lot of trouble.

                  foreach($deletedids as $key=>$value) {

                  // $value will hold the current id number being processed
                  }

                  I’ve done a quick rewrite of your plugin code above, fixing the error I mentioned and using foreach(). It should work if the snippet did.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 10165
                    • 129 Posts
                    Thanks for your suggestions - I’ve tried both approaches and tried adding some echo and exit - I’m using the manager delete resource and purge bin but it silently just goes through and deletes with no echo showing, not sure why - tried echoing in a couple of places..?
                    I keep going through the motions and checking to see if a the associated file is deleted but unfortunately not as yet..

                    just looked at the php log and noticed it says: Invalid argument supplied for foreach()
                      • 10165
                      • 129 Posts
                      just noticed
                      foreach ($deleteids as $key=>$value) {

                      (changed to $deletedids)

                      now I’m getting "can’t open file"