<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Storage_Plugin_Core
{
  public function onItemDeleteBefore($event)
  {
    $item = $event->getPayload();

    if( $item->getType() !== 'storage_file' ) {
      $table = Engine_Api::_()->getItemTable('storage_file');
      $select = $table->select()
        ->where('parent_type = ?', $item->getType())
        ->where('parent_id = ?', $item->getIdentity());
        
      foreach( $table->fetchAll($select) as $file ) {
        try {
          Engine_Api::_()->storage()->deleteExternalsFiles($file->file_id);
          $file->delete();
        } catch( Exception $e ) {
          if( !($e instanceof Engine_Exception) ) {
            $log = Zend_Registry::get('Zend_Log');
            $log->log($e->__toString(), Zend_Log::WARN);
          }
        }
      }
      
      //Delete tinymce files
      $table->deleteTinyMceFiles($item);
    }
  }
}
