Example #1
0
 /**
  * Default model definition.
  */
 function define_default()
 {
     // Implement soft delete (exclude trash model).
     if (!$this->trash) {
         $this->bind('DELETE', function ($event, $model) {
             // Put in trash?
             if ($event['id']) {
                 if ($record = $model->get($event['id'])) {
                     $trashed = put("/trash" . $record->uri(), $record);
                 }
             }
             // Success?
             return $trashed ? true : false;
         });
         $this->bind('PUT', function ($event, $model) {
             $data =& $event['data'];
             $collection = underscore($model->name);
             // Restore from trash?
             if ($data[':restore'] && $collection && $event['id']) {
                 return put("/trash/{$collection}/{$event['id']}", array(':restore' => true));
             }
             // Finalize uploaded images.
             if ($data['images']) {
                 foreach ((array) $data['images'] as $name => $image) {
                     if (!$name || !$image) {
                         continue;
                     }
                     // Put uploaded image in proper location.
                     // Only works if file was uploaded with this $name and $image path.
                     $result = put("/{$collection}/{$event['id']}/images/{$name}", $image);
                     // Confirm or deny result.
                     if ($result) {
                         // Save result to collection.
                         $result['name'] = $name;
                         $data['images'][$name] = $result;
                     } else {
                         // Problem uploading.
                         unset($data['images'][$name]);
                     }
                 }
                 // Update/merge images field?
                 if (!empty($data['images'])) {
                     $record = $model->get($event['id']);
                     $data['images'] = merge($record['images'], $data['images']);
                 }
             }
         });
         $this->bind('after:GET', function ($result, $event, $model) {
             // Not found, check trash?
             if (!$result && $event['id'] && !isset($event['query']['notrash'])) {
                 if ($collection = underscore($model->name)) {
                     if ($result = get("/trash/{$collection}/{$event['id']}", $event['data'])) {
                         // Convert to model record.
                         return $model->get_model_record($result->values());
                     }
                 }
             }
         });
     }
     // Upload something (temp location).
     $this->bind('PUT.upload', function ($event, $model) {
         // This will upload a file to the config.app.public_path/uploads location (temp).
         if ($upload_file = $model->upload_file($event['id'], $event['upload_id'], $event['data'])) {
             return new ModelResource(array('file' => $upload_file), $upload_file);
         }
         return false;
     });
     // Put uploaded image in proper location, related to model, returns file name.
     $this->bind('PUT.images', function ($event, $model) {
         // This will move a previously uploaded image to the image location.
         if ($image_file = $model->upload_image($event['id'], $event['image_id'], $event['data'])) {
             return new ModelResource(array('src' => $image_file), $image_file);
         }
         return false;
     });
     // Put uploaded file content in document.
     $this->bind('PUT.file', function ($event, $model) {
         // This will get the contents of a previously uploaded file and delete it.
         if ($file_content = $model->get_upload_file_content($event['id'], $event['file_id'])) {
             // Save file content in doc field "file_name".
             return array("file_{$event['file_id']}" => $file_content);
         }
         return false;
     });
     // Delete an image.
     $this->bind('DELETE.image', function ($event, $model) {
         $model->delete_image($event['id'], $event['image_id']);
         return false;
     });
     return parent::define_default();
 }