Slide.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * User: charlie
  4. * 首页推荐的幻灯片
  5. */
  6. class Slide extends MongoAr
  7. {
  8. public $_id; //帖子的object id
  9. public $title; // 标题
  10. public $pic=""; //图片的七牛地址
  11. public $type='topic'; // 链接类型 'topic' 'group' 'url' 'subject'
  12. public $obj; //链接的对象的objectid
  13. public $data=array();//补充数据
  14. public $status=1;//状态 1正常 0删除
  15. public $order;//排序权重
  16. public $city_info=array(); //省份
  17. public $start_time;//轮播图上线时间戳
  18. public $end_time;//轮播图结束时间戳
  19. public function __construct($scenario='insert'){
  20. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_app'));
  21. parent::__construct($scenario);
  22. }
  23. public static $status_option = array(
  24. 1 => array('name' => '正常'),
  25. 0 => array('name' => '删除')
  26. );
  27. public static function model($className=__CLASS__)
  28. {
  29. return parent::model($className);
  30. }
  31. public static function get($_id) {
  32. if(CommonFn::isMongoId($_id)){
  33. $criteria = new EMongoCriteria();
  34. $criteria->_id('==', $_id);
  35. $model = self::model()->find($criteria);
  36. return $model;
  37. }else{
  38. return false;
  39. }
  40. }
  41. public static function getIndexSlide($province){
  42. $cache = new ARedisCache();
  43. $key = 'data_cache_'.__CLASS__.md5($province);
  44. $data_cache = $cache->get($key);
  45. $res = array();
  46. if($data_cache){
  47. $res = array_values(unserialize($data_cache));
  48. }else{
  49. $criteria = new EMongoCriteria();
  50. $criteria->status('==', 1);
  51. $criteria->end_time('>=', time());
  52. $criteria->sort('order', EMongoCriteria::SORT_DESC);
  53. $cursor = Slide::model()->findAll($criteria);
  54. $rows = CommonFn::getRows($cursor);
  55. foreach ($rows as $key => $value) {
  56. if($province != 'no' && isset($value['city_info'])&&isset($value['city_info']['province'])&&$value['city_info']['province']!=$province&&$value['city_info']['province']!=''){
  57. unset($rows[$key]);
  58. }
  59. }
  60. $res = Slide::model()->parse($rows);
  61. $res = array_values($res);
  62. $cache->set($key,serialize($res),1800);
  63. }
  64. return $res;
  65. }
  66. public function getCollectionName()
  67. {
  68. return 'slide';
  69. }
  70. public function parseRow($row,$output=array()){
  71. $newRow = array();
  72. $newRow['id'] = (string)$row['_id'];
  73. $newRow['status'] = CommonFn::get_val_if_isset($row,'status',1);
  74. $newRow['title'] = CommonFn::get_val_if_isset($row,'title','');
  75. $newRow['pic'] = CommonFn::get_val_if_isset($row,'pic','');
  76. $newRow['type'] = CommonFn::get_val_if_isset($row,'type','topic');
  77. $newRow['city_info'] = CommonFn::get_val_if_isset($row,'city_info',array("city"=>"","area"=>"","province"=>""));
  78. $newRow['start_time'] = CommonFn::get_val_if_isset($row,'start_time','');
  79. $newRow['end_time'] = CommonFn::get_val_if_isset($row,'end_time','');
  80. $newRow['data'] = CommonFn::get_val_if_isset($row,'data',array());
  81. $newRow['order'] = CommonFn::get_val_if_isset($row,'order',1);
  82. if($newRow['type']=='topic'){
  83. $type = 'ZTopic';
  84. $model = 'Topic';
  85. }elseif($newRow['type']=='group'){
  86. $type = 'ZGroup';
  87. $model = 'Group';
  88. }
  89. if(isset($type) && isset($model) && !empty($model) && !empty($type)){
  90. $z_type = new $type;
  91. $obj = $z_type->idExist($row['obj']);
  92. $newRow['obj'] = $model::model()->parseRow($obj);
  93. }elseif(isset($newRow['type']) && $newRow['type'] == 'url'){
  94. $newRow['obj'] = $row['obj'];
  95. // 暂时不上线 2015-12-24
  96. if(empty($newRow['obj']['url'])){
  97. $newRow['obj']['url'] = 'http://www.yiguanjia.me';
  98. $newRow['obj']['id'] = 'http://www.yiguanjia.me';
  99. } else {
  100. $newRow['obj']['id'] = $newRow['obj']['url'];
  101. }
  102. }elseif(isset($newRow['type']) && $newRow['type'] == 'subject') {
  103. $_subject = Subject::get($row['obj']);
  104. $newRow['obj'] = $_subject->parseRow($_subject->attributes);
  105. }else{
  106. $newRow['obj'] = (object)array();
  107. }
  108. if(APPLICATION=='api'||APPLICATION=='common'){
  109. unset($newRow['action_user']);
  110. unset($newRow['action_time']);
  111. unset($newRow['action_log']);
  112. unset($newRow['order']);
  113. unset($newRow['id']);
  114. unset($newRow['status']);
  115. }
  116. return $this->output($newRow,$output);
  117. }
  118. }