We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52422
    • 4 Posts
    I am looking for a plugin or some snippet that allows me to unpublish/trash/delete document resources automatically after for example 20 days after the resource creation date.

    Its for EVO. Any possibilities?
      • 9995
      • 1,613 Posts
      Maybe you can use "end publishing date" on the resources?
        Evolution user, I like the back-end speed and simplicity smiley
        • 52422
        • 4 Posts
        That will solve the un-publish requirement I have.

        But how to fully trash/delete those resources from my back-end automatically also and in such way that I do not see a whole bunch of red-crossed out resource titles?
          • 4041
          • 788 Posts
          Here is a plugin that should do what you want. Installation instructions are at the top.

          <?php
          /* Auto Doc Purge plugin
             6/29/2016   2:00 pm est
             @breezer  - [email protected]
             open source - dwywwi (do what you want with it) :)
             
             * Searches in specified folders and deletes documents and TV content
               which are older than the amount of $adp['days'] set in the script
             * Runs once at login each new manager session
             * Prints details on a new tab on the manager welcome screen on login
             * Adds an entry to the system events with the details
             
             Installation:
             Create a new plugin with this code.
             Modify the variables in the setup section to your liking.
             For safety's sake the two delete querys for the documents and the TVs 
             are toggled by the $adp['test_mode'] setting.
             Make sure to check the following events on the System Events tab:
             * OnBeforeManagerLogin
             * OnManagerWelcomeRender
             
             ** note that if you delete a document with children,
                they will become orphaned and can only be seen via db access
          */
          
          //========= SETUP=========//
          // to test and make sure you are getting the correct documents
          // set this to true and the delete query is disabled
          // set to false to run the delete queries
          // true  -  false
          $adp['test_mode'] =true;
          
          // how many days till document purge
          $adp['days'] =20;
          
          // set specific parent(s) from which to delete documents
          // otherwise it defaults to checking the whole site
          // comma separated string of id's eg:  '1,5,22'
          $adp['parent'] ='1,5';
          
          // set which field to compare
          // createdon  -  publishedon
          $adp['db_field'] ='createdon';
          
          // tab title and header message
          $adp['lang']['tab_title'] ='<span style="color:orange;"><strong>Purged items</strong></span>';
          $adp['lang']['tab_header'] ='This is a list of purged items for this session.';
          
          //========= END SETUP=========//
          $e = &$modx->Event;
          
          $plugin_output ='';
          
          switch ( $e->name ) {
          
              case 'OnBeforeManagerLogin':
              
                  if( $_SESSION['apd_process'] =='' ){
                  
                      $purge_day = $adp['days'] * 86400;
                      $purge_time =$_SERVER['REQUEST_TIME'] + $modx->config['server_offset_time'];
                      $purge_time = $purge_time - $purge_day;
                      
                      // set up the where part of the query
                      $where =$adp['db_field'].' <='. $purge_time . ( $adp['parent'] !='' ? ' AND parent IN ('.$adp['parent'].')' : '' );
          
                      // check content type to make sure we are only getting html documents
                      $where .=' AND contentType="text/html"';
                      
                      $apd['purge_list'] = $modx->db->makeArray(
                                                  $modx->db->select(
                                                          "id,createdon,pagetitle",
                                                          $modx->getFullTableName( 'site_content' ),
                                                          $where,"","") );
                      $count = count( $apd['purge_list'] );
          
                      if( $count > 0 ){
          
                          $apd['purge_items'] ='';
                          $apd['purge_count'] ='';
          
                          // start the printout of the details
                          $apd['purge_items'].='Items purged: '.$count .'<br>';
          
                          for( $x=0; $x < $count; $x++ ){
          
                              $apd['purge_items'].='id: '. $apd['purge_list'][$x]['id'] .' --
                              '. $apd['purge_list'][$x]['pagetitle'] .' --
                              <em>'.strftime( "%a %B %d, %Y, %H:%M:%S", $apd['purge_list'][$x]['createdon'] ) .'</em><br>';
                              
          //----------------- // query to delete the document
                              if( !$adp['test_mode'] ){
          
                                  $delete = $modx->db->delete( $modx->getFullTableName( 'site_content' ), 'id = '.$apd['purge_list'][$x]['id'] );
          
                                  if( $delete ){
                                  
                                      $apd['purge_items'].='<em>delete success</em><br>';
          
                                  }
          
                              }
          
                              // check for and remove any TV content for this document
                              $apd['purge_tvs'] = $modx->db->makeArray(
                                                          $modx->db->select(
                                                          "id",
                                                          $modx->getFullTableName( 'site_tmplvar_contentvalues' ),
                                                          'contentid='.$apd['purge_list'][$x]['id'], "", "" ) );
          
                              $count_tvs = count( $apd['purge_tvs'] );
          
                              if( $count_tvs> 0 ){
          
                                  $apd['purge_items'].='TV items: '.$count_tvs .' <br>';
          
                                  for( $y=0; $y < $count; $y++ ){
                                  
          //--------------------------// query to delete any TVs for this document
                                      if( !$adp['test_mode'] ){
                                      
                                          $delete = $modx->db->delete( $modx->getFullTableName( 'site_tmplvar_contentvalues' ), 'id = '.$apd['purge_tvs'][$y]['id'] );
          
                                          if( $delete ){
          
                                              $apd['purge_items'].='<em>tv delete success</em><br>';
          
                                          }
                                      }
                                  }
                              }
                          }
                          
                          $_SESSION['apd_purge_list'] = $apd['purge_items'];
                          $_SESSION['apd_purge_count'] =$count;
          
                      }
                  }
                  
              break;
              
              case 'OnManagerWelcomeRender':
          
              if( $_SESSION['apd_process'] =='' ){
          
                  $manager_tab_tpl ='
          <!-- Auto Purge Documents -->
          <div class="tab-page" id="[+tab_id+]" style="padding-left:0; padding-right:0;">
          
              <!-- tab title -->
              <h2 class="tab">[+tab_title+] - ([+purge_count+])</h2>
          
              <script type="text/javascript">tpPane.addTabPage( document.getElementById( "[+tab_id+]" ) );</script>
          
              <!-- header title -->
              <div class="sectionHeader">
                  [+tab_header+]
              </div>
          
              <!-- tab content -->
              <div class="sectionBody">
          
                  [+tab_content+]
          
              </div>
          </div>';
          
          
                  // log this event - Reports / System Events
                  $modx->logEvent( 0, 1, $_SESSION['apd_purge_list'] , 'Auto Doc Purge' );
          
                  // create a new tab on the manager welcome screen showing the purge details
                 $replace = array(
                                  '[+tab_id+]'           => 'tabAPD',
                                  '[+tab_title+]'        => $adp['lang']['tab_title'],
                                  '[+tab_header+]'       => $adp['lang']['tab_header'],
                                  '[+tab_content+]'      => $_SESSION['apd_purge_list'],
                                  '[+purge_count+]'      => $_SESSION['apd_purge_count'] //$count
                                  );
          
                  $plugin_output .=str_replace( array_keys( $replace ), array_values( $replace ), $manager_tab_tpl );
          
                  // set this so that this plugin only runs once per manager session
                  $_SESSION['apd_process'] ='1';
                  $_SESSION['apd_purge_list'] ='';
                  $_SESSION['apd_purge_count'] ='';
              }
                  
              break;
          
              default: '';
          }
          
          $e->output( $plugin_output );
          return;
          ?>
            xforum
            http://frsbuilders.net (under construction) forum for evolution