PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/Servicer_model.php

https://gitlab.com/habracoder/servicer
PHP | 364 lines | 265 code | 49 blank | 50 comment | 34 complexity | e18b5bccf0e4adb8cd5239b0136dc3b9 MD5 | raw file
  1. <?php
  2. class Servicer_model extends CI_Model
  3. {
  4. const TYPE_NEWS = 'news';
  5. const TYPE_GOODS = 'goods';
  6. const TYPE_COMPOSITE = 'composite';
  7. public $title;
  8. public $content;
  9. public $date;
  10. private $params;
  11. private $query;
  12. private $data;
  13. private $count;
  14. private $informer;
  15. private $teasers = [];
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->data = new stdClass();
  20. $this->params = new stdClass();
  21. $this->informer = new stdClass();
  22. $this->initUri()
  23. ->initQuery()
  24. ->initInformer()
  25. ->initTeasers();
  26. }
  27. public function getScript()
  28. {
  29. $informer = $this->getInformer();
  30. $tpl = "var _mgq = _mgq || [];
  31. _mgq.push([':method', [\n:teasers\n],{
  32. awc:{},
  33. dt:'desktop'
  34. }]);
  35. :rejectMethod_mgqp();";
  36. $teasersString = '';
  37. foreach ($this->getTeasers() as $teaser) {
  38. if ($this->getType() == self::TYPE_GOODS) {
  39. $teasersString .= sprintf("['%s',{i:'" . $this->initImage($teaser->image_link) . "'}],\n", implode("', '", [
  40. $teaser->url,
  41. $teaser->id,
  42. '1',
  43. $teaser->title,
  44. isset($teaser->advert_text) ? $teaser->advert_text : '',
  45. '1',
  46. '',
  47. isset($teaser->price) ? $this->formatPrice($teaser->price) : '',
  48. isset($teaser->price_old) ? $this->formatPrice($teaser->price_old) : '',
  49. 'f3z9iJP_G1SN85k2BaTDHGNiBXjhSO6ySZmNok-j6Kxn4TfESCMw3oaTTmnC67za' . rand(10000, 99999),
  50. ]));
  51. } else {
  52. $teasersString .= sprintf(
  53. "['%s',{i:'" . $this->initImage($teaser->image_link) . "'}],\n", implode("', '",
  54. [
  55. $teaser->url,
  56. $teaser->id,
  57. '1',
  58. $teaser->title,
  59. isset($teaser->advert_text) ? $teaser->advert_text : '',
  60. '',
  61. 'f3z9iJP_G1SN85k2BaTDHGNiBXjhSO6ySZmNok-j6Kxn4TfESCMw3oaTTmnC67za' . rand(10000, 99999),
  62. ]
  63. ));
  64. }
  65. }
  66. $teasersString = trim($teasersString, ",\n");
  67. $result = strtr($tpl, [
  68. ':teasers' => $teasersString,
  69. ':method' => $this->getInformerMethod(),
  70. ':main_informer_id' => $informer->parent ? $informer->parent : $informer->uid,
  71. ':rejectMethod' => $this->getRejectMethod()
  72. ]);
  73. return $result;
  74. }
  75. private function getRejectMethod()
  76. {
  77. if ($this->getInformer()->class == 'composite') {
  78. $ident = $this->getInformer()->parent;
  79. $typePrefix = 'C';
  80. } else {
  81. $ident = $this->getInformer()->uid;
  82. $typePrefix = strtoupper(substr($this->getType(), 0, 1));
  83. }
  84. if ($this->settings->get('rejectTeaser')) {
  85. return sprintf(
  86. "_mgq.push(['MarketGid%sReject%s']);\n",
  87. $typePrefix,
  88. $ident
  89. );
  90. }
  91. return '';
  92. }
  93. /**
  94. * Возвращает метод ответа с тизерами
  95. * @return string
  96. */
  97. private function getInformerMethod()
  98. {
  99. $domain = 'marketgid';
  100. preg_match('/(tovarro|lentainform|mgid)/', $_SERVER['HTTP_HOST'], $match);
  101. if (isset($match[1])) {
  102. $domain = $match[1];
  103. }
  104. switch ($domain) {
  105. case 'tovarro':
  106. $prefix = 'Tovarro';
  107. break;
  108. default:
  109. $prefix = 'MarketGid';
  110. break;
  111. }
  112. if ($this->getInformer()->class == self::TYPE_COMPOSITE && (int) $this->getInformer()->parent > 0) {
  113. $dayOfWeek = date('N') > 5 ? 'holyday' : 'weekday' ;
  114. $result = $this->db->select('type')->from('tickers_composite_default')
  115. ->where([
  116. 'composite' => (int) $this->getInformer()->parent,
  117. 'day_of_week' => $dayOfWeek
  118. ])->get()->row();
  119. if ($result && $result->type != $this->getType()) {
  120. return $prefix . 'RedirectComposite' . $this->getInformer()->parent;
  121. }
  122. }
  123. switch ($this->getType()) {
  124. case self::TYPE_GOODS:
  125. $method = $prefix . 'LoadGoods' . $this->getInformer()->id;
  126. break;
  127. case self::TYPE_NEWS:
  128. $method = $prefix . 'LoadNews' . $this->getInformer()->id;
  129. break;
  130. default:
  131. $method = $prefix . 'RedirectComposite' . $this->getInformer()->parent;
  132. break;
  133. }
  134. return $method;
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getTickerDefault()
  140. {
  141. $type = $this->getType();
  142. $informer = $this->getInformer();
  143. if ($informer->class == self::TYPE_COMPOSITE && (int) $informer->parent > 0) {
  144. $dayOfWeek = date('N') > 5 ? 'holyday' : 'weekday' ;
  145. $result = $this->db->select('type')->from('tickers_composite_default')
  146. ->where([
  147. 'composite' => (int) $informer->parent,
  148. 'day_of_week' => $dayOfWeek
  149. ])->get()->row();
  150. if ($result) {
  151. $type = $result->type;
  152. }
  153. }
  154. return $type;
  155. }
  156. public function formatPrice($price)
  157. {
  158. $lang = 'ru';
  159. if ($lang == 'ru' && $price) {
  160. $price = ($price / 25) . ' грн.';
  161. } else {
  162. $price = ' ';
  163. }
  164. return $price;
  165. }
  166. /**
  167. * @param $src
  168. * @return string
  169. */
  170. private function initImage($src)
  171. {
  172. if ($this->settings->get('replaceImages', false)) {
  173. $imageSettings = (array)$this->getData()->mcimage;
  174. $width = $imageSettings['size-width'];
  175. $height = $imageSettings['size-height'];
  176. $src = 'https://imgg.marketgid.net' . '/image/' . rand(1000, 9999) .
  177. '/' . $width . 'x' . $height . '.jpg';
  178. }
  179. return $src;
  180. }
  181. /**
  182. * @return int
  183. */
  184. public function getUid()
  185. {
  186. return (int) $this->params->uid;
  187. }
  188. /**
  189. * @return int
  190. */
  191. public function getId()
  192. {
  193. return (int) $this->params->id;
  194. }
  195. /**
  196. * @return string
  197. */
  198. public function getType()
  199. {
  200. return $this->params->type;
  201. }
  202. /**
  203. * @return int
  204. */
  205. public function getPage()
  206. {
  207. return (int) $this->params->page;
  208. }
  209. /**
  210. * @return $this
  211. */
  212. public function initInformer()
  213. {
  214. $table = $this->getType() == self::TYPE_GOODS ? 'g_blocks' : 'tickers';
  215. $this->informer = $this->db
  216. ->select('*')
  217. ->where('uid', $this->getUid())
  218. ->from($table)
  219. ->get()
  220. ->row();
  221. $this->initConstructorTemplate($this->informer->constructor_template);
  222. return $this;
  223. }
  224. private function initTeasers()
  225. {
  226. $table = $this->getType() == self::TYPE_GOODS ? 'g_hits_1' : 'news_1';
  227. $where = ['droped = 0'];
  228. if ($this->getType() == self::TYPE_GOODS) {
  229. $where[] = 'draft = 0';
  230. }
  231. $query = $this->db->query(strtr(
  232. "select * from :table where :where order by rand() limit :limit",
  233. [
  234. ':table' => $table,
  235. ':where' => implode(' and ', $where),
  236. ':limit' => $this->getCount()
  237. ]
  238. ));
  239. $this->teasers = $query->result();
  240. }
  241. /**
  242. * @param $json
  243. * @return $this
  244. */
  245. private function initConstructorTemplate($json)
  246. {
  247. $data = json_decode($json);
  248. $this->data = (object)$data;
  249. $this->count = (int)$this->data->rows * (int)$this->data->cols;
  250. return $this;
  251. }
  252. /**
  253. * @return int
  254. */
  255. public function getCount()
  256. {
  257. return $this->count;
  258. }
  259. /**
  260. * @return $this
  261. */
  262. private function initUri()
  263. {
  264. preg_match('/(fs\/)?u([0-9]+)\/([0-9]+)\/([0-9]+)/', $_SERVER['REQUEST_URI'], $match);
  265. $this->params->fs = (boolean) $match[1];
  266. $this->params->uid = (int)$match[2];
  267. $this->params->id = (int)$match[3];
  268. $this->params->page = (int)$match[4];
  269. preg_match('/[a-z]{2}\-([a-z]{1})/', $_SERVER['HTTP_HOST'], $typeMatch);
  270. $this->params->type = $typeMatch[1] == 'g' ? 'goods' : 'news';
  271. if (isset($_COOKIE['mg_type'])) {
  272. $this->params->type = $_COOKIE['mg_type'];
  273. }
  274. if ($this->settings->get('teasersType', self::TYPE_GOODS) != $this->getType()) {
  275. $type = $this->settings->get('teasersType', self::TYPE_GOODS);
  276. }
  277. return $this;
  278. }
  279. /**
  280. * @return array
  281. */
  282. public function getTeasers()
  283. {
  284. return $this->teasers;
  285. }
  286. /**
  287. * @return $this
  288. */
  289. private function initQuery()
  290. {
  291. parse_str($_SERVER['QUERY_STRING'], $params);
  292. $this->query = (object) $params;
  293. return $this;
  294. }
  295. /**
  296. * @param string $name
  297. * @param mixed $default
  298. * @return mixed
  299. */
  300. public function getParam($name, $default = null)
  301. {
  302. return isset($this->query->$name) ? $this->query->$name : $default;
  303. }
  304. /**
  305. * @return stdClass
  306. */
  307. public function getInformer()
  308. {
  309. return $this->informer;
  310. }
  311. public function getData()
  312. {
  313. return $this->data;
  314. }
  315. }