Phundament-Packages
[ class tree: Phundament-Packages ] [ index: Phundament-Packages ] [ all elements ]

Source for file LMediaManagerCore.php

Documentation is available at LMediaManagerCore.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Class file.
  6.  *
  7.  * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8.  * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9.  * @author $Author: schmunk $
  10.  * @version $Revision: 601 $  $Date: 2007-02-24 23:49:30 +0100 (Sa, 24 Feb 2007) $
  11.  * @package Phundament.Packages
  12.  * @subpackage LMediaManager
  13.  */
  14.  
  15. /**
  16.  * Provides database access and administration functions
  17.  *
  18.  * @package System.Util
  19.  * @subpackage Cores
  20.  */
  21. Prado :: setPathOfAlias("LMediaManager"dirname(__FILE__));
  22. Prado :: using("LMediaManager.db.propel.*");
  23. Prado :: using("LMediaManager.db.propel.l_media_manager.*");
  24.  
  25. class LMediaManagerCore extends TControl
  26. {
  27.  
  28.     /**
  29.      * @return media files directory, defined in application.xml
  30.      */
  31.     function getMediaDir()
  32.     {
  33.         $param$this->Application->getParameters();
  34.         $return$param->itemAt("MediaDir");
  35.         if (!$this->User->isInRole('root'))
  36.             $return .= "/web/" $this->User->Name;
  37.         return $return;
  38.     }
  39.  
  40.     /**
  41.      * scans media directory and updates database
  42.      */
  43.     public function updateDatabase($directory''$deleteMissingfalse$rightsPanelnull)
  44.     {
  45.         // TODO
  46.         if ($directory == '' && $this->User->isInRole('root'))
  47.             $directory$this->MediaDir "/web/root";
  48.         elseif ($directory == ''$directory$this->MediaDir;
  49.         if ($cache$this->Application->Cache)
  50.         {
  51.             $cache->delete("LMediaManager:sets");
  52.         }
  53.         $files$this->scanDirectory($directory);
  54.         $this->updateMediaFiles($directory$files$deleteMissing$rightsPanel);
  55.     }
  56.  
  57.     /**
  58.      * returns records from database
  59.      *
  60.      * Either a Criteria object or a Integer id.
  61.      * @param Criteria|integer
  62.      */
  63.     public function queryRecords($cnull)
  64.     {
  65.         static $results;
  66.  
  67.         #Prado :: trace("QueryRecords() ...", "Lithron.LMediaManager");
  68.         // caching
  69.         if ($c instanceof Criteria)
  70.         {
  71.             $_id md5(serialize($c));
  72.         }
  73.         else
  74.             $_id $c;
  75.         if (isset($results[$_id])) return $results[$_id];
  76.  
  77.         // querying
  78.         if (is_numeric($c))
  79.         {
  80.             Prado :: trace("Querying database by id($_id)""Lithron.LMediaManager");
  81.             $results[$_id]$resultLMediaManagerFilesPeer :: retrieveByPK($c$this->Application->getModule("database")->getConnection("l_media_manager""propel"));
  82.             return $result;
  83.         }
  84.         elseif (is_string($c))
  85.         {
  86.             $string$c "%";
  87.             $cnew Criteria;
  88.             $c->add(LMediaManagerFilesPeer :: DIRECTORY$stringCriteria :: LIKE);
  89.         }
  90.         if (!$c instanceof Criteria)
  91.         {
  92.             $cnew Criteria;
  93.         }
  94.  
  95.         Prado :: trace("Querying database by criteria ($_id)""Lithron.LMediaManager");
  96.         $results[$_id]$resultLMediaManagerFilesPeer :: doSelect($c$this->Application->getModule("database")->getConnection("l_media_manager""propel"));
  97.         #Prado :: trace("Queried.", "Lithron.LMediaManager");
  98.         return $result;
  99.     }
  100.  
  101.     /**
  102.      * scans media directory
  103.      */
  104.     private function scanDirectory($dir$filesarray ())
  105.     {
  106.         if (!is_dir($dir))
  107.         {
  108.             Prado :: log("Invalid media files directory"4"Lithron.LMediaManager");
  109.             return;
  110.         }
  111.         $_scanscandir($dir);
  112.         foreach ($_scan AS $item)
  113.         {
  114.             $location$dir "/" $item;
  115.             if (substr($item01== ".")
  116.                 continue;
  117.             if (is_dir($location))
  118.             {
  119.                 $files$this->scanDirectory($location$files);
  120.             }
  121.             if (is_file($location))
  122.             {
  123.                 // may need rework due to performance :( TODO
  124.                 $_data['name']$item;
  125.                 $_data['directory']$dir;
  126.                 $_data['md5']md5_file($location);
  127.                 $_data['size']filesize($location);
  128.                 #var_dump($_data);
  129.                 $files[]$_data;
  130.             }
  131.         }
  132.         #var_dump($files);
  133.         $this->Application->getModule("user_message")->add($dir " scanned"1);
  134.         return $files;
  135.     }
  136.     /**
  137.     * compares files and updates database
  138.     */
  139.     private function updateMediaFiles($directory$files$deleteMissingfalse$permissionsLPermissions :: DEFAULTS)
  140.     {
  141.         $completeRS$this->queryRecords();
  142.         $subsetRS$this->queryRecords($directory);
  143.  
  144.         $filesProcessedarray ();
  145.         $conn$this->Application->getModule("database")->getConnection("l_media_manager""propel");
  146.         $magicknew LImage;
  147.  
  148.         $this->Application->getModule("user_message")->add("Found " count($completeRS" files in database"1);
  149.         $this->Application->getModule("user_message")->add("Found " count($files" files in filesystem"1);
  150.  
  151.         if (is_array($files))
  152.  
  153.             // walk throgh files from filesystem
  154.             foreach ($files AS $file)
  155.             {
  156.                 $_foundfalse;
  157.                 // compare with database
  158.                 if (is_array($completeRS))
  159.                 {
  160.                     #Prado :: trace("Comparing " . $file['name'] . " with files from database.", "Lithron.LMediaManager");
  161.                     #var_dump($completeRS);
  162.                     foreach ($completeRS AS $key => $record)
  163.                     {
  164.                         // compare md5
  165.                         if ($record->getMd5(== $file['md5'])
  166.                         {
  167.                             unset ($completeRS[$key]);
  168.                             Prado :: trace("Found " $file['name'" in database.""Lithron.LMediaManager");
  169.                             $_foundtrue;
  170.                             break;
  171.                         }
  172.                     }
  173.                 }
  174.                 foreach ($filesProcessed AS $key => $record)
  175.                 {
  176.                     #    if ($record['directory'].$record['name'] == $file['directory'].$file['name'])
  177.                     #        continue;
  178.                     if ($record['md5'== $file['md5'])
  179.                     {
  180.                         Prado :: log("Duplicate media file '{$file['name']}' - FILE WAS NOT ADDED"8"Lithron.LMediaManager");
  181.                         continue 2;
  182.                     }
  183.                 }
  184.  
  185.                 $filesProcessed[]$file;
  186.  
  187.                 // set columns
  188.                 if ($_found == false)
  189.                 {
  190.                     $this->Application->getModule("user_message")->add("New " $file['name'" scanned"1);
  191.                     Prado :: trace("New media file '{$file['name']}'""Lithron.LMediaManager");
  192.                     // create new file if md5 not found
  193.                     $newnew LMediaManagerFiles;
  194.                     $_data$magick->doIdentify($file['directory'"/" $file['name']);
  195.                     if (is_array($_data))
  196.                         foreach ($_data AS $key => $d)
  197.                         {
  198.                             $file[$key]$d;
  199.                             // TODO ?
  200.                             $new->setType($file['type']);
  201.                             $new->setSize($file['size']);
  202.                             $new->setSizeShort($file['size_short']);
  203.                             $new->setWidth($file['width']);
  204.                             $new->setHeight($file['height']);
  205.                             $new->setColorspace($file['colorspace']);
  206.                             $new->setTransparent($file['transparent']);
  207.                             $new->setResolutionX($file['resolution_x']);
  208.                             $new->setResolutionY($file['resolution_y']);
  209.                             $new->setColors($file['colors']);
  210.                             $new->setImageDepth($file['image_depth']);
  211.                         }
  212.                     $new->setDescription($file['name']);
  213.                 }
  214.                 else
  215.                 {
  216.                     Prado :: trace("Existing media file '{$file['name']}found""Lithron.LMediaManager");
  217.                     // open existing file and update info
  218.                     $cnew Criteria;
  219.                     $c->add(LMediaManagerFilesPeer :: MD5$file['md5']);
  220.                     LRightsManager :: restrictCriteria($c"LMediaManagerFilesPeer");
  221.                     $resultLMediaManagerFilesPeer :: doSelect($c$conn);
  222.                     if (isset ($result[0]))
  223.                         $new$result[0];
  224.                     else
  225.                     {
  226.                         // do not apply changes when file is from another user
  227.                         $um $this->Application->getModule("user_message");
  228.                         if ($um !== null$um->add("File {$file['name']} not added to databasesame file exists with md5 ({$file['md5']})!"1);
  229.                         Prado :: trace("No changes applied to existing media file '{$file['name']}'""Lithron.LMediaManager");
  230.                         continue;
  231.  
  232.                     }
  233.                 }
  234.  
  235.                 LRightsManager :: setRights($new$permissions);
  236.  
  237.                 $new->setFileName($file['name']);
  238.                 $new->setMd5($file['md5']);
  239.                 $new->setDirectory($file['directory']);
  240.                 $new->setStatus(true);
  241.                 $new->save();
  242.  
  243.             }
  244.  
  245.         if (is_array($completeRS))
  246.             // set missing files in db to status false
  247.             foreach ($completeRS AS $file)
  248.             {
  249.                 // compare complete with subset
  250.                 $missingIdfalse;
  251.                 foreach ($subsetRS AS $sub)
  252.                 {
  253.                     if ($sub->getId(== $file->getId())
  254.                     {
  255.                         $missingId$sub->getId();
  256.                         break;
  257.                     }
  258.                 }
  259.  
  260.                 Prado :: trace("Non-existing file in database '{$file->getFileName()}'""Lithron.LMediaManager");
  261.                 $cnew Criteria;
  262.                 $c->add(LMediaManagerFilesPeer :: ID$missingId);
  263.                 $resultLMediaManagerFilesPeer :: doSelect($c$conn);
  264.                 foreach ($result AS $record)
  265.                 {
  266.                     if ($deleteMissing)
  267.                     {
  268.                         Prado :: trace("Deleting media file ""Lithron.LMediaManager");
  269.                         $record->delete();
  270.                     }
  271.                     else
  272.                     {
  273.                         $record->setStatus(false);
  274.                         $record->save();
  275.                     }
  276.                 }
  277.             }
  278.  
  279.     }
  280.  
  281. }
  282. ?>

Documentation generated on Sun, 25 Feb 2007 16:10:57 +0100 by phpDocumentor 1.3.1