Advertisement
Guest User

Untitled

a guest
May 28th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. <?php
  2.  
  3. class Default_AppsController extends Vrench_Controller_Action_Default
  4. {
  5. use Vrench_Trait_Apps;
  6.  
  7. public function appAction()
  8. {
  9. var_dump([]);die();
  10. $this->viewOptions['cart'] = [
  11. 'button' => 'ui small green icon labeled button',
  12. 'icon' => 'icon shopping cart',
  13. 'html' => 'Added to cart'
  14. ];
  15. $req = $this->getRequest();
  16.  
  17. $id = (int) $req->getParam('id');
  18.  
  19. $db = Zend_Db_Table::getDefaultAdapter();
  20.  
  21. $last_version_select = $db->select()
  22. ->from('app_releases',[new Zend_Db_Expr('MAX(version) as latestversion')])
  23. ->where('app_id = ?', $id);
  24. # ->where('status = ?', Default_Model_Release::STATUS_ACCEPTED);
  25. #$last_version = $db->fetchRow($last_version_select);
  26. #$last_version = $last_version['latestversion'];
  27. $select = $db->select()
  28. ->distinct()
  29. ->from(['a' => 'apps'], ['id' => 'app_id', 'type', 'user_id', 'name', 'data', 'copyright', 'legal', 'setup', 'additional_notes', 'release_time', 'website', 'adult_content', 'cont_keyboard', 'cont_mouse', 'cont_xbox', 'cont_pad'])
  30. ->joinInner(['r' => 'app_releases'], 'a.app_id = r.app_id', ['open_video', 'rating', 'comfort'])
  31. ->joinLeft(['c' => 'cart'], 'c.id = a.app_id AND c.is_bundle = 0 and c.user_id = ' . (int) $this->user->user_id, null)
  32. ->joinLeft(['p' => 'purchases'], 'p.app_id = a.app_id AND p.user_id = ' . (int) $this->user->user_id, null)
  33. ->joinLeft(['d' => 'app_discounts'], 'd.app_id = a.app_id AND (NOW() BETWEEN FROM_UNIXTIME(d.create_time) AND FROM_UNIXTIME(d.end_time))', ['discount_start' => 'create_time', 'discount_end' => 'end_time'])
  34. ->columns([
  35. 'in_cart' => 'IF(c.is_bundle IS NULL, 0, 1)',
  36. 'purchased' => 'IF(p.create_time IS NULL, 0, 1)',
  37. 'price' => 'IFNULL(d.price, a.price)',
  38. 'discount' => 'IF(d.price IS NULL, NULL, 100 - 100 * d.price / a.price)',
  39. 'old_price' => 'IF(d.price IS NULL, NULL, a.price)',
  40. ])
  41. # ->where('r.version = ?', $last_version)
  42. ->where('a.app_id = ?', $id)
  43. ->where('a.status = ' . Default_Model_App::STATUS_PUBLISHED . ' OR a.status = ' . Default_Model_App::STATUS_PUBLISHED_REMOVED);
  44. $app = $db->fetchRow($select);
  45.  
  46. if ($app === false) {
  47. throw new Zend_Controller_Action_Exception('Invalid parameters', 404);
  48. }
  49.  
  50. // Ask user to sign in or sign up if he want's to access Adult Content section
  51. // if bot allow scraping
  52. if ($app['adult_content'] && !$this->view->user_id && !$this->ua->isBot()) {
  53. $this->redirect($this->view->url(['controller' => 'apps', 'action' => 'adult-content'], 'default', true));
  54. }
  55.  
  56. // log view record
  57. $db->query("INSERT INTO stats_app_views (date, app_id, views) VALUES ('" . date("Y-m-d") . "','" . (int) $id . "', 1) ON DUPLICATE KEY UPDATE views = views + 1;");
  58.  
  59. // Fetch media
  60. $select = $db->select()
  61. ->from('app_media', ['type', 'source', 'thumbnail'])
  62. ->where('app_id = ?', $id)
  63. ->where('type != ?', Default_Model_Media::TYPE_PROCESSING)
  64. ->order('type DESC');
  65.  
  66. $media = $db->fetchAll($select);
  67.  
  68. // Get cover and unset app cover
  69. foreach ($media as $k => $m) {
  70. if ($m['type'] == Default_Model_Media::TYPE_COVER) {
  71. $cover = $m;
  72. unset($media[$k]);
  73. }
  74. if ($m['type'] == Default_Model_Media::TYPE_VRENCH_APP_COVER)
  75. unset($media[$k]);
  76. }
  77. $media = array_values($media);
  78.  
  79. // Get current user locale
  80. $lang = Zend_Registry::get('Zend_Translate')->getLocale();
  81.  
  82. // Description check
  83. $select = $db->select()
  84. ->from('app_descriptions', ['count' => 'COUNT(*)'])
  85. ->where('app_id = ?', $id)
  86. ->where('language = ?', $lang);
  87.  
  88. $noLocale = !$db->fetchRow($select)['count'];
  89.  
  90. if ($noLocale) {
  91. $lang = 'en';
  92. }
  93.  
  94. // Fetch description
  95. $select = $db->select()
  96. ->from('app_descriptions', ['description'])
  97. ->where('app_id = ?', $id)
  98. ->where('language = ?', $lang);
  99.  
  100. // Assing description to app
  101. $app['description'] = $db->fetchRow($select)['description'];
  102.  
  103. // Platforms select query
  104. $select = $db->select()
  105. ->from(['r' => 'app_releases'], ['platform_id'])
  106. ->joinInner(['p' => 'platforms'], 'r.platform_id = p.platform_id', ['platform_name'])
  107. ->where('r.status = ?', Default_Model_Release::STATUS_ACCEPTED)
  108. ->where('r.app_id = ?', $id)
  109. ->group('r.platform_id');
  110.  
  111. // Platforms array
  112. $platforms = [];
  113.  
  114. // List all platforms
  115. foreach ($db->fetchAll($select) as $row) {
  116. $platforms[$row['platform_id']] = $row['platform_name'];
  117. }
  118.  
  119. // Fetch platform requirements
  120. $select = $db->select()
  121. ->from('app_platform_requirements', ['platform_id', 'type', 'os', 'disk_space', 'memory', 'processor', 'graphics', 'notes'])
  122. ->where('platform_id in (?)', array_keys($platforms))
  123. ->where('app_id = ?', $id);
  124.  
  125. // Requirements array
  126. $requirements = [];
  127.  
  128. // Parse requirements
  129. foreach ($db->fetchAll($select) as $row) {
  130. $p = $row['platform_id'];
  131. $t = $row['type'];
  132.  
  133. // Unset unnecessary values
  134. unset($row['platform_id'], $row['type']);
  135.  
  136. // Check if requirements platform array exists
  137. if (!isset($requirements[$p])) {
  138. $requirements[$p] = [];
  139. }
  140.  
  141. // Set rest of the row as requirements value
  142. $requirements[$p][$t] = $row;
  143. }
  144. // check if user ever launched this app
  145. $select = $db->select()
  146. ->from('stats_launches')
  147. #->where('user_id = ?', $this->user->user_id)
  148. ->where('user_id = ?', 8)
  149. ->where('app_id = ?', $id)
  150. ->where('version = ?', $last_version);
  151.  
  152. $this->view->allowRating = $db->fetchRow($select);
  153. $this->view->app = (object) $app;
  154. $this->view->media = $media;
  155. $this->view->cover = $cover;
  156. $this->view->requirements = $requirements;
  157. $this->view->platforms = $platforms;
  158. }
  159.  
  160. public function adultContentAction()
  161. {
  162. if (!$this->user->user_id)
  163. $this->view->signUpNow = true;
  164.  
  165. $db = Zend_Db_Table::getDefaultAdapter();
  166.  
  167. $baseSelect = $this->getAppsQuery()->limit(16)->where("adult_content = 1");
  168.  
  169. // Get free games
  170. $select = clone $baseSelect;
  171. $this->view->free = $db->fetchAll($select->where('a.price = 0'));
  172.  
  173. $select = clone $baseSelect;
  174. $this->view->discounted = $db->fetchAll($select->having('old_price IS NOT NULL'));
  175.  
  176. $select = clone $baseSelect;
  177. $this->view->games = $db->fetchAll($select->having('old_price IS NULL')->where('a.price != 0'));
  178. }
  179.  
  180. public function rateAction()
  181. {
  182. if($this->getRequest()->isXmlHttpRequest())
  183. {
  184. $db = Zend_Db_Table::getDefaultAdapter();
  185. $data = array(
  186. 'app_id' => $this->getRequest()->getParam('id'),
  187. 'version' => '1',
  188. 'user_id' => $this->user->user_id,
  189. 'date' => date('Y-m-d H:i:s'),
  190. 'rate' => $this->getRequest()->getPost('rate')
  191. );
  192.  
  193.  
  194. $select = $db->select()
  195. ->from('app_ratings')
  196. ->where('app_id = ?', $this->getRequest()->getParam('id'))
  197. ->where('app_id = ?', 1)
  198.  
  199. $db->insert('app_ratings', $data);
  200. $id = $this->getRequest()->getParam('id');
  201. }
  202. dme('not ok'.$id);die();
  203. }
  204.  
  205. public function searchAction()
  206. {
  207. $db = Zend_Db_Table::getDefaultAdapter();
  208.  
  209. $form = new Default_Form_Search();
  210.  
  211. $rangeSelect = $db->select()
  212. ->from(['a' => 'apps'], null)
  213. ->joinLeft(['d' => 'app_discounts'], 'd.app_id = a.app_id AND (UNIX_TIMESTAMP() BETWEEN d.create_time AND d.end_time)', null)
  214. ->columns(['price' => 'IFNULL(d.price, a.price)']);
  215.  
  216. // Fetch price range
  217. $range = $db->fetchAll($db->select()->from($rangeSelect, null)->columns(['min' => 'FLOOR(MIN(price))', 'max' => 'CEIL(MAX(price))']));
  218. $range = isset($range[0]) ? $range[0] : null;
  219.  
  220. if ($range === null) {
  221. $form->removeSubForm('Price');
  222. } else {
  223. if ($range['min'] == $range['max']) {
  224. $range['max'] += 1;
  225. }
  226.  
  227. // Set default values for price
  228. $priceForm = $form->getSubForm('Price');
  229. $priceForm->getElement('min')->setValue(0);
  230. $priceForm->getElement('max')->setValue($range['max']);
  231. }
  232.  
  233. // Populate values from $_GET
  234. $form->populate($_GET);
  235.  
  236. // Fetch $_GET keys and form keys
  237. $getKeys = array_keys($_GET);
  238. $formKeys = array_keys($form->getElements());
  239.  
  240. $result = null;
  241. $select = null;
  242.  
  243. // Check if form values are submitted
  244. if (count(array_diff($formKeys, $getKeys)) !== count($formKeys)) {
  245. $form->isValid($form->getValues());
  246. $select = $this->getAppsQuery();
  247.  
  248. $query = $form->getValue('query');
  249.  
  250. // Escape query - disable wildcards
  251. if (!empty($query)) {
  252. $query = str_replace(['%', '_'], ['\%', '\_'], $db->quote($query));
  253. $query = substr($query, 1, strlen($query) - 2);
  254. $select->where("name LIKE '%{$query}%' ESCAPE '\\\\'");
  255. }
  256.  
  257. if ($range !== null) {
  258. $select->having('price BETWEEN ' . $priceForm->getValue('min') . ' AND ' . $priceForm->getValue('max'));
  259. }
  260. }
  261.  
  262. if ($select !== null) {
  263. if ($this->getRequest()->isXmlHttpRequest()) {
  264. $this->disableView();
  265.  
  266. $data = $db->fetchAll($select->limit(10));
  267.  
  268. if (empty($data)) {
  269. $result = ['error' => $this->translate('No results found')];
  270. } else {
  271. $result = [];
  272.  
  273. foreach ($data as $app) {
  274. $result[] = [
  275. 'name' => $app['name'],
  276. 'price' => $app['price'],
  277. 'formattedPrice' => $this->view->formatNumber($app['price']),
  278. 'platforms' => explode("\n", $app['platforms']),
  279. 'url' => $this->getServerUrl() . $this->view->url(['controller' => 'apps', 'action' => $app['is_bundle'] ? 'bundle' : 'app', 'id' => $app['id']], 'default', true)
  280. ];
  281. }
  282. }
  283.  
  284. $this->getHelper('json')->direct($result);
  285. } else {
  286. $result = $db->fetchAll($select);
  287. }
  288. }
  289.  
  290. // Add view options
  291. if ($range !== null) {
  292. $this->viewOptions['sliderRange'] = $range;
  293. $this->viewOptions['sliderValues'] = [$priceForm->getValue('min'), $priceForm->getValue('max')];
  294. }
  295.  
  296. $this->view->result = $result;
  297. $this->view->form = $form;
  298. }
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement