Source for file LMediaManagerCore.php
Documentation is available at LMediaManagerCore.php
* @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
* @copyright 2005, diemeisterei GmbH. All rights reserved.
* @author $Author: schmunk $
* @version $Revision: 601 $ $Date: 2007-02-24 23:49:30 +0100 (Sa, 24 Feb 2007) $
* @package Phundament.Packages
* @subpackage LMediaManager
* Provides database access and administration functions
Prado :: setPathOfAlias("LMediaManager", dirname(__FILE__ ));
Prado :: using("LMediaManager.db.propel.*");
Prado :: using("LMediaManager.db.propel.l_media_manager.*");
* @return media files directory, defined in application.xml
$param= $this->Application->getParameters();
$return= $param->itemAt("MediaDir");
if (!$this->User->isInRole('root'))
$return .= "/web/" . $this->User->Name;
* scans media directory and updates database
public function updateDatabase($directory= '', $deleteMissing= false, $rightsPanel= null)
if ($directory == '' && $this->User->isInRole('root'))
$directory= $this->MediaDir . "/web/root";
elseif ($directory == '') $directory= $this->MediaDir;
if ($cache= $this->Application->Cache)
$cache->delete("LMediaManager:sets");
$files= $this->scanDirectory($directory);
$this->updateMediaFiles($directory, $files, $deleteMissing, $rightsPanel);
* returns records from database
* Either a Criteria object or a Integer id.
* @param Criteria|integer
#Prado :: trace("QueryRecords() ...", "Lithron.LMediaManager");
if ($c instanceof Criteria)
if (isset ($results[$_id])) return $results[$_id];
Prado :: trace("Querying database by id($_id)", "Lithron.LMediaManager");
$results[$_id]= $result= LMediaManagerFilesPeer :: retrieveByPK($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
$c->add(LMediaManagerFilesPeer :: DIRECTORY, $string, Criteria :: LIKE);
if (!$c instanceof Criteria)
Prado :: trace("Querying database by criteria ($_id)", "Lithron.LMediaManager");
$results[$_id]= $result= LMediaManagerFilesPeer :: doSelect($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
#Prado :: trace("Queried.", "Lithron.LMediaManager");
private function scanDirectory($dir, $files= array ())
Prado :: log("Invalid media files directory", 4, "Lithron.LMediaManager");
foreach ($_scan AS $item)
$location= $dir . "/" . $item;
if (substr($item, 0, 1) == ".")
$files= $this->scanDirectory($location, $files);
// may need rework due to performance :( TODO
$_data['directory']= $dir;
$this->Application->getModule("user_message")->add($dir . " scanned", 1);
* compares files and updates database
private function updateMediaFiles($directory, $files, $deleteMissing= false, $permissions= LPermissions :: DEFAULTS)
$filesProcessed= array ();
$conn= $this->Application->getModule("database")->getConnection("l_media_manager", "propel");
$this->Application->getModule("user_message")->add("Found " . count($completeRS) . " files in database", 1);
$this->Application->getModule("user_message")->add("Found " . count($files) . " files in filesystem", 1);
// walk throgh files from filesystem
foreach ($files AS $file)
#Prado :: trace("Comparing " . $file['name'] . " with files from database.", "Lithron.LMediaManager");
foreach ($completeRS AS $key => $record)
if ($record->getMd5() == $file['md5'])
unset ($completeRS[$key]);
Prado :: trace("Found " . $file['name'] . " in database.", "Lithron.LMediaManager");
foreach ($filesProcessed AS $key => $record)
# if ($record['directory'].$record['name'] == $file['directory'].$file['name'])
if ($record['md5'] == $file['md5'])
Prado :: log("Duplicate media file '{$file['name']}' - FILE WAS NOT ADDED", 8, "Lithron.LMediaManager");
$filesProcessed[]= $file;
$this->Application->getModule("user_message")->add("New " . $file['name'] . " scanned", 1);
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']);
foreach ($_data AS $key => $d)
@ $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']);
Prado :: trace("Existing media file '{$file['name']}' found", "Lithron.LMediaManager");
// open existing file and update info
$c->add(LMediaManagerFilesPeer :: MD5, $file['md5']);
LRightsManager :: restrictCriteria($c, "LMediaManagerFilesPeer");
$result= LMediaManagerFilesPeer :: doSelect($c, $conn);
// do not apply changes when file is from another user
$um = $this->Application->getModule("user_message");
if ($um !== null) $um->add("File {$file['name']} not added to database, same file exists with md5 ({$file['md5']})!", 1);
Prado :: trace("No changes applied to existing media file '{$file['name']}'", "Lithron.LMediaManager");
LRightsManager :: setRights($new, $permissions);
$new->setFileName($file['name']);
$new->setMd5($file['md5']);
$new->setDirectory($file['directory']);
// set missing files in db to status false
foreach ($completeRS AS $file)
// compare complete with subset
foreach ($subsetRS AS $sub)
if ($sub->getId() == $file->getId())
$missingId= $sub->getId();
Prado :: trace("Non-existing file in database '{$file->getFileName()}' ", "Lithron.LMediaManager");
$c->add(LMediaManagerFilesPeer :: ID, $missingId);
$result= LMediaManagerFilesPeer :: doSelect($c, $conn);
foreach ($result AS $record)
Prado :: trace("Deleting media file ", "Lithron.LMediaManager");
$record->setStatus(false);
|