- <?php
-
-
- /**
- * Class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @version $Revision: 268 $ $Date: 2006-06-11 17:25:11 +0200 (So, 11 Jun 2006) $
- * @package Lithron.LMediaManager
- * @subpackage Core
- */
-
- /**
- * Provides database access and administration functions
- *
- * @package Lithron.LMediaManager
- * @subpackage Core
- */
- Prado :: setPathOfAlias("LMediaManager", dirname(__FILE__));
- Prado :: using("LMediaManager.db.propel.*");
- Prado :: using("LMediaManager.db.propel.l_media_manager.*");
-
- class LMediaManagerCore extends TControl
- {
-
- /**
- * @return media files directory, defined in application.xml
- */
- function getMediaDir()
- {
- $param= $this->Application->getParameters();
- return $param->itemAt("MediaDir");
- }
-
- /**
- * scans media directory and updates database
- */
- public function updateDatabase($deleteMissing= false)
- {
- if ($cache= $this->Application->Cache)
- {
- $cache->delete("LMediaManager:sets");
- }
- $files= $this->scanDirectory($this->getMediaDir());
- $this->updateMediaFiles($files, $deleteMissing);
- }
-
- /**
- * returns records from database
- *
- * Either a Criteria object or a Integer id.
- * @param Criteria|integer
- */
- public function queryRecords($c= null)
- {
- if (is_numeric($c))
- {
- Prado :: trace("Querying database by id($c)", "Lithron.LMediaManager");
- return LMediaManagerFilesPeer :: retrieveByPK($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
- }
- if ($c === null)
- $c= new Criteria;
- Prado :: trace("Querying database by criteria", "Lithron.LMediaManager");
- $result= LMediaManagerFilesPeer :: doSelect($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
- return $result;
- }
-
- /**
- * scans media directory
- */
- private function scanDirectory($dir, $files= array ())
- {
- if (!is_dir($dir))
- {
- Prado :: log("Invalid media files directory", 4, "Lithron.LMediaManager");
- return;
- }
- $_scan= scandir($dir);
- foreach ($_scan AS $item)
- {
- $location= $dir . "/" . $item;
- if (substr($item, 0, 1) == ".")
- continue;
- if (is_dir($location))
- {
- $files= $this->scanDirectory($location, $files);
- }
- if (is_file($location))
- {
- // may need rework due to performance :( TODO
- $_data['name']= $item;
- $_data['directory']= $dir;
- $_data['md5']= md5_file($location);
- $_data['size']= filesize($location);
- #var_dump($_data);
- $files[]= $_data;
- }
- }
- #var_dump($files);
- return $files;
- }
- /**
- * compares files and updates database
- */
- private function updateMediaFiles($files, $deleteMissing= false)
- {
- $dbList= $this->queryRecords();
- $processedList= array ();
- $conn= $this->Application->getModule("database")->getConnection("l_media_manager", "propel");
- $magick= new LImage;
-
- if (is_array($files))
- // walk throgh files from filesystem
- foreach ($files AS $file)
- {
- $_found= false;
- if (is_array($dbList))
- // compare with database
- foreach ($dbList AS $key => $record)
- {
- if ($record->getMd5() == $file['md5'])
- {
- unset ($dbList[$key]);
- $_found= true;
- break;
- }
- }
- foreach ($processedList AS $key => $record)
- {
- # if ($record['directory'].$record['name'] == $file['directory'].$file['name'])
- # continue;
- if ($record['md5'] == $file['md5'])
- {
- Prado :: log("Duplicate media file '{$file['name']}' - FILE WAS NOT ADDED", 5, "Lithron.LMediaManager");
- continue 2;
- }
- }
-
-
- if ($_found != true)
- {
- Prado :: trace("New media file '{$file['name']}'", "Lithron.LMediaManager");
- // create new file if md5 not found
- $new= new LMediaManagerFiles;
- $_data= $magick->doIdentify($file['directory'] . "/" . $file['name']);
- if (is_array($_data))
- foreach ($_data AS $key => $d)
- {
- $file[$key]= $d;
- // TODO ?
- @ $new->setType($file['type']);
- $new->setSize($file['size']);
- @ $new->setSizeShort($file['size_short']);
- @ $new->setWidth($file['width']);
- @ $new->setHeight($file['height']);
- @ $new->setColorspace($file['colorspace']);
- @ $new->setTransparent($file['transparent']);
- @ $new->setResolutionX($file['resolution_x']);
- @ $new->setResolutionY($file['resolution_y']);
- @ $new->setColors($file['colors']);
- @ $new->setImageDepth($file['image_depth']);
- }
- $new->setDescription($file['name']);
- }
- else
- {
- Prado :: trace("Existing media file '{$file['name']}' found", "Lithron.LMediaManager");
- // open existing file and update info
- $c= new Criteria;
- $c->add(LMediaManagerFilesPeer :: MD5, $file['md5']);
- $result= LMediaManagerFilesPeer :: doSelect($c, $conn);
- $new= $result[0];
- }
-
- $new->setFileName($file['name']);
- $new->setMd5($file['md5']);
- $new->setDirectory($file['directory']);
- $new->setStatus(true);
- $new->save();
-
- $processedList[]= $file;
- }
-
- if (is_array($dbList))
- // set missing files in db to status false
- foreach ($dbList AS $file)
- {
- Prado :: trace("Non-existing file in database '{$file->getFileName()}'", "Lithron.LMediaManager");
- $c= new Criteria;
- $c->add(LMediaManagerFilesPeer :: MD5, $file->getMd5());
- $result= LMediaManagerFilesPeer :: doSelect($c, $conn);
- foreach ($result AS $record)
- {
- if ($deleteMissing)
- {
- Prado :: trace("Deleting media file ", "Lithron.LMediaManager");
- $record->delete();
- }
- else
- {
- $record->setStatus(false);
- $record->save();
- }
- }
- }
-
- }
-
- }
- ?>