コード例 #1
0
 public function create()
 {
     // TODO: Use Minerva's access system (li3_access)
     // Bigger todo: update li3_acess (Nate's changes) and redo Minerva's access system completely.
     $user = Auth::check('minerva_user');
     if ($user['role'] != 'administrator' && $user['role'] != 'content_editor') {
         $this->redirect('/');
         return;
     }
     if (!empty($this->request->data['Filedata'])) {
         //Logger::debug(json_encode($this->request->data));
         // IMPORTANT: Use MongoDate() when inside an array/object because $_schema isn't deep
         $now = new MongoDate();
         $data = array();
         // IMPORTANT: The current/target gallery id must be passed in order to associate the item.
         // Otherwise, it'd be stored loose in the system.
         $gallery_id = $this->request->data['gallery_id'];
         // If there was only one file uploaded, stick it into a multi-dimensional array.
         // It's just easier to always run the foreach() and code the processing stuff once and here.
         // For now...while we're saving to disk.
         if (!isset($this->request->data['Filedata'][0]['error'])) {
             $this->request->data['Filedata'] = array($this->request->data['Filedata']);
         }
         foreach ($this->request->data['Filedata'] as $file) {
             // TODO: change. possibly adaptable class
             $source_base = '/minerva_gallery/img/gallery_items/';
             $service = 'file';
             // Create an Item object
             $id = new MongoId();
             $document = Item::create(array('_id' => $id, 'created' => $now, 'modified' => $now, 'service' => $service, 'source' => $source_base . $id . '.jpg', 'title' => $file['name'], '_galleries' => array($gallery_id), 'published' => true));
             // Save file  to disk
             // TODO: Again, change this...maybe use an adaptable class for storage
             if ($file['error'] == UPLOAD_ERR_OK) {
                 $upload_dir = LITHIUM_APP_PATH . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'minerva_gallery' . DIRECTORY_SEPARATOR . 'webroot' . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'gallery_items' . DIRECTORY_SEPARATOR;
                 $ext = substr(strrchr($file['name'], '.'), 1);
                 switch (strtolower($ext)) {
                     case 'jpg':
                     case 'jpeg':
                     case 'png':
                     case 'gif':
                     case 'png':
                     case 'doc':
                     case 'txt':
                         if (move_uploaded_file($file['tmp_name'], $upload_dir . $id . '.jpg')) {
                             Logger::debug('saved file to disk successfully.');
                         }
                         break;
                     default:
                         //exit();
                         break;
                 }
             }
             //Logger::debug('document _id: ' . (string)$document->_id);
             if ($document->save($data)) {
                 Logger::debug('saved mongo document successfully.');
             }
         }
     }
     //$this->set(compact('document'));
 }