diff --git a/.gitmodules b/.gitmodules index f1f340c..c07aa44 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "Uploadcare/uploadcare-php"] - path = Uploadcare/uploadcare-php +[submodule "Lib/uploadcare-php"] + path = Lib/uploadcare-php url = git://github.com/uploadcare/uploadcare-php.git diff --git a/Config/uploadcare.php.default b/Config/uploadcare.php.default new file mode 100644 index 0000000..71c46c3 --- /dev/null +++ b/Config/uploadcare.php.default @@ -0,0 +1,19 @@ + array( + 'public_key' => 'demopublickey', + 'secret_key' => 'demoprivatekey', + ), +); diff --git a/Lib/UploadcareUtil.php b/Lib/UploadcareUtil.php new file mode 100644 index 0000000..3fe9f7b --- /dev/null +++ b/Lib/UploadcareUtil.php @@ -0,0 +1,52 @@ += 0) { + require_once $UploadcarePhpPath . DS . '5.3-5.4' . DS . 'Uploadcare.php'; + UploadcareUtil::$api = new Uploadcare\Api($public_key, $secret_key); + } else { + require_once $UploadcarePhpPath . DS . '5.2' . DS . 'Uploadcare.php'; + UploadcareUtil::$api = new Uploadcare_Api($public_key, $secret_key); + } + return UploadcareUtil::$api; + } +} diff --git a/Uploadcare/uploadcare-php b/Lib/uploadcare-php similarity index 100% rename from Uploadcare/uploadcare-php rename to Lib/uploadcare-php diff --git a/Model/Behavior/UploadcareBehavior.php b/Model/Behavior/UploadcareBehavior.php new file mode 100644 index 0000000..3b0547e --- /dev/null +++ b/Model/Behavior/UploadcareBehavior.php @@ -0,0 +1,66 @@ +fields = $fields; + return true; + } + + /** + * Get API instance + * only runs once, so if it already is setup, it doesn't waste cycles + * + * @return Uploadcare_Api + */ + public function api() { + if (!empty($this->api)) { + return $this->api; + } + App::uses('UploadcareUtil', 'Uploadcare.Lib'); + $this->api = UploadcareUtil::api(); + return $this->api; + } + + /** + * After save callback for model + * + * @param Model $model + * @param boolean $created True if this save created a new record + * @return boolean + */ + public function afterSave(Model $model, $created = null) { + foreach ($this->fields as $field) { + if (isset($model->data[$model->alias][$field])) { + $file_id = $model->data[$model->alias][$field]; + $file = $this->api()->getFile($file_id); + $file->store(); + } + } + return true; + } +} diff --git a/README.md b/README.md index 72c4c98..856ed86 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,33 @@ It's based on a [uploadcare-php][4] library. - PHP 5.2+ - php-curl -## Install +## Install Clone the repo: - git clone git://github.com/uploadcare/uploadcare-cakephp.git plugins/Uploadcare --recursive + git clone git://github.com/uploadcare/uploadcare-cakephp.git app/Plugins/Uploadcare --recursive -Unzip file to you CakePHP's plugin folder. +Or as a submodule: -Inside your app/Config/bootstrap.php add: + git submodule add git://github.com/uploadcare/uploadcare-cakephp.git app/Plugin/Uploadcare --recursive + git submodule init --recursive + git submodule update --recursive + +Inside your app/Config/bootstrap.php load this Plugin, with: CakePlugin::load('Uploadcare'); - + Inside your app/Config/core.php add: - Configure::write('uploadcare', array( - 'public_key' => 'demopublickey', - 'private_key' => 'demoprivatekey' - )); - + cd app + cp Plugin/Uploadcare/Config/uploadcare.php.default Config/uploadcare.php + vim Config/uploadcare.php + +Then enter you values for: + + 'public_key' => 'demopublickey', + 'private_key' => 'demoprivatekey' + Change "demopublickey" and "demoprivatekey" to your own public and secret keys. ## Usage example @@ -37,20 +45,19 @@ Plugin has a behavior and a helper. Create some model and attach a behavior: - class File extends AppModel - { + class File extends AppModel { public $actsAs = array( 'Uploadcare.Uploadcare' => array('file_id') ); } - -Behaviour takes an array of fields to be treated as file_id. You can pass as many fields as you can. + +UploadcareBehavior takes an array of fields to be treated as `file_id`. You can pass as many fields as you can. Create some controller that will handle the file the form. Add a helper Uploadcare.Uploadcare to it. class FilesController extends AppController { public $helpers = array('Html', 'Form', 'Uploadcare.Uploadcare'); - + public function add() { if ($this->request->is('post')) { $this->File->create(); @@ -60,7 +67,7 @@ Create some controller that will handle the file the form. Add a helper Uploadca } else { $this->Session->setFlash('Unable to add your post.'); } - } + } } } @@ -70,7 +77,7 @@ Create a View to display form with Uploadcare widget: Form->create('File', array('action' => 'add')); ?> Form->input('File.file_id', array('type' => 'hidden', 'role' => 'uploadcare-uploader')); ?> Form->end('Save!'); ?> - + That's everything to be ready to upload. When controller will recieve a POST data and try to create new object, the Uploadcare.Uploadcare behavior will handle all the fields you provided and store files for you. @@ -82,26 +89,26 @@ Create a controller. File model has a field "file_id": class FilesController extends AppController { public $helpers = array('Html', 'Form', 'Uploadcare.Uploadcare'); - + public function index() { $this->set('files', $this->File->find('all')); } } - + Inside view: Uploadcare->file($file['File']['file_id'])->scaleCrop(400, 400)->getImgTag(); ?> - + "Uploadcare" helper provides 2 basic methods. By calling $this->Uploadcare->api() - + you will get an instance of Uploadcare_Api. - + $this->Uploadcare->file($file_id) - + you will get an object of Uploadcare_File class. You can find more about this classes at [uploadcare-php][4] main repo. diff --git a/Uploadcare/Model/Behavior/UploadcareBehavior.php b/Uploadcare/Model/Behavior/UploadcareBehavior.php deleted file mode 100644 index d1a51a2..0000000 --- a/Uploadcare/Model/Behavior/UploadcareBehavior.php +++ /dev/null @@ -1,41 +0,0 @@ -api = new Uploadcare_Api($public_key, $secret_key); - $this->fields = $fields; - } - - public function afterSave(Model $model, $created = array()) - { - foreach ($this->fields as $field) { - if (isset($model->data[$model->alias][$field])) { - $file_id = $model->data[$model->alias][$field]; - $file = $this->api->getFile($file_id); - $file->store(); - } - } - } -} \ No newline at end of file diff --git a/Uploadcare/View/Helper/UploadcareHelper.php b/Uploadcare/View/Helper/UploadcareHelper.php deleted file mode 100644 index 6318b05..0000000 --- a/Uploadcare/View/Helper/UploadcareHelper.php +++ /dev/null @@ -1,45 +0,0 @@ -api = new Uploadcare_Api($public_key, $secret_key); - parent::__construct($View, $settings); - } - - /** - * Get API instance - * - * @return Uploadcare_Api - **/ - public function api() - { - return $this->api; - } - - /** - * Get File - * - * @param $file_id File ID - * @return Uploadcare_File - **/ - public function file($file_id) - { - return $this->api->getFile($file_id); - } -} \ No newline at end of file diff --git a/View/Helper/UploadcareHelper.php b/View/Helper/UploadcareHelper.php new file mode 100644 index 0000000..86c78bf --- /dev/null +++ b/View/Helper/UploadcareHelper.php @@ -0,0 +1,39 @@ +api)) { + return $this->api; + } + App::uses('UploadcareUtil', 'Uploadcare.Lib'); + $this->api = UploadcareUtil::api(); + return $this->api; + } + + /** + * Get File + * + * @param $file_id File ID + * @return Uploadcare_File + */ + public function file($file_id) { + return $this->api()->getFile($file_id); + } +}