To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Show
Ignore:
Timestamp:
09/03/10 18:01:27 (21 months ago)
Author:
tboutell
Message:

Fixed uploads in which one or more files are not acceptable.

Moved all validation to the annotation form. The initial upload form now validates only that you uploaded at least one thing. After that you're off to the land of annotation. This eliminates a lot of duplicate code that is hard to maintain

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/apostrophePlugin/trunk/lib/action/BaseaMediaActions.class.php

    r2134 r2136  
    608608    // Belongs at the beginning, not the end 
    609609    $this->forward404Unless(aMediaTools::userHasUploadPrivilege()); 
     610     
     611    // This has been simplified. We no longer do real validation in the first pass, 
     612    // we just make sure there is at least one file. Then the validation of the annotation 
     613    // pass can take over to minimize duplicate code 
    610614    $this->form = new aMediaUploadMultipleForm(); 
     615    $this->mustUploadSomething = false; 
    611616    if ($request->isMethod('post')) 
    612617    { 
    613       $this->form->bind( 
    614         $request->getParameter('a_media_items'), 
    615         $request->getFiles('a_media_items')); 
    616       if ($this->form->isValid()) 
    617       { 
    618         $request->setParameter('first_pass', true); 
    619         // Saving embedded forms is weird. We can get the form objects 
    620         // via getEmbeddedForms(), but those objects were never really 
    621         // bound, so getValue will fail on them. We have to look at the 
    622         // values array of the parent form instead. The widgets and 
    623         // validators of the embedded forms are rolled into it. 
    624         // See: 
    625         // http://thatsquality.com/articles/can-the-symfony-forms-framework-be-domesticated-a-simple-todo-list 
    626         $items = $request->getParameter("a_media_items"); 
    627         for ($i = 0; ($i < aMediaTools::getOption('batch_max')); $i++) 
    628         { 
    629           $values = $this->form->getValues(); 
    630           $file = $values["item-$i"]['file']; 
    631           if ($file) 
    632           { 
    633             // Humanize the original filename 
    634             $title = $file->getOriginalName(); 
    635             $title = preg_replace('/\.\w+$/', '', $title); 
    636             $title = aTools::slugify($title, false, false, ' '); 
    637             $items["item-$i"]['title'] = $title; 
    638           } 
    639           else 
    640           { 
    641             // So the editImagesForm validator won't complain about these 
    642             unset($items["item-$i"]); 
    643           } 
    644         } 
    645         $request->setParameter("a_media_items", $items); 
     618      $count = 0; 
     619      $request->setParameter('first_pass', true); 
     620      // Saving embedded forms is weird. We can get the form objects 
     621      // via getEmbeddedForms(), but those objects were never really 
     622      // bound, so getValue will fail on them. We have to look at the 
     623      // values array of the parent form instead. The widgets and 
     624      // validators of the embedded forms are rolled into it. 
     625      // See: 
     626      // http://thatsquality.com/articles/can-the-symfony-forms-framework-be-domesticated-a-simple-todo-list 
     627      $items = $request->getParameter("a_media_items"); 
     628      $files = $request->getFiles("a_media_items"); 
     629      for ($i = 0; ($i < aMediaTools::getOption('batch_max')); $i++) 
     630      { 
     631        $values = $this->form->getValues(); 
     632        // This is how we check for the presence of a file upload without a full form validation 
     633        $file = $files["item-$i"]['file']; 
     634        if (isset($file['newfile']['tmp_name']) && strlen($file['newfile']['tmp_name'])) 
     635        { 
     636          // Humanize the original filename 
     637          $title = $file['newfile']['name']; 
     638          $title = preg_replace('/\.\w+$/', '', $title); 
     639          $title = aTools::slugify($title, false, false, ' '); 
     640          $items["item-$i"]['title'] = $title; 
     641          $count++; 
     642        } 
     643        else 
     644        { 
     645          // So the editImagesForm validator won't complain about these 
     646          unset($items["item-$i"]); 
     647        } 
     648      } 
     649      $request->setParameter("a_media_items", $items); 
     650      if ($count) 
     651      { 
    646652        // We're not doing stupid iframe tricks anymore, so we can just forward 
    647653        $this->forward('aMedia', 'editMultiple'); 
    648         //  
    649         // // We'd like to just do this... 
    650         // // $this->forward('aMedia', 'edit'); 
    651         // // But we need to break out of the iframe, and  
    652         // // modern browsers ignore Window-target: _top which 
    653         // // would otherwise be perfect for this. 
    654         // // Fortunately, the persistent file upload widget can tolerate 
    655         // // a GET-method redirect very nicely as long as we pass the 
    656         // // persistids. So we make the current parameters available 
    657         // // to a template that breaks out of the iframe via 
    658         // // JavaScript and passes the prameters on. 
    659         // $this->parameters = $request->getParameterHolder('a_media_items')->getAll(); 
    660         // // If I don't do this I just get redirected back to myself 
    661         // unset($this->parameters['module']); 
    662         // unset($this->parameters['action']); 
    663         // return 'Redirect'; 
     654      } 
     655      else 
     656      { 
     657        $this->mustUploadSomething = true; 
    664658      } 
    665659    }