<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Core_Model_DbTable_Files extends Engine_Db_Table {

  protected $_rowClass = 'Core_Model_File';
  
  public function getFileNameExist($file_name) {
    return $this->select()->from($this->info('name'), 'file_id')->where('name =?', $file_name)->query()->fetchColumn();
  }
  
  public function getPaginator($params = array()) {
    return Zend_Paginator::factory($this->getFiles($params));
  }
  
  public function getFiles($params = array()) {

    $select = $this->select()->order('file_id DESC');
    
    if(!empty($params['extension']) && isset($params['extension']))
      $select->where('extension IN (?)', $params['extension']);
    
    if(!empty($params['name']) && isset($params['name']))
      $select->where("name LIKE ?", '%'.$params['name'].'%');

    if(!empty($params['fetchAll']))
      return $this->fetchAll($select);

    return $select;
  }
  
  public function getFilesAssoc() {
  
    $filesArray = array('' => '');
		$files = $this->getFiles(array('fetchAll' => 1, 'extension' => array('gif', 'jpg', 'jpeg', 'png', 'webp', 'svg')));
		foreach($files as $file ) {
			$filesArray[$file->storage_path] = $file->name;
		}
    return $filesArray;
  } 
}
