Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions Config/uploadcare.php.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Configuration for the Uploadcare API files
*
* Usage:
* cd app
* cp Plugin/Uploadcare/Config/uploadcare.php.default Config/uploadcare.php
* vim Config/uploadcare.php
*
* put in your public_key and secret_key values
*
* NOTE: it will load this configuration file whenever you need it
*/
$config = array(
'Uploadcare' => array(
'public_key' => 'demopublickey',
'secret_key' => 'demoprivatekey',
),
);
52 changes: 52 additions & 0 deletions Lib/UploadcareUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Loader for the Uploadcare API files
*
* Usage:
* App::uses('UploadcareUtil', 'Uploadcare.Lib');
* Uploadcare::api();
*
*/
class UploadcareUtil {

/**
* Placeholder
*
* @var mixed object or null
*/
static $api = null;

/**
* Setup the API object/class and return it
*
* @return object Uploadcare_Api
*/
static function api() {
if (!empty(UploadcareUtil::$api)) {
return UploadcareUtil::$api;
}
// verify config
Configure::load('uploadcare');
$public_key = Configure::read('Uploadcare.public_key');
$secret_key = Configure::read('Uploadcare.secret_key');
if (empty($secret_key)) {
$secret_key = Configure::read('Uploadcare.private_key'); // alt naming, deprecated
}
if (empty($public_key) || empty($secret_key)) {
throw new OutOfBoundsException('UploadcareUtil::api() error: you must configure the API keys');
}
if ($public_key=='demopublickey' || $secret_key=='demoprivatekey') {
throw new OutOfBoundsException('UploadcareUtil::api() error: you have the "demo" keys specified');
}
// load in vendors and initialize api
$UploadcarePhpPath = dirname(__FILE__) . DS . 'uploadcare-php' . DS . 'uploadcare' . DS . 'lib';
if (strnatcmp(phpversion(),'5.3') >= 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;
}
}
66 changes: 66 additions & 0 deletions Model/Behavior/UploadcareBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Upload care allows you to add basic API usage on your Models
*/

class UploadcareBehavior extends ModelBehavior {

/**
* List of fields to act as file_id
*
* @var array
*/
private $fields = array();

/**
* Uploadcare API
*
* @var Uploadcare_Api
*/
private $api = null;

/**
* Model Setup / initalization function
*
* @param Model $model
* @param array $fields
* @return boolean
*/
public function setup(Model $model, $fields = array()) {
$this->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;
}
}
53 changes: 30 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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.');
}
}
}
}
}

Expand All @@ -70,7 +77,7 @@ Create a View to display form with Uploadcare widget:
<?php echo $this->Form->create('File', array('action' => 'add')); ?>
<?php echo $this->Form->input('File.file_id', array('type' => 'hidden', 'role' => 'uploadcare-uploader')); ?>
<?php echo $this->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.

Expand All @@ -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:

<?php foreach ($files as $file): ?>
<?php echo $this->Uploadcare->file($file['File']['file_id'])->scaleCrop(400, 400)->getImgTag(); ?>
<?php endforeach; ?>

"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.
Expand Down
41 changes: 0 additions & 41 deletions Uploadcare/Model/Behavior/UploadcareBehavior.php

This file was deleted.

45 changes: 0 additions & 45 deletions Uploadcare/View/Helper/UploadcareHelper.php

This file was deleted.

39 changes: 39 additions & 0 deletions View/Helper/UploadcareHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Upload care allows you to add basic API usage on your Models
*/

class UploadcareHelper extends AppHelper {

/**
* Uploadcare API (placeholder)
*
* @var Uploadcare_Api
*/
private $api = null;

/**
* 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;
}

/**
* Get File
*
* @param $file_id File ID
* @return Uploadcare_File
*/
public function file($file_id) {
return $this->api()->getFile($file_id);
}
}