PageRenderTime 44ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/service/forum/srv/post/do/PwPostDoPoll.php

https://gitlab.com/wuhang2003/phpwind
PHP | 329 lines | 215 code | 62 blank | 52 comment | 41 complexity | 0d97a40774448ad200853362d63c4ae5 MD5 | raw file
  1. <?php
  2. defined('WEKIT_VERSION') || exit('Forbidden');
  3. Wind::import('SRV:forum.srv.post.do.PwPostDoBase');
  4. /**
  5. * 帖子发布-投票帖 相关服务
  6. *
  7. * @author MingXing Sun <mingxing.sun@aliyun-inc.com>
  8. * @copyright ©2003-2103 phpwind.com
  9. * @license http://www.phpwind.com
  10. * @version $Id: PwPostDoPoll.php 23975 2013-01-17 10:20:11Z jieyin $
  11. * @package forum
  12. */
  13. class PwPostDoPoll extends PwPostDoBase {
  14. public $tid = null;
  15. public $poll = array();
  16. public $action;
  17. public $post_max_size;
  18. public $user = null;
  19. public $info = array();
  20. public function __construct(PwPost $pwpost, $tid = null, $poll = array()) {
  21. $this->user = $pwpost->user;
  22. $this->tid = $tid ? $tid : null;
  23. $this->poll = $poll;
  24. $this->action = $this->tid ? 'modify' : 'add';
  25. $this->post_max_size = @ini_get('post_max_size');
  26. $this->max_file_uploads = @ini_get('max_file_uploads');
  27. }
  28. /**
  29. * 添加投票与投票关系内容
  30. */
  31. public function addThread($tid) {
  32. return $this->addPoll($tid);
  33. }
  34. /**
  35. * 更新投票内容
  36. */
  37. public function updateThread($tid) {
  38. return $this->updatePoll($tid);
  39. }
  40. /**
  41. * 设置检查
  42. */
  43. public function check($postDm) {
  44. if ($this->action == 'add' && !$this->user->getPermission('allow_add_vote')) {
  45. return new PwError('VOTE:group.permission.add', array('{grouptitle}' => $this->user->getGroupInfo('name')));
  46. }
  47. if (($result = $this->_checkPoll()) !== true) {
  48. return $result;
  49. }
  50. return true;
  51. }
  52. public function addPoll($tid) {
  53. if (($attachInfo = $this->uploadOptionImage()) instanceof PwError) return $attachInfo;
  54. $pollData = $this->poll['poll'];
  55. $optionData = $this->poll['option'];
  56. Wind::import('SRV:poll.dm.PwPollDm');
  57. $pollDm = new PwPollDm(); /* @var $pwPollDm PwPollDm */
  58. $pollDm->setIsViewResult($pollData['isviewresult']);
  59. $pollDm->setOptionLimit($pollData['optionlimit']);
  60. $pollDm->setCreatedUserid($this->user->uid);
  61. $pollData['regtimelimit'] && $pollDm->setRegtimeLimit(pw::str2time($pollData['regtimelimit']));
  62. $expiredTime = $pollData['expiredday'] ? intval($pollData['expiredday']) * 86400 + pw::getTime() : 0;
  63. $pollDm->setExpiredTime($expiredTime);
  64. $optinNum = $pollData['ismultiple'] ? count($optionData) : 0;
  65. $pollDm->setOptionLimit(min($optinNum, $pollData['optionlimit']));
  66. $attachInfo && $pollDm->setIsIncludeImg(1);
  67. $newPollid = $this->_getPollDS()->addPoll($pollDm);
  68. Wind::import('SRV:poll.dm.PwPollOptionDm');
  69. foreach ($optionData as $key => $value) {
  70. if (!$value) continue;
  71. $dm = new PwPollOptionDm(); /* @var $pwPollDm PwPollDm */
  72. $image = isset($attachInfo['optionpic'][$key]) ? $attachInfo['optionpic'][$key]['path'] : '';
  73. $dm->setContent($value)->setPollid($newPollid)->setImage($image);
  74. $this->_getPollOptionDS()->add($dm);
  75. }
  76. Wind::import('SRV:poll.dm.PwThreadPollDm');
  77. $threadPollDm = new PwThreadPollDm(); /* @var $threadPollDm PwThreadPollDm */
  78. $threadPollDm->setTid($tid)->setPollid($newPollid)->setCreatedUserid($this->user->uid);
  79. $this->_getThreadPollDS()->addPoll($threadPollDm);
  80. $this->_afterUpdate($newPollid);
  81. return true;
  82. }
  83. public function updatePoll($tid) {
  84. $this->info = $this->getThreadPollBo()->info;
  85. if ($this->info['poll']['voter_num']) return true;
  86. if (($attachInfo = $this->uploadOptionImage()) instanceof PwError) return $attachInfo;
  87. $pollData = $this->poll['poll'];
  88. Wind::import('SRV:poll.dm.PwPollDm');
  89. $pollDm = new PwPollDm($this->info['poll_id']); /* @var $pwPollDm PwPollDm */
  90. $pollDm->setIsViewResult($pollData['isviewresult']);
  91. $pollDm->setOptionLimit($pollData['optionlimit']);
  92. $pollDm->setRegtimeLimit($pollData['regtimelimit'] ? pw::str2time($pollData['regtimelimit']) : 0);
  93. $expiredTime = $pollData['expiredday'] ? intval($pollData['expiredday']) * 86400 + $this->info['poll']['created_time'] : 0;
  94. $pollDm->setExpiredTime($expiredTime);
  95. $optinNum = $pollData['ismultiple'] ? $this->info['poll']['optionnum'] + count($this->poll['newoption']) : 0;
  96. $pollDm->setOptionLimit(min($optinNum, $pollData['optionlimit']));
  97. $attachInfo && $pollDm->setIsIncludeImg(1);
  98. $this->_getPollDS()->updatePoll($pollDm);
  99. $this->_updatePollOption($attachInfo);
  100. return true;
  101. }
  102. private function _updatePollOption($attachInfo) {
  103. $optionInfo = $this->info['option'];
  104. $optionData = $this->poll['option'];
  105. Wind::import('SRV:poll.dm.PwPollOptionDm');
  106. $deleteIds = array();
  107. foreach (array_keys($optionInfo) as $_id) {
  108. $attach = isset($attachInfo['optionpic'][$_id]) ? $attachInfo['optionpic'][$_id] : '';
  109. $optionContent = trim($optionData[$_id]);
  110. $isUpdate = ($optionInfo[$_id]['content'] != $optionContent || $attach) ? true : false;
  111. !$optionContent && $deleteIds[] = $_id;
  112. if (!($isUpdate && $optionContent)) continue;
  113. $dm = new PwPollOptionDm($_id); /* @var $pwPollDm PwPollDm */
  114. $optionInfo[$_id]['content'] != $optionData[$_id] && $dm->setContent($optionData[$_id]);
  115. if ($attach) {
  116. $dm->setImage($attach['path']);
  117. $optionImgPath = $optionInfo[$_id]['image'];
  118. $optionImgPath && $this->_getPollService()->removeImg($optionImgPath);
  119. }
  120. $this->_getPollOptionDS()->update($dm);
  121. }
  122. if ($deleteIds) {
  123. $this->_getPollOptionDS()->batchDelete($deleteIds);
  124. }
  125. foreach ((array)$this->poll['newoption'] as $key=>$value) {
  126. if (!$value) continue;
  127. $dm = new PwPollOptionDm(); /* @var $pwPollDm PwPollDm */
  128. $image = isset($attachInfo['newoptionpic'][$key]) ? $attachInfo['newoptionpic'][$key]['path'] : '';
  129. $dm->setContent($value)->setPollid($this->info['poll_id'])->setImage($image);
  130. $this->_getPollOptionDS()->add($dm);
  131. }
  132. $this->_afterUpdate($this->info['poll_id']);
  133. return true;
  134. }
  135. private function _afterUpdate($pollid) {
  136. $optionList = $this->_getPollOptionDS()->getByPollid($pollid);
  137. if (!$optionList) return false;
  138. $flag = false;
  139. foreach ($optionList as $value) {
  140. if (!$value['image']) continue;
  141. $flag = true;
  142. }
  143. Wind::import('SRV:poll.dm.PwPollDm');
  144. $dm = new PwPollDm($pollid);
  145. $dm->setIsIncludeImg($flag ? 1 : 0);
  146. $this->_getPollDs()->updatePoll($dm);
  147. return true;
  148. }
  149. /**
  150. * 上次投票项图片
  151. *
  152. */
  153. public function uploadOptionImage() {
  154. Wind::import('SRV:upload.action.PwPollUpload');
  155. Wind::import('LIB:upload.PwUpload');
  156. $bhv = new PwPollUpload($this->user);
  157. $upload = new PwUpload($bhv);
  158. if (($result = $upload->check()) === true) {
  159. $result = $upload->execute();
  160. }
  161. if ($result !== true) {
  162. return $result == false ? new PwError('operate.fail') : $result;
  163. }
  164. return $bhv->getAttachInfo();
  165. }
  166. /**
  167. * 投票验证
  168. *
  169. * @return bool
  170. */
  171. private function _checkPoll() {
  172. switch ($this->action) {
  173. case 'modify' :
  174. return $this->_checkInModify();
  175. break;
  176. case 'add':
  177. return $this->_checkInAdd();
  178. break;
  179. }
  180. return true;
  181. }
  182. private function _checkInModify() {
  183. $this->info = $this->getThreadPollBo()->info;
  184. if (!$this->info) return new PwError('VOTE:thread.not.exist');
  185. if ($this->info['poll']['voter_num']) return true;
  186. $option = array_merge($this->poll['option'], $this->poll['newoption']);
  187. $reulst = array();
  188. foreach ($option as $value) {
  189. $value = trim($value);
  190. if (!$value) continue;
  191. $reulst[] = $value;
  192. }
  193. $optionNum = count($reulst);
  194. if ($optionNum < 2) return new PwError('VOTE:options.illegal');
  195. if ($optionNum != count(array_unique($reulst))) {
  196. return new PwError('VOTE:options.repeat');
  197. }
  198. return true;
  199. }
  200. private function _checkInAdd() {
  201. $option = $this->poll['option'];
  202. $reulst = array();
  203. foreach ($option as $value) {
  204. $value = trim($value);
  205. if (!$value) continue;
  206. $reulst[] = $value;
  207. }
  208. $optionNum = count($reulst);
  209. if ($optionNum < 2) return new PwError('VOTE:options.illegal');
  210. if ($optionNum != count(array_unique($reulst))) {
  211. return new PwError('VOTE:options.repeat');
  212. }
  213. return true;
  214. }
  215. public function createHtmlBeforeContent() {
  216. PwHook::template('displayPostPollHtml', 'TPL:bbs.post_poll', true, $this);
  217. }
  218. public function dataProcessing($postDm) {
  219. $postDm->setSpecial('poll');
  220. return $postDm;
  221. }
  222. /**
  223. * get PwThreadPollBo
  224. *
  225. * @return PwThreadPollBo
  226. */
  227. public function getThreadPollBo() {
  228. static $_instance = null;
  229. if ($_instance == null) {
  230. Wind::import('SRV:poll.bo.PwThreadPollBo');
  231. $_instance = new PwThreadPollBo($this->tid);
  232. }
  233. return $_instance;
  234. }
  235. /**
  236. * get PwPollService
  237. *
  238. * @return PwPollService
  239. */
  240. private function _getPollService() {
  241. return Wekit::load('poll.srv.PwPollService');
  242. }
  243. /**
  244. * get PwPoll
  245. *
  246. * @return PwPoll
  247. */
  248. private function _getPollDS() {
  249. return Wekit::load('poll.PwPoll');
  250. }
  251. /**
  252. * get PwPollOption
  253. *
  254. * @return PwPollOption
  255. */
  256. private function _getPollOptionDS() {
  257. return Wekit::load('poll.PwPollOption');
  258. }
  259. /**
  260. * get PwThreadPoll
  261. *
  262. * @return PwThreadPoll
  263. */
  264. private function _getThreadPollDS() {
  265. return Wekit::load('poll.PwThreadPoll');
  266. }
  267. }