mediaãã©ã°ã¤ã³å°å ¥(1) åºæ¬çãªå°å ¥
ç»åãªã©ãã¢ãããã¼ããã¡ã¤ã«ãä¿åã»è¡¨ç¤ºãããã©ã°ã¤ã³ãMedia Plugin for CakePHPããå°å ¥ãã¦ã¿ã¾ããã
å人çã«ãå®éã«ä½¿ãã«ã¯ãè¥å¹²æ©è½è¿½å ã欲ããæãã§ãããã¾ãã¯åºæ¬æ©è½ã®å°å
¥ããã
ä»åã®ç®æ¨
- ãã¦ã¼ã¶ã®ã¤ã¡ã¼ã¸ç»åã¢ãããã¼ãï¼è¡¨ç¤ºãæ©è½ã追å
- ï¼ã¦ã¼ã¶ãè¨å®ã§ããã¤ã¡ã¼ã¸ç»åã¯ã1種é¡
- ã¢ãããã¼ãç»é¢ã¯ãusers/edit(ã¦ã¼ã¶æ å ±ç·¨éç»é¢ï¼ã¨ã¯å¥ã¢ã¯ã·ã§ã³(usersã®é ç®ãç·¨éããªã)ã
- CakePHPã®ãã¼ã¸ã§ã³ã¯1.2.5
æéåã£ãç¹
- Formã«type => 'file'ã®è¨å®ãã¹
- user_idã®è¨å®(usersã®é ç®ãç·¨éããªãç»åè¨å®ã¢ã¯ã·ã§ã³ã«è¨å®ããããï¼
åèãµã¤ã
æé
CakePHP1.2.3 MediaPluginをつかってみた - 忍び歩く男 - SLYWALKER
http://d.hatena.ne.jp/cake-memo/20091125
ãã©ã°ã¤ã³æ¦è¦ã»ã½ã¼ã¹æ§æãªã©
【CakePHP】「Media Plugin」の概要と構成
(追å )æ¬å®¶ç´¹ä»è³æ
The_CakePHP_Media_Plugin.pdf
å°å ¥ä½æ¥
- GitHubããã½ã¼ã¹åå¾ã
ãã¼ã¸ã§ã³ã¯0.6.0ã
- app/plugins/media ãä½æããåå¾ããã½ã¼ã¹ãå ¨ã¦ã¢ãããã¼ã
[cake@cake cake-frame]$ mkdir app/plugins/media (ã¢ãããã¼ãã¯çç¥ï¼
- attachmentsãã¼ãã«ã®ä½æ
[cake@cake cake-frame]$ ./cake/console/cake schema run create media -path app/plugins/media/config/sql/ Welcome to CakePHP v1.2.4.8284 Console --------------------------------------------------------------- App : app Path: /var/www/html/cake-frame/app --------------------------------------------------------------- Cake Schema Shell --------------------------------------------------------------- The following table(s) will be dropped. attachments Are you sure you want to drop the table(s)? (y/n) [n] > y Dropping table(s). attachments updated. The following table(s) will be created. attachments Are you sure you want to create the table(s)? (y/n) [y] > y Creating table(s). attachments updated. End create. 1. DROP TABLE IF EXISTS `attachments`; 2. CREATE TABLE `attachments` ( `id` int(10) NOT NULL AUTO_INCREMENT, `model` varchar(255) NOT NULL, `foreign_key` int(10) NOT NULL, `dirname` varchar(255) DEFAULT NULL, `basename` varchar(255) NOT NULL, `checksum` varchar(255) NOT NULL, `group` varchar(255) DEFAULT NULL, `alternative` varchar(50) DEFAULT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`));
- app/webroot/mediaã®ä½æã¨ãã¼ããã·ã§ã³è¨å®
ver0.6ã§ã¯ãcakeã³ãã³ãããã¤ã³ã¿ã©ã¯ãã£ãã«ä½æã§ãã¾ãã
transfer/.htaccessã®ä½æããèªåã§ã§ãã¦ãã¾ãã
[cake@cake cake-frame]$ ./cake/console/cake media -------------------------------------------------------------------------------- Media Shell -------------------------------------------------------------------------------- [I]nitialize Media Directory [P]rotect Transfer Directory [S]ynchronize [M]ake [C]ollect [H]elp [Q]uit What would you like to do? (I/P/S/M/C/H/Q) [q] > I Do you want to create missing media directories now? [n] > y /app/webroot/media/ [SKIP] /app/webroot/media/static/ [OK ] /app/webroot/media/static/aud [OK ] /app/webroot/media/static/css [OK ] /app/webroot/media/static/doc [OK ] /app/webroot/media/static/gen [OK ] /app/webroot/media/static/ico [OK ] /app/webroot/media/static/img [OK ] /app/webroot/media/static/js [OK ] /app/webroot/media/static/txt [OK ] /app/webroot/media/static/vid [OK ] /app/webroot/media/transfer/ [OK ] /app/webroot/media/transfer/aud [OK ] /app/webroot/media/transfer/css [OK ] /app/webroot/media/transfer/doc [OK ] /app/webroot/media/transfer/gen [OK ] /app/webroot/media/transfer/ico [OK ] /app/webroot/media/transfer/img [OK ] /app/webroot/media/transfer/js [OK ] /app/webroot/media/transfer/txt [OK ] /app/webroot/media/transfer/vid [OK ] /app/webroot/media/filter/ [OK ] Your transfer directory is missing a htaccess file to block requests. Do you want to create it now? [n] > y /app/webroot/media/transfer/.htaccess created. Remember to set the correct permissions on transfer and filter directory. -------------------------------------------------------------------------------- Media Shell -------------------------------------------------------------------------------- [I]nitialize Media Directory [P]rotect Transfer Directory [S]ynchronize [M]ake [C]ollect [H]elp [Q]uit What would you like to do? (I/P/S/M/C/H/Q) [q] > Q Quitting. [cake@cake cake-frame]$ chmod -R 777 app/webroot/media/transfer/ [cake@cake cake-frame]$ chmod -R 777 app/webroot/media/filter/ [cake@cake cake-frame]$ ls -l app/webroot/media/ åè¨ 24 drwxrwxrwx 2 cake cake 4096 2æ 15 23:14 filter drwxr-xr-x 11 cake cake 4096 2æ 15 23:14 static drwxrwxrwx 11 cake cake 4096 2æ 15 23:14 transfer
ã¢ãããã¼ããã¡ã¤ã«ã«ã¤ãã¦ã.gitignoreã®è¨å®è¿½å ãå¿ããã«ã
--- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ app/tmp/* app/config/core.php app/config/database.php +app/webroot/media/*
- ãã¡ã¤ã«ã®ç§»å
ãã¡ãããcakeã³ãã³ãããå®æ½ã
webroot/js/ã¨webroot/css/ã«ãããã¡ã¤ã«ã追å ãã¦ãªãã®ã§ãå®è³ªãã®2ãã£ã¬ã¯ããªããã®ãã¡ã¤ã«ç§»åã«ãªãã¾ãã
ã§ãããã¯ãmediaãã©ã°ã¤ã³çµç±ã§ãã¡ã¤ã«ãå¼ã³åºãå ´åã®ã¿ç§»åãå¿ è¦ã®ãããªã®ã§ãé常éãå¼ã³åºããªãä¸è¦ã®ããã§ãã
-------------------------------------------------------------------------------- Media Shell -------------------------------------------------------------------------------- [I]nitialize Media Directory [P]rotect Transfer Directory [S]ynchronize [M]ake [C]ollect [H]elp [Q]uit What would you like to do? (I/P/S/M/C/H/Q) [q] > C Would you like to collect files from /app/vendors/? [y] > y Would you like to collect files from /vendors/? [y] > y Would you like to collect files from /app/webroot/? [y] > y Would you like to collect files from /app/plugins/cakeplus/vendors/? [y] > y Would you like to collect files from /app/plugins/media/vendors/? [y] > y Would you like to add another path? [n] > n Would you like to link (instead of copy) the files? [n] > n Mapping -------------------------------------------------------------------------------- /app/webroot/favicon.ico -> /app/webroot/media/static/img/favicon.ico /app/webroot/js/jquery-1.3.2.min.js -> /app/webroot/media/static/gen/jquery-1.3.2.min.js ï¼ä»¥ä¸ã移åãããã¡ã¤ã«ãåè¨ï¼ Looks OK? [y] > y Collecting -------------------------------------------------------------------------------- 2.94% [OK ] /app/webroot/favicon.ico 5.88% [OK ] /app/webroot/js/jquery-1.3.2.min.js (以ä¸ã移åå¦çåè¨ï¼ [I]nitialize Media Directory [P]rotect Transfer Directory [S]ynchronize [M]ake [C]ollect [H]elp [Q]uit What would you like to do? (I/P/S/M/C/H/Q) [q] > q Quitting.
- media pluginè¨å®ã®èªã¿è¾¼ã¿è¿½å
app/config/bootstrap.php å·®å
--- a/app/config/bootstrap.php +++ b/app/config/bootstrap.php @@ -45,5 +45,8 @@ require_once ('init.php'); require_once ('config.php'); +/* media plugin */ +require_once(APP.'plugins'.DS.'media'.DS.'config'.DS.'core.php'); +
init.phpãããã³config.phpã¯ç¬èªã«è¿½å ããè¨å®ãã¡ã¤ã«ã
media pluginè¨å®ã®ä¸æ¸ããèæ
®ãã¦ããããã®ãã¡ã¤ã«ã®å¾ã«media pluginã®è¨å®ãèªã¿è¾¼ãäºã«ãã¾ããã
- ãã¡ã¤ã«ãã¢ãããã¼ãããã¢ãã«ã«ãAssociationã追å
対象ã¢ãã«ã¯Userãªã®ã§ã以ä¸ã®éãã«è¨å®ã追å ã
app/models/user.php å·®å
//The Associations below have been created with all possible keys, those that are not needed can be removed var $hasMany = array( + 'Attachment' => array( + 'className' => 'Media.Attachment', + 'foreignKey' => 'foreign_key', + 'conditions' => array('model' => 'User'), + 'dependent' => true, + ) );
ãããããç»åç·¨éç»é¢ãå¥ä½æãããããåèãµã¤ãã¨éãç¹ãåºã¦ãã¾ãã
- ã³ã³ããã¼ã©ç·¨é
app/controllers/users_controller.php
--- a/app/controllers/users_controller.php +++ b/app/controllers/users_controller.php @@ -10,6 +10,7 @@ class UsersController extends ModuleController { var $actionMapPlus = array( 'listview' => 'read', 'change_password' => 'update', + 'edit_image' => 'update', ); var $disableTokenActions = array('add','mobile_add'); @@ -73,6 +74,31 @@ class UsersController extends ModuleController { self::_edit($id); } + function edit_image() { + $id = $this->AuthPlus->user("id"); + + if (!empty($this->data)) { + $this->data['User']['id'] = $id; + + if ($this->User->saveAll($this->data, array('validate' => 'first'))) { + $this->Session->setFlash(__('The User has been saved', true)); + $this->redirect(array('action'=>'index')); + } else { + $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); + $this->data['User']['id'] = $id; + } + } + + $this->data = $this->User->read(null, $id); + } +
ããããæ¸ãã¦ã¾ããã
$this->data['User']['id'] = $id; if ($this->User->saveAll($this->data, array('validate' => 'first'))) {
ãã®ï¼ã¤ãè¦ã§ãã
- 追å ãã³ãã¬ã¼ã
app/views/users/edit_image.ctp
<div class="users form"> <?php echo $form->create('User', array( 'url' => array( 'controller' => 'users', 'action' => 'edit_image', ), 'type' => 'file', ));?> <fieldset> <legend><?php __('Edit Image');?></legend> <?php echo $this->element('attachments', array('plugin' => 'media')); echo $token->create(); ?> </fieldset> <?php echo $form->end('Submit');?> </div> <div class="backButton"> <?php echo $form->create('', array('url' => array( 'action' => 'view', $this->data['User']['id']), 'type' => 'GET', )); echo $form->end('Cancel'); ?> </div>
users/edit_image/edit_imageãããã¢ãããã¼ãããç»åãé¸æãTextual replacementã¨ããé
ç®ã«ãé©å½ãªæååãå
¥åãã¦éä¿¡ããã¨ã
ã¢ã¯ã·ã§ã³ã§åä¿¡ãã$this->dataã¯ã以ä¸ã®ããã«ãªã£ã¦ãã¾ãã
array(2) { ["Attachment"]=> array(1) { [0]=> array(4) { ["model"]=> string(4) "User" ["group"]=> string(10) "attachment" ["alternative"]=> string(4) "another name" ["file"]=> array(5) { ["name"]=> string(11) "tooMuch.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phplHK1t1" ["error"]=> int(0) ["size"]=> int(32702) } } } ["User"]=> array(1) { ["__Token"]=> string(32) "a5f4435546a39b8dcd462ada229a9eb9"} }
ããã¦ãattachmentsã«ã¯ä»¥ä¸ã®ãã¼ã¿ãä¿åããã¾ãã
mysql> SELECT * FROM attachments\G *************************** 1. row *************************** id: 1 model: User foreign_key: 1 dirname: transfer/img basename: toomuch.jpg checksum: e5c689305d1dfb481a3d098280034e21 group: attachment alternative: another name created: 2010-02-16 00:34:40 modified: 2010-02-16 00:34:40 1 row in set (0.00 sec)
ããã¦å®ç»åãã¼ã¿ã¯ãdirname/basenameã§ãã以ä¸ã«ä¿åããã¦ãã¾ããã
[cake@cake cake-frame]$ ls -l app/webroot/media/transfer/img/ åè¨ 36 -rw-r--r-- 1 apache apache 32702 2æ 16 00:34 toomuch.jpg