push_api.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * position_api.class.php 推荐至推荐位接口类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-8-3
  8. */
  9. defined('IN_PHPCMS') or exit('No permission resources.');
  10. class push_api {
  11. private $db, $pos_data; //数据调用属性
  12. public function __construct() {
  13. $this->db = pc_base::load_model('position_model'); //加载数据模型
  14. }
  15. /**
  16. * 推荐位推送修改接口
  17. * 适合在文章发布、修改时调用
  18. * @param int $id 推荐文章ID
  19. * @param int $modelid 模型ID
  20. * @param array $posid 推送到的推荐位ID
  21. * @param array $data 推送数据
  22. * @param int $expiration 过期时间设置
  23. * @param int $undel 是否判断推荐位去除情况
  24. * @param string $model 调取的数据模型
  25. * 调用方式
  26. * $push = pc_base::load_app_class('push_api','admin');
  27. * $push->position_update(323, 25, 45, array(20,21), array('title'=>'文章标题','thumb'=>'缩略图路径','inputtime'='时间戳'));
  28. */
  29. public function position_update($id, $modelid, $catid, $posid, $data, $expiration = 0, $undel = 0, $model = 'content_model') {
  30. $arr = $param = array();
  31. $id = intval($id);
  32. if($id == '0') return false;
  33. $modelid = intval($modelid);
  34. $data['inputtime'] = $data['inputtime'] ? $data['inputtime'] : SYS_TIME;
  35. //组装属性参数
  36. $arr['modelid'] = $modelid;
  37. $arr['catid'] = $catid;
  38. $arr['posid'] = $posid;
  39. $arr['dosubmit'] = '1';
  40. //组装数据
  41. $param[0] = $data;
  42. $param[0]['id'] = $id;
  43. if ($undel==0) $pos_info = $this->position_del($catid, $id, $posid);
  44. return $this->position_list($param, $arr, $expiration, $model) ? true : false;
  45. }
  46. /**
  47. * 推荐位删除计算
  48. * Enter description here ...
  49. * @param int $catid 栏目ID
  50. * @param int $id 文章id
  51. * @param array $input_posid 传入推荐位数组
  52. */
  53. private function position_del($catid,$id,$input_posid) {
  54. $array = array();
  55. $pos_data = pc_base::load_model('position_data_model');
  56. //查找已存在推荐位
  57. $r = $pos_data->select(array('id'=>$id,'catid'=>$catid),'posid');
  58. if(!$r) return false;
  59. foreach ($r as $v) $array[] = $v['posid'];
  60. //差集计算,需要删除的推荐
  61. $real_posid = implode(',', array_diff($array,$input_posid));
  62. if (!$real_posid) return false;
  63. $sql = "`catid`='$catid' AND `id`='$id' AND `posid` IN ($real_posid)";
  64. return $pos_data->delete($sql) ? true : false;
  65. }
  66. /**
  67. * 判断文章是否被推荐
  68. * @param $id
  69. * @param $modelid
  70. */
  71. private function content_pos($id, $modelid) {
  72. $id = intval($id);
  73. $modelid = intval($modelid);
  74. if ($id && $modelid) {
  75. $db_data = pc_base::load_model('position_data_model');
  76. $this->db_content = pc_base::load_model('content_model');
  77. $MODEL = getcache('model','commons');
  78. $this->db_content->table_name = $this->db_content->db_tablepre.$MODEL[$modelid]['tablename'];
  79. $posids = $db_data->get_one(array('id'=>$id,'modelid'=>$modelid)) ? 1 : 0;
  80. if ($posids==0) $this->db_content->update(array('posids'=>$posids),array('id'=>$id));
  81. }
  82. return true;
  83. }
  84. /**
  85. * 接口处理方法
  86. * @param array $param 属性 请求时,为模型、栏目数组。提交添加为二维信息数据 。例:array(1=>array('title'=>'多发发送方法', ....))
  87. * @param array $arr 参数 表单数据,只在请求添加时传递。 例:array('modelid'=>1, 'catid'=>12);
  88. * @param int $expiration 过期时间设置
  89. * @param string $model 调取的数据库型名称
  90. */
  91. public function position_list($param = array(), $arr = array(), $expiration = 0, $model = 'content_model') {
  92. if ($arr['dosubmit']) {
  93. if (!$model) {
  94. $model = 'content_model';
  95. } else {
  96. $model = $model;
  97. }
  98. $db = pc_base::load_model($model);
  99. $modelid = intval($arr['modelid']);
  100. $catid = intval($arr['catid']);
  101. $expiration = intval($expiration)>SYS_TIME ? intval($expiration) : 0;
  102. $db->set_model($modelid);
  103. $info = $r = array();
  104. $pos_data = pc_base::load_model('position_data_model');
  105. $position_info = getcache('position', 'commons');
  106. $fulltext_array = getcache('model_field_'.$modelid,'model');
  107. if (is_array($arr['posid']) && !empty($arr['posid']) && is_array($param) && !empty($param)) {
  108. foreach ($arr['posid'] as $pid) {
  109. $ext = $func_char = '';
  110. $r = $this->db->get_one(array('posid'=>$pid), 'extention'); //检查推荐位是否启用了扩展字段
  111. $ext = $r['extention'] ? $r['extention'] : '';
  112. if ($ext) {
  113. $ext = str_replace(array('\'', '"', ' '), '', $ext);
  114. $func_char = strpos($ext, '(');
  115. if ($func_char) {
  116. $func_name = $param_k = $param_arr = '';
  117. $func_name = substr($ext, 0, $func_char);
  118. $param_k = substr($ext, $func_char+1, strrpos($ext, ')')-($func_char+1));
  119. $param_arr = explode(',', $param_k);
  120. }
  121. }
  122. foreach ($param as $d) {
  123. $info['id'] = $info['listorder'] = $d['id'];
  124. $info['catid'] = $catid;
  125. $info['posid'] = $pid;
  126. $info['module'] = $model == 'yp_content_model' ? 'yp' : 'content';
  127. $info['modelid'] = $modelid;
  128. $fields_arr = $fields_value = '';
  129. foreach($fulltext_array AS $key=>$value){
  130. $fields_arr[] = '{'.$key.'}';
  131. $fields_value[] = $d[$key];
  132. if($value['isposition']) {
  133. if ($d[$key]) $info['data'][$key] = $d[$key];
  134. }
  135. }
  136. if ($ext) {
  137. if ($func_name) {
  138. foreach ($param_arr as $k => $v) {
  139. $c_func_name = $c_param = $c_param_arr = $c_func_char = '';
  140. $c_func_char = strpos($v, '(');
  141. if ($c_func_char) {
  142. $c_func_name = substr($v, 0, $c_func_char);
  143. $c_param = substr($v, $c_func_char+1, strrpos($v, ')')-($c_func_char+1));
  144. $c_param_arr = explode(',', $c_param);
  145. $param_arr[$k] = call_user_func_array($c_func_name, $c_param_arr);
  146. } else {
  147. $param_arr[$k] = str_replace($fields_arr, $fields_value, $v);
  148. }
  149. }
  150. $info['extention'] = call_user_func_array($func_name, $param_arr);
  151. } else {
  152. $info['extention'] = $d[$ext];
  153. }
  154. }
  155. //颜色选择为隐藏域 在这里进行取值
  156. $info['data']['style'] = $d['style'];
  157. $info['thumb'] = $info['data']['thumb'] ? 1 : 0;
  158. $info['siteid'] = get_siteid();
  159. $info['data'] = array2string($info['data']);
  160. $info['expiration'] = $expiration;
  161. if ($r = $pos_data->get_one(array('id'=>$d['id'], 'posid'=>$pid, 'catid'=>$info['catid']))) {
  162. if($r['synedit'] == '0') $pos_data->update($info, array('id'=>$d['id'], 'posid'=>$pid, 'catid'=>$info['catid']));
  163. } else {
  164. $pos_data->insert($info);
  165. }
  166. $db->update(array('posids'=>1), array('id'=>$d['id']));
  167. unset($info);
  168. }
  169. $maxnum = $position_info[$pid]['maxnum']+4;
  170. $r = $pos_data->select(array('catid'=>$catid, 'posid'=>$pid), 'id, listorder', $maxnum.',1', 'listorder DESC, id DESC');
  171. if ($r && $position_info[$pid]['maxnum']) {
  172. $listorder = $r[0]['listorder'];
  173. $where = '`catid`='.$catid.' AND `posid`='.$pid.' AND `listorder`<'.$listorder;
  174. $result = $pos_data->select($where, 'id, modelid');
  175. foreach ($result as $r) {
  176. $pos_data->delete(array('id'=>$r['id'], 'posid'=>$pid, 'catid'=>$catid));
  177. $this->content_pos($r['id'], $r['modelid']);
  178. }
  179. }
  180. }
  181. }
  182. return true;
  183. } else {
  184. $infos = $info = array();
  185. $where = '1';
  186. $siteid = get_siteid();
  187. $category = getcache('category_content_'.$siteid,'commons');
  188. $positions = getcache('position', 'commons');
  189. if(!empty($positions)) {
  190. foreach ($positions as $pid => $p) {
  191. //if ($p['catid']) $catids = array_keys((array)subcat($p['catid'], 0, 1));
  192. //获取栏目下所有子栏目
  193. if ($p['catid']) $catids = explode(',',$category[$p['catid']]['arrchildid']);
  194. if (($p['siteid']==0 || $p['siteid']==$siteid) && ($p['modelid']==0 || $p['modelid']==$param['modelid']) && ($p['catid']==0 || in_array($param['catid'], $catids))) {
  195. $info[$pid] = $p['name'];
  196. }
  197. }
  198. return array(
  199. 'posid' => array('name'=>L('position'), 'htmltype'=>'checkbox', 'defaultvalue'=>'', 'data'=>$info, 'validator'=>array('min'=>1)),
  200. );
  201. }
  202. }
  203. }
  204. }
  205. ?>