diff --git a/Controller/BlogEntriesController.php b/Controller/BlogEntriesController.php index 30912d2..5b71b3c 100644 --- a/Controller/BlogEntriesController.php +++ b/Controller/BlogEntriesController.php @@ -16,6 +16,8 @@ * @property BlogEntry $BlogEntry * @property BlogCategory $BlogCategory */ + + class BlogEntriesController extends BlogsAppController { /** @@ -29,6 +31,7 @@ class BlogEntriesController extends BlogsAppController { ); /** + * @var array helpers * @var array helpers */ public $helpers = array( @@ -70,13 +73,22 @@ class BlogEntriesController extends BlogsAppController { ), ), 'Categories.Categories', + 'Files.Download', + 'AuthorizationKeys.AuthorizationKey' => [ + //'operationType' => AuthorizationKeyComponent::OPERATION_REDIRECT, + 'operationType' => 'popup', + //'operationType' => 'redirect', + 'targetAction' => 'download_pdf', + 'model' => 'BlogEntry', + ], 'ContentComments.ContentComments' => array( 'viewVarsKey' => array( 'contentKey' => 'blogEntry.BlogEntry.key', 'useComment' => 'blogSetting.use_comment' ), 'allow' => array('view') - ) ); + ) + ); /** * @var array 絞り込みフィルタ保持値 @@ -94,8 +106,10 @@ class BlogEntriesController extends BlogsAppController { */ public function beforeFilter() { // ゲストアクセスOKのアクションを設定 - $this->Auth->allow('index', 'view', 'category', 'tag', 'year_month'); + $this->Auth->allow('index', 'view', 'category', 'tag', 'year_month', 'download', 'download_pdf'); //$this->Categories->initCategories(); + //$this->AuthorizationKey->contentId =23; // TODO hardcord + //$this->AuthorizationKey->model ='BlogEntry'; // TODO hardcord parent::beforeFilter(); } @@ -296,7 +310,65 @@ public function view() { } } -/** + public function download() { + // ここから元コンテンツを取得する処理 + $this->_prepare(); + $key = $this->params['pass'][1]; + $conditions = $this->BlogEntry->getConditions( + Current::read('Block.id'), + $this->Auth->user('id'), + $this->_getPermission(), + $this->_getCurrentDateTime() + ); + + $conditions['BlogEntry.key'] = $key; + $options = array( + 'conditions' => $conditions, + ); + $blogEntry = $this->BlogEntry->find('first', $options); + // ここまで元コンテンツを取得する処理 + + // ダウンロード実行 + if ($blogEntry) { + return $this->Download->doDownload($blogEntry['BlogEntry']['id']); + } else { + // 表示できない記事へのアクセスなら404 + throw new NotFoundException(__('Invalid blog entry')); + } + } + + public function download_pdf() { + // ここから元コンテンツを取得する処理 + $this->_prepare(); + $key = $this->params['pass'][1]; + + $conditions = $this->BlogEntry->getConditions( + Current::read('Block.id'), + $this->Auth->user('id'), + $this->_getPermission(), + $this->_getCurrentDateTime() + ); + + $conditions['BlogEntry.key'] = $key; + $options = array( + 'conditions' => $conditions, + 'recursive' => 1, + ); + $blogEntry = $this->BlogEntry->find('first', $options); + // ここまで元コンテンツを取得する処理 + + $this->AuthorizationKey->guard('popup', 'BlogEntry', $blogEntry); + + // ダウンロード実行 + if ($blogEntry) { + return $this->Download->doDownload($blogEntry['BlogEntry']['id'], ['filed' => 'pdf', 'download' => true]); + } else { + // 表示できない記事へのアクセスなら404 + throw new NotFoundException(__('Invalid blog entry')); + } + } + + /** * 年月選択肢をViewへセット * * @return void diff --git a/Controller/BlogEntriesEditController.php b/Controller/BlogEntriesEditController.php index 2a1cb8c..83d8ff0 100644 --- a/Controller/BlogEntriesEditController.php +++ b/Controller/BlogEntriesEditController.php @@ -45,6 +45,8 @@ class BlogEntriesEditController extends BlogsAppController { 'Categories.Categories', //'Blogs.BlogEntryPermission', 'NetCommons.NetCommonsTime', + 'Files.FileUpload', + 'Files.Download', ); /** @@ -201,4 +203,113 @@ public function delete() { NetCommonsUrl::actionUrl( array('controller' => 'blog_entries', 'action' => 'index', 'frame_id' => Current::read('Frame.id'), 'block_id' => Current::read('Block.id')))); } + + public function import() { + App::uses('CsvFileReader', 'Files.Utility'); + if ($this->request->is(array('post', 'put'))) { + $file = $this->FileUpload->getTemporaryUploadFile('import_csv'); + debug($file); + $reader = new CsvFileReader($file); + foreach($reader as $row){ + debug($row); + } + } + } + + public function regist() { + $UploadFile = ClassRegistry::init('Files.UploadFile'); + $path = '/var/www/app/app/Plugin/Files/Test/Fixture/logo.gif'; + $path2 = TMP . 'logo.gif'; + copy($path, $path2); + $UploadFile->registByFilePath($path2, 'blogs', 'content_key..', 'photo'); + } + + public function attach_file() { + $UploadFile = ClassRegistry::init('Files.UploadFile'); + $path = '/var/www/app/app/Plugin/Files/Test/Fixture/logo.gif'; + $path2 = TMP . 'logo.gif'; + copy($path, $path2); + $data = $this->BlogEntry->findByIsLatest(true); + //$file = new File($path2); + $this->BlogEntry->attachFile($data, 'pdf', $path2); + + $savedData = $this->BlogEntry->findById($data['BlogEntry']['id']); + debug($savedData); + } + + public function temporary_download() { + App::uses('TemporaryFile', 'Files.Utility'); + $file = new TemporaryFile(); + $file->append('test'); + + //$this->Download = $this->Components->load('Files.Download'); + //return $this->Download->downloadFile($file, ['name', 'test.txt']); + + $this->response->file($file->path, ['name' => 'test.txt']); + return $this->response; + } + + + /** + * 配列のCSV出力例 + * + * @return CakeResponse|null + */ + public function csv_download2() { + + if ($this->request->is(array('post', 'put'))) { + App::uses('CsvFileWriter', 'Files.Utility'); + + $data = [ + ['データID', 'タイトル', '本文', '作成日時'], + [1, '薪だなつくりました', '薪だなつくるの大変だったよ', '2015-10-01 10:00:00'], + [2, '薪ストーブ点火', '寒くなってきたので薪ストーブに火入れましたよ', '2015-12-01 13:00:00'], + ]; + $csvWriter = new CsvFileWriter(); + foreach($data as $line){ + $csvWriter->add($line); + } + $csvWriter->close(); + + return $csvWriter->download('export.csv'); + } + + } + + /** + * Modelから取得したデータの指定カラムだけCSV出力する例 + * + * @return CakeResponse + */ + public function csv_download3() { + App::uses('CsvFileWriter', 'Files.Utility'); + + $header = [ + 'BlogEntry.id' => 'データID', + 'BlogEntry.title' => 'タイトル', + 'BlogEntry.body1' => '本文1', + 'BlogEntry.publish_start' => '公開日時' + ]; + $result = $this->BlogEntry->find('all'); + + $csvWriter = new CsvFileWriter(['header' => $header]); + foreach($result as $data){ + $csvWriter->addModelData($data); + } + $csvWriter->close(); + + //$zip = new ZipArchive(); + //$tmpFile = new TemporaryFile(); + //$zip->open($tmpFile->path, ZipArchive::CREATE | ZipArchive::OVERWRITE); + //$zip->addFile($csvWriter->path); + //$zip->close(); + + // パスワード + + + //return $csvWriter->download('export.csv'); + return $csvWriter->zipDownload('test.zip', '日本語ファイル名.csv', 'pass'); + } + } + diff --git a/Model/BlogEntry.php b/Model/BlogEntry.php index bf8ac5e..5127d6e 100644 --- a/Model/BlogEntry.php +++ b/Model/BlogEntry.php @@ -12,6 +12,7 @@ App::uses('BlogsAppModel', 'Blogs.Model'); App::uses('NetCommonsTime', 'NetCommons.Utility'); +//App::uses('AttachmentBehavior', 'Files.Model/Behavior'); /** * Summary for BlogEntry Model @@ -21,7 +22,7 @@ class BlogEntry extends BlogsAppModel { /** * @var int recursiveはデフォルトアソシエーションなしに */ - public $recursive = -1; + public $recursive = 0; /** * use behaviors @@ -37,6 +38,21 @@ class BlogEntry extends BlogsAppModel { 'Likes.Like', 'Workflow.WorkflowComment', //'Categories.Category', + 'Files.Attachment' => [ + 'foo_photo' => [ + 'thumbnailSizes' => array( + // デフォルトはAttachmentビヘイビアできめてあるが、下記の様に設定も可能 + // NC2 800 > 640 > 480だった + 'big' => '800ml', + 'medium' => '400ml', + 'small' => '200ml', + 'thumb' => '80x80', + ), + //'contentKeyFieldName' => 'id' + ], + 'pdf', + ], + 'AuthorizationKeys.AuthorizationKey', 'ContentComments.ContentComment' ); @@ -53,13 +69,11 @@ class BlogEntry extends BlogsAppModel { 'fields' => '', 'order' => '' ), - // 'CategoryOrder' => array( - // 'className' => 'Categories.CategoryOrder', - // 'foreignKey' => false, - // 'conditions' => 'CategoryOrder.category_key=Category.key', - // 'fields' => '', - // 'order' => '' - // ) + //'PhotoFile' => array( // コンテンツを更新してもファイル差し替えないこともあるのでコンテンツn:ファイル1の関係 + // 'className' => 'Files.Attachment', + // 'foreignKey' => 'photo_file_id', + // 'conditions' => '', + //),// 関連づけておいて、afterSaveで関連モデルのsaveする? ); /** @@ -69,6 +83,12 @@ class BlogEntry extends BlogsAppModel { */ protected function _getValidateSpecification() { $validate = array( + 'pdf' => [ + 'rule' => array('isValidExtension', array('pdf'), false), + 'message' => 'pdf only' + ], + + 'title' => array( 'notBlank' => [ 'rule' => array('notBlank'), diff --git a/Test/Case/SandBoxTest.php b/Test/Case/SandBoxTest.php new file mode 100644 index 0000000..a800ca0 --- /dev/null +++ b/Test/Case/SandBoxTest.php @@ -0,0 +1,60 @@ + + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + */ + +App::uses('Blog', 'Blogs.Model'); +App::uses('BlogsAppModelTestBase', 'Blogs.Test/Case/Model'); + +/** + * Summary for Blog Test Case + * + * @property Blog $Blog + */ +class SandBoxTest extends BlogsAppModelTestBase { + +/** + * Fixtures + * + * @var array + */ + public $fixtures = array( + //'plugin.blogs.blog', + //'plugin.blogs.blog_entry', + //'plugin.blogs.blog_setting', + //'plugin.blocks.block', + //'plugin.blocks.block_role_permission', + //'plugin.rooms.room', + //'plugin.rooms.roles_room', + //'plugin.categories.category', + //'plugin.categories.categoryOrder', + //'plugin.frames.frame', + //'plugin.boxes.box', + //'plugin.blogs.plugin', + //'plugin.m17n.language', + //'plugin.users.user', + ); + + public function testIndex() { + $e1 = new extend1(); + $e2 = new extend2(); + $e1::$staticVal = 'foo'; + debug($e2::$staticVal); + } + +} + +class base +{ + static $staticVal; +} +class extend1 extends base{ + +} +class extend2 extends base{ + +} \ No newline at end of file diff --git a/View/BlogEntries/index.ctp b/View/BlogEntries/index.ctp index 19dac45..edc71d2 100644 --- a/View/BlogEntries/index.ctp +++ b/View/BlogEntries/index.ctp @@ -36,6 +36,15 @@ echo $this->Html->css( 'inline' => false ) ); +echo $this->Html->script( + '/AuthorizationKeys/js/authorization_keys.js', + array( + 'plugin' => false, + 'once' => true, + 'inline' => false + ) +); + ?>
@@ -117,7 +126,8 @@ echo $this->Html->css( 'controller' => 'blog_entries', 'action' => 'view', //'frame_id' => Current::read('Frame.id'), - 'key' => $blogEntry['BlogEntry']['key'] + 'key' => $blogEntry['BlogEntry']['key'], + 'photo' ) ) ); @@ -125,6 +135,39 @@ echo $this->Html->css( element('entry_meta_info', array('blogEntry' => $blogEntry)); ?> + +
+ Image : + Html->image( + $this->NetCommonsHtml->url( + [ + 'action' => 'download', + 'key' => $blogEntry['BlogEntry']['key'], + 'photo', + 'thumb', + ] + ) + ); ?> +
+ +
+ PDF : + Html->link('PDF', + '#', + ['authorization-keys-popup-link', + 'url' => $this->NetCommonsHtml->url( + [ + 'action' => 'download_pdf', + 'key' => $blogEntry['BlogEntry']['key'], + 'pdf', + ] + ), + 'frame-id' => Current::read('Frame.id') + ] + ); ?> +
+ +
diff --git a/View/BlogEntries/view.ctp b/View/BlogEntries/view.ctp index a6c7579..a954e18 100644 --- a/View/BlogEntries/view.ctp +++ b/View/BlogEntries/view.ctp @@ -42,6 +42,34 @@ echo $this->Html->css( element('BlogEntries/edit_link', array('status' => $blogEntry['BlogEntry']['status'])); ?>
+ +
+ Image : + Html->image( + $this->NetCommonsHtml->url( + [ + 'action' => 'download', + 'key' => $blogEntry['BlogEntry']['key'], + 'photo', + 'big', + ] + ) + ); ?> +
+
+ PDF : + Html->link('PDF', + $this->NetCommonsHtml->url( + [ + 'action' => 'download_pdf', + 'key' => $blogEntry['BlogEntry']['key'], + 'pdf', + ] + ) + ); ?> +
+ +
diff --git a/View/BlogEntriesEdit/csv_download2.ctp b/View/BlogEntriesEdit/csv_download2.ctp new file mode 100644 index 0000000..4d58b7b --- /dev/null +++ b/View/BlogEntriesEdit/csv_download2.ctp @@ -0,0 +1,7 @@ + 'get' +]; +echo $this->NetCommonsForm->create(false, $options); +echo $this->NetCommonsForm->submit('ダウンロード'); +echo $this->NetCommonsForm->end(); \ No newline at end of file diff --git a/View/BlogEntriesEdit/form.ctp b/View/BlogEntriesEdit/form.ctp index ae99bce..010f693 100644 --- a/View/BlogEntriesEdit/form.ctp +++ b/View/BlogEntriesEdit/form.ctp @@ -21,6 +21,7 @@ echo $this->Html->script( 'inline' => false ) ); +//$this->request->data['BlogEntry']['flag'] = 0; ?> false, ), 'div' => 'form-control', - 'novalidate' => true + 'novalidate' => true, + 'type' => 'file', ) ); + echo $this->Form->input('flag', ['type' => 'hidden']); $this->NetCommonsForm->unlockField('Tag'); ?> NetCommonsForm->input('key', array('type' => 'hidden')); ?> NetCommonsForm->input('key', array('type' => 'hidden')); ?> - Form->hidden('Frame.id', array( - // 'value' => Current::read('Frame.id'), - // )); ?>
@@ -79,6 +79,13 @@ $dataJson = json_encode( ));?>
+ NetCommonsForm->uploadFile('foo_photo'); + ?> + + NetCommonsForm->uploadFile('BlogEntry.pdf', ['help' => 'PDFを選ぶぺん']); + ?> NetCommonsForm->input('publish_start', diff --git a/View/BlogEntriesEdit/import.ctp b/View/BlogEntriesEdit/import.ctp new file mode 100644 index 0000000..e710583 --- /dev/null +++ b/View/BlogEntriesEdit/import.ctp @@ -0,0 +1,8 @@ +NetCommonsForm->create(false, ['type' => 'file']); +//echo $this->NetCommonsForm->create(null, ['type' => 'file']); +echo $this->NetCommonsForm->input('import_csv', ['type' => 'file']); +?> +NetCommonsForm->submit(); +echo $this->NetCommonsForm->end(); diff --git a/composer.json b/composer.json index 73fc72b..14b0562 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "netcommons/tags": "dev-master", "satooshi/php-coveralls": "dev-master" }, - "license": "NetCommons License", + "license": "LicenseRef-NetCommons", + "license-ref-net-commons": "https://raw.githubusercontent.com/NetCommons3/NetCommons3/master/license.txt", "authors": [ { "name": "Ryuji AMANO (RYUS INC.)",