PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/tag.php

https://github.com/mcatm/blox
PHP | 439 lines | 368 code | 62 blank | 9 comment | 69 complexity | b5cab7cc09b9b6121891b8980b275eeb MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class BLX_Tag {
  3. var $msg;
  4. function get($user_param = array()) {
  5. $CI =& get_instance();
  6. $CI->load->library(array('pagination', 'user'));
  7. $label = $CI->setting->get('url_alias_tag');//タグのラベル
  8. $param = array(//デフォルトの設定
  9. 'auth' => 0,
  10. 'base_url' => base_url(),
  11. 'id' => 0,
  12. 'label' => 'tag',
  13. 'num_links' => 4,
  14. 'offset' => 0,
  15. 'order' => 'desc',
  16. 'pager' => true,
  17. 'qty' => $CI->setting->get('post_max_tag_per_page'),
  18. 'query' => "",
  19. 'sort' => 'createdate',
  20. 'stack' => true,
  21. 'uri_segment' => 2,
  22. 'user' => 0,
  23. 'where' => ""
  24. );
  25. $param = array_merge($param, $user_param);//ユーザーパラメータで書き換え
  26. $CI->db->start_cache();
  27. //条件定義
  28. if ($param['where'] != "") $CI->db->where('('.$param['where'].')');//where文が直接書き込まれている場合
  29. if ($param['id'] != 0) {
  30. if (is_array($param['id'])) {//複数検索
  31. foreach($param['id'] as $t) {
  32. $CI->db->or_where('tag_id', $t);
  33. }
  34. } else {
  35. $CI->db->where('tag_id', $param['id']);
  36. }
  37. }
  38. if (isset($param['tag']) && !empty($param['tag'])) {
  39. if (is_array($param['tag'])) {//複数検索
  40. foreach($param['tag'] as $t) {
  41. $CI->db->or_where('tag_name', $t);
  42. }
  43. } else {
  44. $CI->db->where('tag_name', $param['tag']);
  45. }
  46. }
  47. if ($param['query'] != "") $CI->db->like('tag_name', $param['query']);//検索キー
  48. $CI->db->stop_cache();
  49. $count = $CI->db->count_all_results(DB_TBL_TAG);
  50. if (isset($param['count'])) {
  51. $CI->db->flush_cache();
  52. return $count;//カウントを返すだけ
  53. }
  54. if ($count > 0) {//記事が存在した場合
  55. $CI->pagination->initialize(array(
  56. 'base_url' => $param['base_url'],
  57. 'total_rows' => $count,
  58. 'uri_segment' => $param['uri_segment'],
  59. 'num_links' => $param['num_links'],
  60. 'per_page' => $param['qty']
  61. ));
  62. $CI->db->order_by('tag_'.$param['sort'], $param['order']);
  63. if ($param['stack']) {
  64. $CI->data->set($CI->db->get(DB_TBL_TAG, $param['qty'], $param['offset']), $param);
  65. } else {
  66. $out = $CI->data->get($CI->db->get(DB_TBL_TAG, $param['qty'], $param['offset']), $param);
  67. $CI->db->flush_cache();
  68. return $out;
  69. }
  70. $CI->db->flush_cache();
  71. if ($param['pager']) {
  72. $CI->data->set_array('page', array(
  73. 'total' => $CI->pagination->total_rows,
  74. 'current' => $CI->pagination->cur_page,
  75. 'qty' => $CI->pagination->per_page,
  76. 'pager' => $CI->pagination->create_links()
  77. ));
  78. }
  79. foreach($CI->data->out[$param['label']] as $k => $v) {//追加データ付与
  80. }
  81. }
  82. $CI->db->flush_cache();
  83. }
  84. function get_post($tagname, $user_param = array()) {
  85. $CI =& get_instance();
  86. $CI->load->library(array('pagination'));
  87. $param = array(//デフォルトの設定
  88. 'auth' => 0,
  89. 'base_url' => base_url(),
  90. 'comment' => false,
  91. 'ext' => true,
  92. 'file' => false,
  93. 'file_main' => true,
  94. 'file_main_arr' => array(),
  95. 'history' => false,
  96. 'id' => 0,
  97. 'id_type' => 'id',
  98. 'label' => 'post',
  99. 'neighbor' => false,
  100. 'num_links' => 4,
  101. 'offset' => 0,
  102. 'order' => 'desc',
  103. 'pager' => true,
  104. 'get_parent' => false,
  105. 'qty' => $CI->setting->get('post_max_qty_per_page'),
  106. 'query' => "",
  107. 'schedule' => false,
  108. 'sort' => 'createdate',
  109. 'stack' => true,
  110. 'tag' => true,
  111. 'uri_segment' => 2,
  112. 'user' => 0,
  113. 'where' => ""
  114. );
  115. $param = array_merge($param, $user_param);//ユーザーパラメータで書き換え
  116. //タグをゲット
  117. $tag = $this->get(array(
  118. 'tag' => $tagname,
  119. 'stack' => false
  120. ));
  121. if (isset($tag)) {
  122. $CI->db->start_cache();
  123. $CI->db->select('*, post_id AS id, post_status AS status, post_createdate AS createdate, post_type AS type');
  124. $CI->db->join(DB_TBL_LINX, 'post_id = linx_a');
  125. #$CI->db->group_by('post_id');
  126. $CI->db->where('linx_type', 'post2tag');
  127. foreach($tag as $t) {
  128. $CI->db->where('linx_b', $t['id']);
  129. }
  130. if (!isset($param['deleted'])) $CI->db->where('post_deleted', 0);//削除されているものを読み込まない
  131. if (isset($param['type'])) $CI->db->where('post_type', $param['type']);//記事タイプ
  132. if (isset($param['status'])) $CI->db->where('post_status', $param['status']);//状態
  133. if ($param['auth'] !== 'cron') {
  134. $auth_where = '(post_status <= '.$param['auth'];
  135. if (!empty($CI->data->out['me']['id']) && defined('ADMIN_MODE') && !isset($param['div'])) $auth_where .= ' OR linx_b = '.$CI->data->out['me']['id'];
  136. $auth_where .= ')';
  137. $CI->db->where($auth_where);
  138. }
  139. if ($param['qty'] == 0) $param['qty'] = $count;//qtyが0の場合は、全てを選択
  140. $CI->db->stop_cache();
  141. $count = $CI->db->count_all_results(DB_TBL_POST, false);
  142. if (isset($param['count'])) {
  143. $CI->db->flush_cache();
  144. return $count;//カウントを返すだけ
  145. }
  146. if ($count > 0) {//if the posts exist
  147. $CI->pagination->initialize(array(
  148. 'base_url' => $param['base_url'],
  149. 'total_rows' => $count,
  150. 'uri_segment' => $param['uri_segment'],
  151. 'num_links' => $param['num_links'],
  152. 'per_page' => $param['qty']
  153. ));
  154. $CI->db->order_by('post_'.$param['sort'], $param['order']);
  155. if ($param['stack']) {
  156. $CI->data->set($CI->db->get(DB_TBL_POST, $param['qty'], $param['offset']), $param);
  157. } else {
  158. $out = $CI->data->get($CI->db->get(DB_TBL_POST, $param['qty'], $param['offset']), $param);
  159. $CI->db->flush_cache();
  160. return $out;
  161. }
  162. $CI->db->flush_cache();
  163. //get a status of pages.
  164. if ($param['pager']) {
  165. $CI->data->set_array('page', array(
  166. 'total' => $CI->pagination->total_rows,
  167. 'current' => $CI->pagination->cur_page,
  168. 'offset' => $param['offset'],
  169. 'qty' => $CI->pagination->per_page,
  170. 'pager' => $CI->pagination->create_links()
  171. ));
  172. }
  173. if (isset($CI->data->out[$param['label']])) {
  174. $post_id = array();
  175. foreach($CI->data->out[$param['label']] as $p) $post_id[] = $p['id'];
  176. $param['id'] = $post_id;
  177. $param['pager'] = false;
  178. $CI->post->get($param);
  179. }
  180. }
  181. }
  182. $CI->db->flush_cache();
  183. }
  184. function count($user_param = array()) {
  185. $user_param['count'] = true;
  186. return $this->get($user_param);
  187. }
  188. function set($tagstr = "", $type = '', $row_id, $author_id = 0) {//タグの登録処理
  189. $this->_unlink_tag($type, $row_id);
  190. if ($tagstr != "") {
  191. $tag = $this->_separate_tag($tagstr);
  192. $this->_update_tag($tag, $type, $row_id, $author_id);
  193. }
  194. }
  195. function _update_tag($tag, $type = "", $row_id, $author_id = 0) {
  196. $CI =& get_instance();
  197. $CI->load->library('linx');
  198. $CI->load->helper('date');
  199. $now = now();//日付取得
  200. foreach ($tag as $k => $v) {
  201. if ($v) {
  202. $CI->db->where('tag_name', $v);
  203. if ($CI->db->count_all_results(DB_TBL_TAG) == 0) {//未登録のタグ
  204. //タグを登録
  205. $set = array(
  206. 'tag_name' => $v,
  207. 'tag_count' => 1,
  208. 'tag_createdate' => $now,
  209. 'tag_update' => $now
  210. );
  211. $CI->db->insert(DB_TBL_TAG, $set);
  212. $tag_id = $CI->db->insert_id();
  213. //タグをエントリと関連づける
  214. $lnx = array(
  215. 'a' => $row_id,
  216. 'b' => $tag_id
  217. );
  218. if ($author_id > 0) $lnx['status'] = $author_id;
  219. $CI->linx->set($type, $lnx);
  220. } else {//登録済みのタグ
  221. $CI->db->where('tag_name', $v);
  222. $q = $CI->db->get(DB_TBL_TAG);
  223. $r = $q->result();
  224. $tag_id = $r[0]->tag_id;
  225. $where = array(
  226. 'a' => $row_id,
  227. 'b' => $tag_id
  228. );
  229. $c = $CI->linx->count($type, $where);
  230. if ($c == 0) {//関連付けられていない場合、タグを関連づける
  231. $lnx = array(
  232. 'a' => $row_id,
  233. 'b' => $tag_id
  234. );
  235. if ($author_id > 0) $lnx['status'] = $author_id;
  236. $CI->linx->set($type, $lnx);
  237. }
  238. //タグのデータを更新
  239. $new_count = $CI->linx->count($type, array('b' => $tag_id));
  240. $CI->db->where('tag_id', $tag_id);
  241. $CI->db->update(DB_TBL_TAG, array(
  242. 'tag_count' => $new_count,
  243. 'tag_update' => $now
  244. ));
  245. }
  246. }
  247. }
  248. }
  249. function _separate_tag($str) {//文字列をタグの配列に変換
  250. $CI =& get_instance();
  251. $arr = explode($CI->setting->get('tag_delimiter'), $str);
  252. foreach ($arr as $k => $v) {
  253. $v = trim($v);
  254. if($v != "") $dat[$k] = htmlspecialchars(trim(strip_tags($v)));
  255. }
  256. return $dat;
  257. }
  258. function _unlink_tag($type = '', $row_id) {//タグを解除
  259. if ($type != "") {
  260. $CI =& get_instance();
  261. $CI->load->library('linx');
  262. $CI->linx->delete($type, array(
  263. 'a' => $row_id
  264. ));
  265. }
  266. }
  267. function _merge_tag($arr) {//カンマ区切り
  268. $CI =& get_instance();
  269. $n = 0;
  270. $str = "";
  271. if (count($arr)) {
  272. foreach ($arr as $k=>$v) {
  273. if ($n) $str .= $CI->setting->get('tag_delimiter');
  274. $str .= $v['name'];
  275. $n++;
  276. }
  277. }
  278. return $str;
  279. }
  280. function _get_related($tag = array(), $user_param = array()) {
  281. $CI =& get_instance();
  282. if (!empty($tag) && $CI->setting->get('flg_get_related')) {
  283. $CI->load->helper('array');
  284. $post = array();
  285. $param = array(
  286. 'label' => 'post',
  287. 'datatype' => 'php',
  288. 'content' => '',
  289. 'qty' => 10,
  290. 'total_pie' => 0,
  291. 'tag_qty' => count($tag),
  292. 'id' => 0
  293. );
  294. $param = array_merge($param, $user_param);
  295. $path = 'related/'.$param['label'].'/'.$param['id'].'/';//cache用パスを作成
  296. $dat = $CI->output->get_cache($path);
  297. if (!$dat) {
  298. foreach ($tag as $k => $t) {
  299. if ($param['content'] != "") {
  300. $c = preg_match_all('('.$t['name'].')', $param['content'], $mt);
  301. $tag[$k]['appearance'] = $c;
  302. $param['total_pie'] += ($c + 1);
  303. } else {
  304. $tag[$k]['appearance'] = 0;
  305. $param['total_pie'] = 1;
  306. }
  307. }
  308. foreach ($tag as $k => $t) {
  309. $CI->load->library('post');
  310. $tag[$k]['rate'] = round($t['appearance'] / $param['total_pie'], 4);
  311. $CI->db->select('*, post_id AS id');
  312. $CI->db->join(DB_TBL_LINX, 'linx_a = post_id');
  313. $CI->db->where('linx_b', $t['id']);
  314. $CI->db->where('linx_type', 'post2tag');
  315. $CI->db->where('post_id != '.$param['id']);
  316. $CI->db->where('post_deleted', 0);
  317. $CI->db->where('post_status', 0);
  318. $q = $CI->db->get(DB_TBL_POST, $param['qty']);
  319. #print_r($q->result());
  320. foreach ($q->result() as $pk => $pv) {
  321. if (isset($post[$pv->post_id]['appearance'])) {
  322. $post[$pv->post_id]['appearance'] += preg_match_all('('.$t['name'].')', $pv->post_meta, $mt);
  323. $post[$pv->post_id]['level'] += ($post[$pv->post_id]['appearance'] + 1) * (1 + $tag[$k]['rate']);
  324. } else {
  325. $CI->post->get(array('id' => $pv->post_id, 'qty' => 1, 'label' => 'tmp_related'));
  326. $post[$pv->post_id] = $CI->data->out['tmp_related'][0];
  327. unset($CI->data->out['tmp_related'][0]);
  328. $post[$pv->post_id]['appearance'] = preg_match_all('('.$t['name'].')', $pv->post_meta, $mt);
  329. $post[$pv->post_id]['level'] = ($post[$pv->post_id]['appearance'] + 1) * (1 + $tag[$k]['rate']);
  330. }
  331. }
  332. }
  333. uasort($post, "sort_related");
  334. #exit($param['qty']);
  335. $i = 0;
  336. foreach($post as $p) {
  337. $dat[] = $p;
  338. $i++;
  339. if ($i == $param['qty']) break;
  340. }
  341. $CI->output->set_cache($path, compress_array($post), 120);
  342. } else {
  343. $dat = decompress_array($dat);
  344. }
  345. return $dat;
  346. } else {
  347. return array();
  348. }
  349. }
  350. function delete($id = array()) {
  351. $CI =& get_instance();
  352. if (!empty($id) && is_array($id)) {
  353. $tag_id = $id;
  354. } else {
  355. if ($CI->input->post('id[]')) $tag_id = $CI->input->post('id[]');
  356. if ($CI->input->post('id')) $tag_id[] = $CI->input->post('id');
  357. }
  358. if (is_array($tag_id)) {
  359. foreach ($tag_id as $id) {
  360. $CI->db->where('tag_id', $id);
  361. $CI->db->delete(DB_TBL_TAG);
  362. $CI->linx->unlink('tag', $id);//リンクを削除
  363. }
  364. }
  365. }
  366. function BLX_Tag() {
  367. }
  368. }
  369. function sort_related($a, $b) {
  370. if ($a['level'] < $b['level']) {
  371. return 1;
  372. } elseif ($a['level'] > $b['level']) {
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. ?>