energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file DirectoryObject.class.php

Documentation is available at DirectoryObject.class.php

  1. <?php
  2.  
  3. /**
  4.  * Содержит класс Uploads
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13. //require_once('core/framework/FileSystemObject.class.php');
  14. //require_once('core/framework/FileObject.class.php');
  15.  
  16. /**
  17.  * Класс предназначенный для обработки набора FileObject
  18.  *
  19.  * @package energine
  20.  * @subpackage core
  21.  * @author dr.Pavka
  22.  */
  23.  
  24. class DirectoryObject extends FileSystemObject {
  25.     /**
  26.      * Directory handle
  27.      *
  28.      * @var resource 
  29.      * @access private
  30.      */
  31.     private $dirHandle = null;
  32.  
  33.     /**
  34.      * Массив файлов
  35.      *
  36.      * @var array 
  37.      * @access private
  38.      */
  39.     private $files = array();
  40.  
  41.     /**
  42.      * Текущий ключ
  43.      *
  44.      * @var int 
  45.      * @access private
  46.      */
  47.     private $iterator = 0;
  48.  
  49.     /**
  50.      * Конструктор класса
  51.      *
  52.      * @return void 
  53.      */
  54.     public function __construct({
  55.         parent::__construct();
  56.     }
  57.  
  58.     /**
  59.      * Статический метод загрузки возвращающий self
  60.      *
  61.      * @param string путь к файлу
  62.      * @return DirectoryObject 
  63.      * @access public
  64.      * @static
  65.      */
  66.  
  67.     public static function loadFrom($path{
  68.         if (!file_exists($path|| !is_dir($path)) {
  69.             throw new SystemException('ERR_DEV_NO_FILE'SystemException::ERR_DEVELOPER$path);
  70.         }
  71.         if (!is_writeable($path)) {
  72.             throw new SystemException('ERR_DEV_DIR_NOT_WRITABLE'SystemException::ERR_DEVELOPER$path);
  73.         }
  74.         $result new DirectoryObject();
  75.         $result->loadData($path);
  76.         return $result;
  77.     }
  78.  
  79.     /**
  80.      * Деструктор закрывает открытый ресурс
  81.      *
  82.      * @return void 
  83.      * @access public
  84.      */
  85.  
  86.     public function __destruct({
  87.         if (!empty($this->dirHandle)) {
  88.             closedir($this->dirHandle);
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Открывает директорию
  94.      *
  95.      * @return void 
  96.      * @access public
  97.      */
  98.  
  99.     public function open({
  100.         if (!($this->dirHandle = opendir($this->getPath()))) {
  101.             throw new SystemException('ERR_DEV_UNABLE_OPEN_DIR'SystemException::ERR_DEVELOPER$this->path);
  102.         }
  103.         while (false !== ($fileName readdir($this->dirHandle))) {
  104.             if (substr($fileName01!= '.'{
  105.                 $fullPath $this->getPath().'/'.$fileName;
  106.  
  107.                 if (isset($_POST['imageonly']&& !in_array(self::getTypeInfo($fullPath)array(self::IS_IMAGEself::IS_FOLDER ))) {
  108.                     //dummy
  109.                 }
  110.                 else {
  111.                     $this->files[(is_dir($fullPath))?DirectoryObject::loadFrom($fullPath):FileObject::loadFrom($fullPath);
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * Создание папки
  119.      *
  120.      * @param array данные папки
  121.      * @return boolean 
  122.      * @access public
  123.      */
  124.  
  125.     public function create($data{
  126.         $result false;
  127.         if (!isset($data[self::TABLE_NAME])) {
  128.             throw new SystemException('ERR_DEV_INSUFFICIENT_DATA'SystemException::ERR_DEVELOPER);
  129.         }
  130.  
  131.         if(!isset($data[self::TABLE_NAME]['upl_path'])){
  132.             $data[self::TABLE_NAME]['upl_path'Translit::transliterate($data[self::TABLE_NAME]['upl_name']'_'true);
  133.         }
  134.         $data[self::TABLE_NAME]['upl_path'$data['path'].'/'.$data[self::TABLE_NAME]['upl_path'];
  135.         unset($data['path']);
  136.         $data $data[self::TABLE_NAME];
  137.  
  138.         if (!file_exists($data['upl_path'])) {
  139.             $result @mkdir($data['upl_path']);
  140.         }
  141.  
  142.         if ($result{
  143.             $this->dbh->modify(QAL::INSERTself::TABLE_NAME$data);
  144.         }
  145.         return $result;
  146.     }
  147.  
  148.     /**
  149.      * Удаление папки
  150.      *
  151.      * @return boolean 
  152.      * @access public
  153.      */
  154.  
  155.     public function delete({
  156.         if (@rmdir($this->getPath())) {
  157.             parent::delete();
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Переходит к следующему объекту
  163.      *
  164.      * @return type 
  165.      * @access public
  166.      */
  167.  
  168.     public function next({
  169.         $this->iterator++;
  170.     }
  171.  
  172.     /**
  173.      * Перемещается на первый єлемент
  174.      *
  175.      * @return void 
  176.      * @access public
  177.      */
  178.  
  179.     public function rewind({
  180.         $this->iterator = 0;
  181.     }
  182.  
  183.     /**
  184.      * Возвращает текущий объект
  185.      *
  186.      * @return mixed 
  187.      * @access public
  188.      */
  189.  
  190.     public function current({
  191.         return $this->files[$this->iterator];
  192.     }
  193.  
  194.     /**
  195.      * Возворащает ключ текущего объекта
  196.      *
  197.      * @return int 
  198.      * @access public
  199.      */
  200.  
  201.     public function key({
  202.         return $this->iterator;
  203.     }
  204.  
  205.     /**
  206.      * Проверяет существует ли текущий елемент
  207.      *
  208.      * @return boolean 
  209.      * @access public
  210.      */
  211.  
  212.     public function valid({
  213.         return !empty($this->dirHandle&& $this->iterator<sizeof($this->files);
  214.     }
  215.  
  216.     /**
  217.      * Возвращает объект в виде массива
  218.      * Если он не открыт, возвращается информация о самом объексте - иначе, о всех вложенных объектах
  219.      *
  220.      * @return array 
  221.      * @access public
  222.      */
  223.  
  224.     public function asArray({
  225.         $result array();
  226.         if (empty($this->dirHandle)) {
  227.             $result parent::asArray();
  228.         }
  229.         else {
  230.             foreach ($this->files as $file{
  231.                 $data $file->asArray();
  232.                 $result[$data;
  233.             }
  234.             usort($resultarray($this'sortFileNames'));
  235.         }
  236.         return $result;
  237.     }
  238.  
  239.     /**
  240.      * Сортировка содержимого папки по алфавиту
  241.      *
  242.      * @return int 
  243.      * @access private
  244.      */
  245.  
  246.     private function sortFileNames($current$next{
  247.         return strcasecmp($current['upl_name']$next['upl_name']);
  248.     }
  249. }
В создании документации нам помог: phpDocumentor