Author: ornicar2
Date: 2010-02-02 21:20:15 +0100 (Tue, 02 Feb 2010)
New Revision: 27442

Added:
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/DmAdminMediaForm.php
   plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaMoveTest.php
   plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmWidgetRenderTagTest.php
Modified:
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/BasedmMediaLibraryActions.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMediaFolder.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmMediaLibraryTest.php
   plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaFolderTest.php
Log:
[Diem]
- admin media library now allows to move existing medias around folders
- added unit and functional tests for moving medias
- refactored admin media library actions

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/BasedmMediaLibraryActions.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/BasedmMediaLibraryActions.class.php
       2010-02-02 19:25:57 UTC (rev 27441)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/BasedmMediaLibraryActions.class.php
       2010-02-02 20:20:15 UTC (rev 27442)
@@ -17,7 +17,7 @@
       $this->getUser()->logAlert($this->getI18n()->__('This file is not 
writable'), false);
     }
 
-    $this->form = new DmMediaForm($this->file);
+    $this->form = new DmAdminMediaForm($this->file);
   }
 
   public function executeIndex(sfWebRequest $request)
@@ -75,53 +75,45 @@
     return $links;
   }
 
-  public function executeNewFile(sfWebRequest $request)
+  public function executeSaveFile(dmWebRequest $request)
   {
-    $this->forward404Unless(
-      $parent = 
dmDb::table('DmMediaFolder')->find($request->getParameter('folder_id')),
-      sprintf('There is no parent %d', $request->getParameter('folder_id'))
-    );
-
-    if  (!$parent->isWritable())
+    // modify existing media
+    if ($mediaId = dmArray::get($request->getParameter('dm_admin_media_form'), 
'id'))
     {
-      $this->getUser()->logAlert(
-        $this->getI18n()->__('Folder %1% is not writable', array('%1%' => 
$parent->getFullPath()))
-      );
-
-      return $this->renderPartial('dmInterface/flash');
-    }
-
-    $form = new DmMediaForm();
-    $form->setDefault('dm_media_folder_id', $parent->getId());
-
-    return $this->renderText($form->render('.dm_form.list.little 
action=dmMediaLibrary/saveFile'));
-  }
-
-  public function executeSaveFile(sfWebRequest $request)
-  {
-    if ($mediaId = dmArray::get($request->getParameter('dm_media_form'), 'id'))
-    {
       $this->forward404Unless($media = dmDb::table('DmMedia')->find($mediaId));
+      $form = new DmAdminMediaForm($media);
     }
+    // create new media
     else
     {
       $media = null;
-    }
 
-    $form = new DmMediaForm($media);
+      $this->forward404Unless($folder = 
dmDb::table('DmMediaFolder')->find($request->getParameter('folder_id')));
 
-    if ($form->bindAndValid($request))
+      if(!$folder->isWritable())
+      {
+        $this->getUser()->logAlert($this->getI18n()->__('Folder %1% is not 
writable', array('%1%' => $folder->fullPath)));
+      }
+      
+      $form = new DmAdminMediaForm();
+      $form->setDefault('dm_media_folder_id', $folder->id);
+    }
+    
+    if ($request->isMethod('post') && $form->bindAndValid($request))
     {
-      $object = $form->save();
+      $redirect = $form->getValue('file') || $media->dm_media_folder_id != 
$form->getValue('dm_media_folder_id');
 
-      if($form->getValue('file'))
+      $media = $form->save();
+
+      if($redirect)
       {
-        $this->getUser()->setFlash('dm_media_open', $object->id, false);
-        return 
$this->renderText($this->getRouting()->getMediaUrl($object->Folder));
+        $this->getUser()->setFlash('dm_media_open', $media->id, false);
+        return 
$this->renderText($this->getRouting()->getMediaUrl(dmDb::table('DmMediaFolder')->find($media->dm_media_folder_id)));
       }
     }
 
-    return $this->renderText($form->render('.dm_form.list.little 
action=dmMediaLibrary/saveFile'));
+    $action = $media ? 'dmMediaLibrary/saveFile' : 
'dmMediaLibrary/saveFile?folder_id='.$folder->id;
+    return $this->renderText($form->render('.dm_form.list.little 
action="'.$action.'"'));
   }
 
   public function executeDeleteFile(sfWebRequest $request)

Added: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/DmAdminMediaForm.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/DmAdminMediaForm.php
                              (rev 0)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMediaLibrary/lib/DmAdminMediaForm.php
      2010-02-02 20:20:15 UTC (rev 27442)
@@ -0,0 +1,42 @@
+<?php
+
+class DmAdminMediaForm extends DmMediaForm
+{
+  public function configure()
+  {
+    parent::configure();
+
+    if($this->object->exists())
+    {
+      $folderChoices = $this->getFolderChoices();
+
+      unset($this['dm_media_folder_id']);
+      
+      $this->widgetSchema['dm_media_folder_id'] = new sfWidgetFormChoice(array(
+        'choices' => $folderChoices
+      ));
+      $this->validatorSchema['dm_media_folder_id'] = new 
sfValidatorChoice(array(
+        'choices' => array_keys($folderChoices),
+        'required' => true
+      ));
+      $this->widgetSchema->setLabel('dm_media_folder_id', 'Move');
+    }
+  }
+
+  protected function getFolderChoices()
+  {
+    $_folderChoices = dmDb::query('DmMediaFolder f')
+    ->orderBy('f.lft')
+    ->select('f.id, f.level, f.rel_path')
+    ->fetchPDO();
+
+    $folderChoices = array();
+    foreach($_folderChoices as $values)
+    {
+      $name = basename($values[2]) ? basename($values[2]) : 'root';
+      $folderChoices[$values[0]] = str_repeat('&nbsp;&nbsp;', 
$values[1]).'-&nbsp;'.$name;
+    }
+
+    return $folderChoices;
+  }
+}
\ No newline at end of file

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
 2010-02-02 19:25:57 UTC (rev 27441)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
 2010-02-02 20:20:15 UTC (rev 27442)
@@ -39,6 +39,12 @@
     }
     
     unset($values['file']);
+
+    if($this->object->exists() && $values['dm_media_folder_id'] != 
$this->object->dm_media_folder_id)
+    {
+      $moveToFolderId = $values['dm_media_folder_id'];
+      $values['dm_media_folder_id'] = $this->object->dm_media_folder_id;
+    }
     
     parent::doUpdateObject($values);
 
@@ -59,6 +65,11 @@
         }
       }
     }
+
+    if(isset($moveToFolderId))
+    {
+      $this->object->move(dmDb::table('DmMediaFolder')->find($moveToFolderId));
+    }
   }
 
   public function clearName($validator, $values)

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
    2010-02-02 19:25:57 UTC (rev 27441)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
    2010-02-02 20:20:15 UTC (rev 27442)
@@ -227,7 +227,42 @@
     return $toMedia;
   }
 
+  public function move(DmMediaFolder $folder)
+  {
+    if($folder->id == $this->dm_media_folder_id)
+    {
+      return $this;
+    }
 
+    if(!$this->isWritable())
+    {
+      throw new dmException(sprintf('The file %s is not writable.', 
dmProject::unRootify($this->fullPath)));
+    }
+
+    if(!$folder->isWritable())
+    {
+      throw new dmException(sprintf('The folder %s is not writable.', 
dmProject::unRootify($folder->fullPath)));
+    }
+
+    if($folder->hasSubFolder($this->file))
+    {
+      throw new dmException(sprintf('The selected folder already contains a 
folder named "%s".', $this->name));
+    }
+
+    if($folder->hasFile($this->file))
+    {
+      throw new dmException(sprintf('The selected folder already contains a 
file named "%s".', $this->name));
+    }
+
+    rename($this->fullPath, $folder->fullPath.'/'.$this->file);
+
+    $this->dm_media_folder_id = $folder->id;
+    $this->Folder = $folder;
+    $this->save();
+
+    return $this;
+  }
+
   public function refreshFromFile()
   {
     $this->fromArray(array(

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMediaFolder.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMediaFolder.class.php
      2010-02-02 19:25:57 UTC (rev 27441)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMediaFolder.class.php
      2010-02-02 20:20:15 UTC (rev 27442)
@@ -122,6 +122,20 @@
     ->exists();
   }
 
+  /**
+   * Checks if a file already exists in this folder
+   *
+   * @param string $name A file name
+   * @return bool
+   */
+  public function hasFile($name)
+  {
+    return dmDb::query('DmMedia m')
+    ->where('m.dm_media_folder_id = ?', $this->id)
+    ->andWhere('m.file = ?', $name)
+    ->exists();
+  }
+
   /*
    * Shortcut to ->getNode()->isRoot()
    */
@@ -150,7 +164,7 @@
    */
   public function create()
   {
-    return 
$this->getServiceContainer()->getService('filesystem')->mkdir($this->getFullPath());
+    return $this->getService('filesystem')->mkdir($this->getFullPath());
   }
 
   /**
@@ -189,7 +203,7 @@
     $oldRelPath = $this->get('rel_path');
     $newRelPath = $this->getNode()->getParent()->get('rel_path').'/'.$name;
 
-    $fs = $this->getServiceContainer()->getService('filesystem');
+    $fs = $this->getService('filesystem');
 
     $oldFullPath = $this->getFullPath();
     $newFullPath = dmOs::join($this->getNode()->getParent()->getFullPath(), 
$name);
@@ -256,7 +270,7 @@
     $oldRelPath = $this->get('rel_path');
     $newRelPath = $folder->get('rel_path').'/'.$this->name;
 
-    $fs = $this->getServiceContainer()->getService('filesystem');
+    $fs = $this->getService('filesystem');
 
     $oldFullPath = $this->getFullPath();
     $newFullPath = dmOs::join($folder->getFullPath(), $this->name);
@@ -345,7 +359,7 @@
     // Remove dir itself
     if(!$this->getNode()->isRoot() && $this->dirExists())
     {
-      
$this->getServiceContainer()->getService('filesystem')->deleteDir($this->fullPath);
+      $this->getService('filesystem')->deleteDir($this->fullPath);
     }
 
     return parent::delete($conn);

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmMediaLibraryTest.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmMediaLibraryTest.php
     2010-02-02 19:25:57 UTC (rev 27441)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmMediaLibraryTest.php
     2010-02-02 20:20:15 UTC (rev 27442)
@@ -83,9 +83,10 @@
 ))
 ->has('h1', false)
 ->has('form', true)
+->has('error_list', false)
 
-->info('Change legend, author and icense')
-->click('input.submit', array('dm_media_form' => array(
+->info('Change legend, author and license')
+->click('input.submit', array('dm_admin_media_form' => array(
   'author' => 'new author',
   'legend' => 'new legend',
   'license' => 'new license',
@@ -98,7 +99,8 @@
   'method' => 'post'
 ))
 ->has('h1', false)
-->has('form', true);
+->has('form', true)
+->has('error_list', false);
 
 $media = dmDb::table('DmMedia')->findOneByRelPath('images/default.jpg');
 
@@ -107,6 +109,38 @@
 $b->test()->is($media->author, 'new author', 'media author is 
'.$media->author);
 $b->test()->is($media->license, 'new license', 'media license is 
'.$media->license);
 
+$deeperFolder = 
dmDb::table('DmMediaFolder')->findOneByRelPath('images/deeper');
+
+$b->info('Move the file')
+->click('input.submit', array('dm_admin_media_form' => array(
+  'author' => 'new author',
+  'legend' => 'new legend',
+  'license' => 'new license',
+  'dm_media_folder_id' => $deeperFolder->id,
+  'id' => 2
+)))
+->checks(array(
+  'code' => 200,
+  'moduleAction' => 'dmMediaLibrary/saveFile',
+  'method' => 'post'
+))
+->has('form', false)
+->testResponseContent('/index.php/tools/media/media/path/images/deeper');
+
+$deeperFolder->refresh(true);
+$deeperFolder->refreshRelated('Medias');
+
+$b->test()->is($media->relPath, 'images/deeper/default.jpg', 'media relPath is 
'.$media->relPath);
+
+$b->info('Follow ajax redirection')
+->get('/tools/media/media/path/images/deeper')
+->checks(array(
+  'code' => 200,
+  'moduleAction' => 'dmMediaLibrary/path',
+  'h1' => 'deeper'
+))
+->has('.dm_media_library ul.content li.file:first a span', 'default . jpg');
+
 $b->info('Add a folder')
 ->get('/tools/media/media')
 ->redirect()
@@ -293,9 +327,9 @@
 ->click('div.control a.new_file')
 ->checks(array(
   'code' => 200,
-  'moduleAction' => 'dmMediaLibrary/newFile'
+  'moduleAction' => 'dmMediaLibrary/saveFile'
 ))
-->has('form input#dm_media_form_file')
+->has('form input#dm_admin_media_form_file')
 
 ->info('Submit unchanged form')
 ->click('input.submit')

Modified: plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaFolderTest.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaFolderTest.php       
2010-02-02 19:25:57 UTC (rev 27441)
+++ plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaFolderTest.php       
2010-02-02 20:20:15 UTC (rev 27442)
@@ -4,7 +4,7 @@
 $helper = new dmMediaUnitTestHelper();
 $helper->boot();
 
-$t = new lime_test(120);
+$t = new lime_test(122);
 
 $table = dmDb::table('DmMediaFolder');
 $root = $table->checkRoot();
@@ -115,6 +115,8 @@
   $t->is($folder->getNodeParentId(), $folder->isRoot() ? null : 
$folder->Node->getParent()->id, $folder->name.' node parent id is 
'.$folder->getNodeParentId());
 }
 
+$t->ok(!$f2->hasFile('test.jpg'), 'f2 has no file test.jpg');
+
 $t->comment('Add a file in f2');
 copy(
   dmOs::join(sfConfig::get('dm_core_dir'), 'data/image/defaultMedia.jpg'),
@@ -127,6 +129,8 @@
 $t->ok($media->exists(), 'media exists');
 $t->is($media->Folder, $f2, 'media folder is f2');
 
+$t->ok($f2->hasFile('test.jpg'), 'f2 has file test.jpg');
+
 $t->is($f2->nbElements, 2, 'f2 has 2 element');
 
 $t->is($f2->getDmMediasByFileName(), array(

Added: plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaMoveTest.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaMoveTest.php         
                (rev 0)
+++ plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmMediaMoveTest.php 
2010-02-02 20:20:15 UTC (rev 27442)
@@ -0,0 +1,73 @@
+<?php
+
+require_once(dirname(__FILE__).'/helper/dmMediaUnitTestHelper.php');
+$helper = new dmMediaUnitTestHelper();
+$helper->boot();
+
+$t = new lime_test();
+
+$table = dmDb::table('DmMediaFolder');
+$root = $table->checkRoot();
+
+$t->diag('syncing root');
+$root->sync();
+
+$helper->checkTreeIntegrity($t);
+
+$helper->testFolderCorrelations($t);
+
+$grandParent = $table->createQuery('f')
+->select('f.*, RANDOM() as rand')
+->orderBy('rand')
+->fetchOne();
+
+$parent = $table->create(array(
+  'rel_path' => $grandParent->relPath.'/parent'
+));
+$parent->Node->insertAsFirstChildOf($grandParent);
+
+$t->is($parent->exists(), true, 'Folder parent created');
+$t->is((string)$parent->Node->getParent(), (string)$grandParent, 'Folder 
parent inserted in grand-parent');
+
+$f1 = $table->create(array(
+  'rel_path' => $parent->relPath.'/f1'
+));
+$f1->Node->insertAsFirstChildOf($parent);
+
+$t->is($f1->exists(), true, 'Folder f1 created');
+$t->is((string)$f1->Node->getParent(), (string)$parent, 'Folder f1 inserted in 
parent');
+
+$f2 = $table->create(array(
+  'rel_path' => $parent->relPath.'/f2'
+));
+$f2->Node->insertAsFirstChildOf($parent);
+
+$t->is($f2->exists(), true, 'Folder f2 created');
+$t->is((string)$f2->Node->getParent(), (string)$parent, 'Folder f2 inserted in 
parent');
+
+$fileName = basename(__FILE__);
+$filePath = dmOs::join($f1->fullPath, $fileName);
+copy(__FILE__, $filePath);
+
+$media = dmDb::table('DmMedia')->create(array(
+  'file' => $fileName,
+  'author' => 'Thibault D.',
+  'legend' => 'dmMedia test cases',
+  'dm_media_folder_id' => $f1->id
+))->saveGet();
+
+$t->ok($media->exists(), 'media has been saved');
+
+$t->is($media->fullPath, $f1->fullPath.'/'.$media->file, 'Media full path is 
'.$media->fullPath);
+
+$t->ok(file_exists($inF1Path = $f1->fullPath.'/'.$fileName), $inF1Path.' 
exists');
+$t->ok(!file_exists($inF2Path = $f2->fullPath.'/'.$fileName), $inF2Path.' does 
not exist');
+
+$t->comment('Test media->move');
+
+$media->move($f2);
+
+$t->is($media->fullPath, $f2->fullPath.'/'.$media->file, 'Media full path is 
'.$media->fullPath);
+
+$t->ok(!file_exists($inF1Path = $f1->fullPath.'/'.$fileName), $inF1Path.' does 
no more exist');
+$t->ok(file_exists($inF2Path = $f2->fullPath.'/'.$fileName), $inF2Path.' 
exists');
\ No newline at end of file

Added: plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmWidgetRenderTagTest.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmWidgetRenderTagTest.php   
                        (rev 0)
+++ plugins/diemPlugin/trunk/dmCorePlugin/test/unit/dmWidgetRenderTagTest.php   
2010-02-02 20:20:15 UTC (rev 27442)
@@ -0,0 +1,24 @@
+<?php
+
+require_once(dirname(__FILE__).'/helper/dmUnitTestHelper.php');
+$helper = new dmUnitTestHelper();
+$helper->boot();
+
+$t = new lime_test();
+
+class sfWidgetFormDmTest extends sfWidgetFormInputText
+{
+
+  public function render($name, $value = null, $attributes = array(), $errors 
= array())
+  {
+    $attributes['class'] = json_encode(array(
+      'key' => 'value'
+    ));
+
+    return parent::render($name, $value, $attributes, $errors);
+  }
+}
+
+$widget = new sfWidgetFormDmTest;
+
+//dmDebug::kill($widget->render('test'));
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to