PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/plugins/blog/models/blog_category.php

https://github.com/hashing/basercms
PHP | 315 lines | 192 code | 18 blank | 105 comment | 22 complexity | edcef9b2ace91e2e2e8d5dfdc1b14f08 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.plugins.blog.models
  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. * Include files
  22. */
  23. /**
  24. * ブログカテゴリモデル
  25. *
  26. * @package baser.plugins.blog.models
  27. */
  28. class BlogCategory extends BlogAppModel {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'BlogCategory';
  36. /**
  37. * バリデーション設定
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $validationParams = array();
  43. /**
  44. * actsAs
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $actsAs = array('Tree', 'BcCache');
  50. /**
  51. * hasMany
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $hasMany = array('BlogPost'=>
  57. array('className'=>'Blog.BlogPost',
  58. 'order'=>'id DESC',
  59. 'limit'=>10,
  60. 'foreignKey'=>'blog_category_id',
  61. 'dependent'=>false,
  62. 'exclusive'=>false,
  63. 'finderQuery'=>''));
  64. /**
  65. * validate
  66. *
  67. * @var array
  68. * @access public
  69. */
  70. var $validate = array(
  71. 'name' => array(
  72. array( 'rule' => array('notEmpty'),
  73. 'message' => "ブログカテゴリ名を入力してください。",
  74. 'required' => true),
  75. array( 'rule' => 'halfText',
  76. 'message' => 'ブログカテゴリ名は半角のみで入力してください。'),
  77. array( 'rule' => array('duplicateBlogCategory'),
  78. 'message' => '入力されたブログカテゴリは既に登録されています。'),
  79. array( 'rule' => array('maxLength', 255),
  80. 'message' => 'ブログカテゴリ名は255文字以内で入力してください。')
  81. ),
  82. 'title' => array(
  83. array( 'rule' => array('notEmpty'),
  84. 'message' => "ブログカテゴリタイトルを入力してください。",
  85. 'required' => true),
  86. array( 'rule' => array('maxLength', 255),
  87. 'message' => 'ブログカテゴリ名は255文字以内で入力してください。')
  88. )
  89. );
  90. /**
  91. * コントロールソースを取得する
  92. *
  93. * @param string フィールド名
  94. * @return array コントロールソース
  95. * @access public
  96. */
  97. function getControlSource($field,$options = array()) {
  98. switch($field) {
  99. case 'parent_id':
  100. if(!isset($options['blogContentId'])) {
  101. return false;
  102. }
  103. $conditions = array();
  104. if(isset($options['conditions'])) {
  105. $conditions = $options['conditions'];
  106. }
  107. $conditions['BlogCategory.blog_content_id'] = $options['blogContentId'];
  108. if(!empty($options['excludeParentId'])) {
  109. $children = $this->children($options['excludeParentId']);
  110. $excludeIds = array($options['excludeParentId']);
  111. foreach($children as $child) {
  112. $excludeIds[] = $child['BlogCategory']['id'];
  113. }
  114. $conditions['NOT']['BlogCategory.id'] = $excludeIds;
  115. }
  116. if(isset($options['ownerId'])) {
  117. $ownerIdConditions = array(
  118. array('BlogCategory.owner_id' => null),
  119. array('BlogCategory.owner_id' => $options['ownerId']),
  120. );
  121. if(isset($conditions['OR'])) {
  122. $conditions['OR'] = am($conditions['OR'],$ownerIdConditions);
  123. } else {
  124. $conditions['OR'] = $ownerIdConditions;
  125. }
  126. }
  127. $parents = $this->generatetreelist($conditions);
  128. $controlSources['parent_id'] = array();
  129. foreach($parents as $key => $parent) {
  130. if(preg_match("/^([_]+)/i",$parent,$matches)) {
  131. $parent = preg_replace("/^[_]+/i",'',$parent);
  132. $prefix = str_replace('_','&nbsp&nbsp&nbsp',$matches[1]);
  133. $parent = $prefix.'└'.$parent;
  134. }
  135. $controlSources['parent_id'][$key] = $parent;
  136. }
  137. break;
  138. case 'owner_id':
  139. $UserGroup = ClassRegistry::init('UserGroup');
  140. $controlSources['owner_id'] = $UserGroup->find('list', array('fields' => array('id', 'title'), 'recursive' => -1));
  141. break;
  142. }
  143. if(isset($controlSources[$field])) {
  144. return $controlSources[$field];
  145. }else {
  146. return false;
  147. }
  148. }
  149. /**
  150. * 同じニックネームのカテゴリがないかチェックする
  151. * 同じブログコンテンツが条件
  152. *
  153. * @param array $check
  154. * @return boolean
  155. * @access public
  156. */
  157. function duplicateBlogCategory($check) {
  158. $conditions = array('BlogCategory.'.key($check)=>$check[key($check)],
  159. 'BlogCategory.blog_content_id' => $this->validationParams['blogContentId']);
  160. if($this->exists()) {
  161. $conditions['NOT'] = array('BlogCategory.id'=>$this->id);
  162. }
  163. $ret = $this->find($conditions);
  164. if($ret) {
  165. return false;
  166. }else {
  167. return true;
  168. }
  169. }
  170. /**
  171. * 関連する記事データをカテゴリ無所属に変更し保存する
  172. *
  173. * @param boolean $cascade
  174. * @return boolean
  175. * @access public
  176. */
  177. function beforeDelete($cascade = true) {
  178. parent::beforeDelete($cascade);
  179. $ret = true;
  180. if(!empty($this->data['BlogCategory']['id'])){
  181. $id = $this->data['BlogCategory']['id'];
  182. $this->BlogPost->unBindModel(array('belongsTo'=>array('BlogCategory')));
  183. $datas = $this->BlogPost->find('all',array('conditions'=>array('BlogPost.blog_category_id'=>$id)));
  184. if($datas) {
  185. foreach($datas as $data) {
  186. $data['BlogPost']['blog_category_id'] = '';
  187. $this->BlogPost->set($data);
  188. if(!$this->BlogPost->save()) {
  189. $ret = false;
  190. }
  191. }
  192. }
  193. }
  194. return $ret;
  195. }
  196. /**
  197. * カテゴリリストを取得する
  198. *
  199. * @param int $id
  200. * @param boolean $count
  201. * @return array
  202. * @access public
  203. */
  204. function getCategoryList($blogContentId, $options) {
  205. $options = array_merge(array(
  206. 'depth' => 1,
  207. 'type' => null,
  208. 'order' => 'BlogCategory.id',
  209. 'limit' => false,
  210. 'viewCount' => false,
  211. 'fields' => array('id', 'name', 'title')
  212. ), $options);
  213. $datas = array();
  214. extract($options);
  215. if(!$type) {
  216. $datas = $this->_getCategoryList($blogContentId, null, $viewCount, $depth);
  217. } elseif($type == 'year') {
  218. $options = array(
  219. 'category' => true,
  220. 'limit' => $limit,
  221. 'viewCount' => $viewCount,
  222. 'type' => 'year'
  223. );
  224. $_datas = $this->BlogPost->getPostedDates($blogContentId, $options);
  225. $datas = array();
  226. foreach($_datas as $data) {
  227. if($viewCount) {
  228. $data['BlogCategory']['count'] = $data['count'];
  229. }
  230. $datas[$data['year']][] = array('BlogCategory' => $data['BlogCategory']);
  231. }
  232. }
  233. return $datas;
  234. }
  235. /**
  236. * カテゴリリストを取得する(再帰処理)
  237. *
  238. * @param int $blogContentId
  239. * @param int $id
  240. * @param int $viewCount
  241. * @param int $depth
  242. * @param int $current
  243. * @param array $fields
  244. * @return array
  245. */
  246. function _getCategoryList($blogContentId, $id = null, $viewCount = false, $depth = 1, $current = 1, $fields = array()) {
  247. $datas = $this->find('all',array(
  248. 'conditions'=>array('BlogCategory.blog_content_id' => $blogContentId, 'BlogCategory.parent_id' => $id),
  249. 'fields' => $fields,
  250. 'recursive' => -1));
  251. if($datas) {
  252. foreach($datas as $key => $data) {
  253. if($viewCount) {
  254. $datas[$key]['BlogCategory']['count'] = $this->BlogPost->find('count', array(
  255. 'conditions' =>
  256. am(
  257. array('BlogPost.blog_category_id' => $data['BlogCategory']['id']),
  258. $this->BlogPost->getConditionAllowPublish()
  259. ),
  260. 'cache' => false
  261. ));
  262. }
  263. if($current < $depth) {
  264. $current++;
  265. $children = $this->_getCategoryList($blogContentId, $data['BlogCategory']['id'], $viewCount, $depth, $current);
  266. if($children) {
  267. $datas[$key]['BlogCategory']['children'] = $children;
  268. }
  269. }
  270. }
  271. }
  272. return $datas;
  273. }
  274. /**
  275. * 新しいカテゴリが追加できる状態かチェックする
  276. *
  277. * @param int $userGroupId
  278. * @param boolean $rootEditable
  279. * @return boolean
  280. * @access public
  281. */
  282. function checkNewCategoryAddable($userGroupId, $rootEditable) {
  283. $newCatAddable = false;
  284. $ownerCats = $this->find('count', array(
  285. 'conditions' => array(
  286. 'OR' => array(
  287. array('BlogCategory.owner_id' => null),
  288. array('BlogCategory.owner_id' => $userGroupId)
  289. )
  290. )));
  291. if($ownerCats || $rootEditable) {
  292. $newCatAddable = true;
  293. }
  294. return $newCatAddable;
  295. }
  296. }
  297. ?>