PageRenderTime 144ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/models/behaviors/bc_contents_manager.php

https://github.com/hashing/basercms
PHP | 150 lines | 75 code | 12 blank | 63 comment | 15 complexity | 0d38c15c1a76d8b76df6598472e418b9 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * コンテンツ管理ビヘイビア
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.models.behaviors
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * コンテンツ管理ビヘイビア
  22. *
  23. * @subpackage baser.models.behaviors
  24. */
  25. class BcContentsManagerBehavior extends ModelBehavior {
  26. /**
  27. * Content Model
  28. *
  29. * @var Content
  30. * @access public
  31. */
  32. var $Content = null;
  33. /**
  34. * コンテンツデータを登録する
  35. * コンテンツデータを次のように作成して引き渡す
  36. * array('Content' =>
  37. * array( 'model_id' => 'モデルでのID'
  38. * 'category' => 'カテゴリ名',
  39. * 'title' => 'コンテンツタイトル', // 検索対象
  40. * 'detail' => 'コンテンツ内容', // 検索対象
  41. * 'url' => 'URL',
  42. * 'status' => '公開ステータス'
  43. * ))
  44. *
  45. * @param Model $model
  46. * @param array $data
  47. * @return boolean
  48. * @access public
  49. */
  50. function saveContent(&$model, $data) {
  51. if(!$data) {
  52. return;
  53. }
  54. $data['Content']['model'] = $model->alias;
  55. // タグ、空白を除外
  56. $data['Content']['detail'] = str_replace(array("\r\n","\r","\n", "\t", "\s"), '', trim(strip_tags($data['Content']['detail'])));
  57. // 検索用データとして保存
  58. $id = '';
  59. $this->Content = ClassRegistry::init('Content');
  60. if(!empty($data['Content']['model_id'])) {
  61. $before = $this->Content->find('first', array(
  62. 'fields' => array('Content.id', 'Content.category'),
  63. 'conditions' => array(
  64. 'Content.model' => $data['Content']['model'],
  65. 'Content.model_id' => $data['Content']['model_id']
  66. )));
  67. }
  68. if($before) {
  69. $data['Content']['id'] = $before['Content']['id'];
  70. $this->Content->set($data);
  71. } else {
  72. if(empty($data['Content']['priority'])) {
  73. $data['Content']['priority'] = '0.5';
  74. }
  75. $this->Content->create($data);
  76. }
  77. $result = $this->Content->save();
  78. // カテゴリを site_configsに保存
  79. if($result) {
  80. return $this->updateContentMeta($model, $data['Content']['category']);
  81. }
  82. return $result;
  83. }
  84. /**
  85. * コンテンツデータを削除する
  86. *
  87. * @param Model $model
  88. * @param string $url
  89. */
  90. function deleteContent(&$model, $id) {
  91. $this->Content = ClassRegistry::init('Content');
  92. if($this->Content->deleteAll(array('Content.model' => $model->alias, 'Content.model_id' => $id))) {
  93. return $this->updateContentMeta($model);
  94. }
  95. }
  96. /**
  97. * コンテンツメタ情報を更新する
  98. *
  99. * @param string $contentCategory
  100. * @return boolean
  101. * @access public
  102. */
  103. function updateContentMeta(&$model) {
  104. $db = ConnectionManager::getDataSource('baser');
  105. $contentCategories = array();
  106. $contentTypes = array();
  107. if($db->config['driver']=='bc_csv') {
  108. // CSVの場合GROUP BYが利用できない(baserCMS 2.0.2)
  109. $contents = $this->Content->find('all', array('conditions' => array('Content.status' => true)));
  110. foreach($contents as $content) {
  111. if($content['Content']['category'] && !in_array($content['Content']['category'], $contentCategories)) {
  112. $contentCategories[$content['Content']['category']] = $content['Content']['category'];
  113. }
  114. if($content['Content']['type'] && !in_array($content['Content']['type'], $contentTypes)) {
  115. $contentTypes[$content['Content']['type']] = $content['Content']['type'];
  116. }
  117. }
  118. } else {
  119. $contents = $this->Content->find('all', array('fields' => array('Content.category'), 'group' => array('Content.category'), 'conditions' => array('Content.status' => true)));
  120. foreach($contents as $content) {
  121. if($content['Content']['category']) {
  122. $contentCategories[$content['Content']['category']] = $content['Content']['category'];
  123. }
  124. }
  125. $contents = $this->Content->find('all', array('fields' => array('Content.type'), 'group' => array('Content.type'), 'conditions' => array('Content.status' => true)));
  126. foreach($contents as $content) {
  127. if($content['Content']['type']) {
  128. $contentTypes[$content['Content']['type']] = $content['Content']['type'];
  129. }
  130. }
  131. }
  132. $siteConfigs['SiteConfig']['content_categories'] = serialize($contentCategories);
  133. $siteConfigs['SiteConfig']['content_types'] = serialize($contentTypes);
  134. $SiteConfig = ClassRegistry::init('SiteConfig');
  135. return $SiteConfig->saveKeyValue($siteConfigs);
  136. }
  137. }
  138. ?>