· 4 years ago · Apr 28, 2021, 12:14 PM
1<?php
2class Module_market_items extends Trongate {
3
4 private $default_limit = 20;
5 private $per_page_options = array(10, 20, 50, 100);
6
7 function make_item_details_safe($output) {
8 $body = $output['body'];
9 $resp_obj = json_decode($body);
10 $item_obj = $resp_obj[0];
11
12 unset($item_obj->secret_dir);
13 unset($item_obj->approved);
14 unset($item_obj->status_level_id);
15 unset($item_obj->finished_build);
16 unset($item_obj->live);
17 unset($item_obj->module_file_name);
18 unset($item_obj->lifetime_revenue);
19 unset($item_obj->new_category_title);
20 unset($item_obj->assigned_to);
21 unset($item_obj->done_one_upload);
22
23 //let's also add module features to this...
24 $module_features = $this->_fetch_module_features($item_obj->id);
25 $item_obj->features = json_encode($module_features);
26
27 //attempt to get screenshots
28 $this->module('item_gallery');
29 $screenshots = $this->item_gallery->_get_previously_uploaded_files($item_obj->code, true);
30
31 if (gettype($screenshots) !== 'array') {
32 $screenshots = [];
33 }
34
35 $item_obj->screenshots = json_encode($screenshots);
36
37 //get the publisher name
38 $publisher_obj = $this->model->get_where($item_obj->publisher_id, 'publishers');
39
40 if ($publisher_obj == false) {
41 $item_obj->publisher_name = 'Unknown publisher';
42 } else {
43 $item_obj->publisher_name = $publisher_obj->publisher_name;
44 }
45
46 $estimated_downloads = $item_obj->estimated_downloads;
47
48 if ($estimated_downloads == 1) {
49 $downloads_entity = 'download';
50 } else {
51 $downloads_entity = 'downloads';
52 }
53
54 $item_obj->estimated_downloads = number_format($estimated_downloads).' '.$downloads_entity;
55
56 $resp_obj[0] = $item_obj;
57 $resp_str = json_encode($resp_obj);
58 $output['body'] = $resp_str;
59 return $output;
60 }
61
62 function _fetch_module_features($item_id) {
63 $rows = $this->model->get_where_custom('module_market_items_id', $item_id, '=', 'id', 'mod_features');
64 $features = [];
65 foreach($rows as $row) {
66 $features[] = $row->feature_description;
67 }
68
69 return $features;
70 }
71
72 function get_new_and_popular($limit, $no_display=null) {
73 //fetch all of the new and popular items on the MM homepage
74 if (!is_numeric($limit)) {
75 http_response_code(400);
76 echo 'Non numeric limit';
77 die();
78 }
79
80 $sql1 = 'SELECT
81 module_market_items.*,
82 publishers.publisher_name
83 FROM
84 module_market_items
85 INNER JOIN
86 publishers
87 ON
88 module_market_items.publisher_id = publishers.id
89 ORDER BY estimated_downloads DESC
90 LIMIT 0, '.$limit;
91 $rows1 = $this->model->query($sql1, 'object');
92
93 $sql2 = 'SELECT
94 module_market_items.*,
95 publishers.publisher_name
96 FROM
97 module_market_items
98 INNER JOIN
99 publishers
100 ON
101 module_market_items.publisher_id = publishers.id
102 ORDER BY date_created DESC
103 LIMIT 0, '.$limit;
104 $rows2 = $this->model->query($sql2, 'object');
105
106 $results = array_merge($rows1, $rows2);
107
108 foreach($results as $row_key=>$row_value) {
109 $estimated_downloads = $row_value->estimated_downloads;
110 $estimated_downloads = number_format($estimated_downloads, 2);
111 $estimated_downloads = str_replace('.00', '', $estimated_downloads);
112 $results[$row_key]->estimated_downloads = $estimated_downloads;
113 }
114
115 if (isset($no_display)) {
116 return $results;
117 } else {
118 http_response_code(200);
119 echo json_encode($results);
120 die();
121 }
122 }
123
124 function removeIncludeNewParam($input) {
125 $params = $input['params'];
126
127 if (isset($params['includeNewItemsToo'])) {
128 unset($params['includeNewItemsToo']);
129 }
130
131 //check if approved is 1 and live is 1
132
133 $input['params'] = $params;
134 return $input;
135 }
136
137 function _make_sure_allowed($scenario, $params=null) {
138
139 $allowed = false;
140
141 //attempt to get trongate token
142 $this->module('trongate_tokens');
143 $token = $this->trongate_tokens->_attempt_get_valid_token();
144
145 //get the user level
146 $user_level = $this->trongate_tokens->_get_user_level($token);
147
148 if ($scenario == 'download free module') {
149 echo 'ready'; die();
150 }
151
152 if ($user_level == 'admin') {
153 $allowed = true;
154 return $token; //no need to worry about levels here
155 } else {
156
157 if (isset($params)) {
158 extract($params);
159 }
160
161 if ($scenario == 'write module') {
162
163 if (isset($code)) {
164 //is this a valid module code?
165 $module_obj = $this->model->get_one_where('code', $code, 'module_market_items');
166
167 if (gettype($module_obj) == 'object') {
168 $owner_id = $module_obj->publisher_id;
169
170 $this->module('publishers');
171 $publisher_id = $this->publishers->_get_publisher_id();
172
173 if ($owner_id == $publisher_id) {
174 $allowed = true;
175 return $token;
176 }
177
178 }
179
180 }
181
182 }
183
184 }
185
186 //you should not be here but...
187 if ($allowed == false) {
188 redirect('forbidden');
189 }
190
191 }
192
193 function submit_approved_status() {
194 $this->module('security');
195 $token = $this->security->_make_sure_allowed();
196
197 if ($token == false) {
198 echo 'not allowed'; die();
199 }
200
201 $params['approved'] = $this->input('approved_id');
202 $params['code'] = $this->url->segment(3);
203 $sql = 'update module_market_items set approved = :approved where code = :code';
204 $this->model->query_bind($sql, $params);
205
206 redirect(previous_url());
207 }
208
209 function _draw_admin_controls($module_obj) {
210 //fetch the item
211 $data['approved'] = $module_obj->approved;
212 $options[0] = 'Not approved';
213 $options[1] = 'Approved';
214 $data['item_code'] = $module_obj->code;
215 $data['options'] = $options;
216 $data['module_obj'] = $module_obj;
217 $data['view_module'] = 'module_market_items';
218 $this->view("admin_controls", $data);
219 }
220
221 function _download_refused($msg) {
222 http_response_code(401);
223 echo $msg; die();
224 }
225
226 function request_download_free() {
227 //gets called from API only!!!
228 $allowed = false; // assume failure
229
230 if (!isset($_SERVER['HTTP_TRONGATETOKEN'])) {
231 http_response_code(400);
232 echo 'No token';
233 die();
234 }
235
236 $token = $_SERVER['HTTP_TRONGATETOKEN'];
237
238 //make sure token is from any valid user
239 $this->module('security');
240 $valid_token = $this->security->_is_token_valid($token);
241
242 if ($valid_token == false) {
243 http_response_code(400);
244 echo 'Invalid token';
245 die();
246 }
247
248 $code = $this->url->segment(3);
249
250 //make sure item code is valid
251 if ($code == '') {
252 http_response_code(400);
253 echo 'No item code';
254 die();
255 }
256
257 //make sure the item is valid
258 $item_obj = $this->model->get_one_where('code', $code, 'module_market_items');
259 if ($item_obj == false) {
260 http_response_code(400);
261 echo 'Invalid item code';
262 die();
263 }
264
265 $allowed = true; //user is allowed
266
267 //get the location of the target file
268 $module_file_path = $this->_get_file_path($item_obj);
269
270 if (!file_exists($module_file_path)) {
271 http_response_code(400);
272 echo 'File does not exist';
273 die();
274 }
275
276 //by now, user has been cleared for download and the file exists!
277
278 //make sure we have a 'temp' path with an index file
279 $temp_path = 'module_resources/'.$item_obj->code.'/temp/index.php';
280 $this->module('module_market-submit_mod');
281
282 if (!file_exists($temp_path)) {
283 $target_dir = str_replace('/index.php', '', $temp_path);
284 $this->submit_mod->_make_sure_dir_exists($target_dir);
285 }
286
287 //generate a random folder name...
288 $folder_name = make_rand_str(32);
289 $folder_name = strtolower($folder_name);
290 $nowtime = time();
291
292 //build the temp folder path...
293 $temp_dir = str_replace('/index.php', '', $temp_path).'/';
294
295 //create random string that's made up of a timestamp PLUS a 16 digit rand str
296 $rand_dir_name = time();
297 $rand_dir_name.= make_rand_str(16);
298 $rand_dir_name = strtolower($rand_dir_name);
299
300 $temp_file_dir = $temp_dir.$rand_dir_name.'/';
301 $this->submit_mod->_make_sure_dir_exists($temp_file_dir);
302
303 //get the name of the uploaded file
304 $module_file_name = $item_obj->module_file_name;
305
306 //copy the uploaded file into the new temp dir
307 $new_file_path = $temp_file_dir.$module_file_name;
308 copy($module_file_path, $new_file_path);
309
310 //echo out the new file path
311 $data['file_path'] = BASE_URL.$new_file_path;
312 $data['file_name'] = $module_file_name;
313 $data['item_code'] = $code;
314 echo json_encode($data);
315
316 $this->_delete_old_temp_files($code);
317 die();
318 }
319
320 function test($code) {
321 $api_key = '4qUvVvtSFCasttJPqYpKuzBE5PQS6ujBb3dEaH9nE8BQ8dX5yUbdKmLeyTaN3CJ8';
322 redirect('module_market_items/request_download_alt/'.$code.'/'.$api_key);
323 }
324
325 function _request_download_alt($api_key) {
326
327 //requesting download from desktop app
328 $post = file_get_contents('php://input');
329 $posted_data = json_decode($post, true);
330
331 if (!isset($posted_data['item_code'])) {
332 $this->_download_refused('No item code.');
333 }
334
335 $code = $posted_data['item_code'];
336
337 //make sure item code is valid
338 if ($code == '') {
339 $this->_download_refused('No item code.');
340 }
341
342 $item_obj = $this->model->get_one_where('code', $code, 'module_market_items');
343
344 if ($item_obj == false) {
345 $this->_download_refused('Invalid item code.');
346 }
347
348 $api_key_len = strlen($api_key);
349 if ($api_key_len !== 64) {
350 http_response_code(400);
351 echo 'Invalid API Key'; die();
352 }
353
354 //attempt to get member obj from the api_key
355 $params['api_key'] = $api_key;
356 $sql = 'SELECT
357 members.*
358 FROM
359 api_keys
360 INNER JOIN
361 trongate_users
362 ON
363 api_keys.trongate_user_id = trongate_users.id
364 INNER JOIN
365 members
366 ON
367 trongate_users.id = members.trongate_user_id
368 WHERE api_keys.api_key = :api_key';
369 $rows = $this->model->query_bind($sql, $params, 'object');
370 $num_rows = count($rows);
371
372 if ($num_rows > 0) {
373 $member_obj = $rows[0]; //the first row!
374 //if error_msg is true then we are good to go
375 $error_msg = $this->_check_download_allowed($member_obj, $item_obj);
376
377 if ($error_msg == true) {
378 $this->_init_download($item_obj);
379 } else {
380 $this->_download_refused($error_msg);
381 }
382 } else {
383 $this->_download_refused('Invalid API key.');
384 }
385
386 }
387
388 function _init_download($item_obj) {
389
390 //user has passed authorization, let's download the item...
391
392 //get the location of the target file
393 $module_file_path = $this->_get_file_path($item_obj);
394
395 if (!file_exists($module_file_path)) {
396 $this->_download_refused('File does not exist');
397 }
398
399 $temp_path = 'module_resources/'.$item_obj->code.'/temp/index.php';
400 $this->module('module_market-submit_mod');
401
402 if (!file_exists($temp_path)) {
403 $target_dir = str_replace('/index.php', '', $temp_path);
404 $this->submit_mod->_make_sure_dir_exists($target_dir);
405 }
406
407 //generate a random folder name...
408 $folder_name = make_rand_str(32);
409 $folder_name = strtolower($folder_name);
410 $nowtime = time();
411
412 //build the temp folder path...
413 $temp_dir = str_replace('/index.php', '', $temp_path).'/';
414
415 //create random string that's made up of a timestamp PLUS a 16 digit rand str
416 $rand_dir_name = time();
417 $rand_dir_name.= make_rand_str(16);
418 $rand_dir_name = strtolower($rand_dir_name);
419
420 $temp_file_dir = $temp_dir.$rand_dir_name.'/';
421 $this->submit_mod->_make_sure_dir_exists($temp_file_dir);
422
423 //get the name of the uploaded file
424 $module_file_name = $item_obj->module_file_name;
425
426 //copy the uploaded file into the new temp dir
427 $new_file_path = $temp_file_dir.$module_file_name;
428 copy($module_file_path, $new_file_path);
429
430 //echo out the new file path
431 $data['file_path'] = BASE_URL.$new_file_path;
432 $data['file_name'] = $module_file_name;
433 $data['item_code'] = $item_obj->code;
434
435 $category_id = $item_obj->category_id;
436 settype($category_id, 'integer');
437
438 $this->module('mod_market_categories');
439 $special_categories = $this->mod_market_categories->_get_special_categories();
440
441 if (in_array($category_id, $special_categories)) {
442 $data['force_choose_location'] = true;
443 } else {
444 $data['force_choose_location'] = false;
445 }
446
447 http_response_code(200);
448 echo json_encode($data);
449
450 $this->_delete_old_temp_files($item_obj->code);
451 die();
452 }
453
454 function _check_download_allowed($member_obj, $item_obj) {
455 //is this member allowed to download this item?
456
457 //who owns this item?
458 $owner_member_id = $this->_get_item_member_id($item_obj);
459
460 if ($owner_member_id == false) {
461 $error_msg = 'Unable to find item owner';
462 return $error_msg; //end of the road
463 } elseif($member_obj->id == $owner_member_id) {
464 $error_msg = true;
465 return $error_msg; //allowed since you are the owner!
466 }
467
468 //if we get past here then we are NOT the owner but there is an owner so...
469
470 //fetch the item price
471 settype($item_obj->price, 'double');
472
473 if ($item_obj->price < 0.01) {
474 $error_msg = true;
475 return $error_msg;
476 } else {
477 //we have work to here because this is a paid app
478 $error_msg = 'Paid app - fix this later';
479 return $error_msg;
480 }
481
482 }
483
484 function _get_item_member_id($item_obj) {
485 //attempt to return the member_id of the person who published the item
486
487 $params['code'] = $item_obj->code;
488 $sql = 'SELECT
489 members.id
490 FROM
491 module_market_items
492 INNER JOIN
493 publishers
494 ON
495 module_market_items.publisher_id = publishers.id
496 INNER JOIN
497 members
498 ON
499 publishers.member_id = members.id
500 WHERE module_market_items.code = :code';
501 $rows = $this->model->query_bind($sql, $params, 'object');
502 $num_rows = count($rows);
503 if($num_rows>0) {
504 $target_row = $rows[0];
505 $member_id = $target_row->id;
506 } else {
507 $member_id = false;
508 }
509
510 return $member_id;
511
512 }
513
514 function request_download($code) {
515 //gets called from website
516 $allowed = false; // assume failure
517
518 //make sure item code is valid
519 if ($code == '') {
520 $this->_download_refused('No item code.');
521 }
522
523 $item_obj = $this->model->get_one_where('code', $code, 'module_market_items');
524 if ($item_obj == false) {
525 $this->_download_refused('Invalid item code.');
526 }
527
528 //attempt to fetch Trongate token
529 $this->module('trongate_tokens');
530 $token = $this->trongate_tokens->_attempt_get_valid_token();
531
532 if ($token == false) {
533 if (isset($_SERVER['HTTP_TRONGATETOKEN'])) {
534 $token = $_SERVER['HTTP_TRONGATETOKEN'];
535 }
536 }
537
538 if ($token == false) {
539 http_response_code(401);
540 $this->_download_refused('No token.');
541 } else {
542 //user has a valid token START
543 $user_level = $this->trongate_tokens->_get_user_level($token);
544 $user_level = strtolower($user_level);
545
546 if ($user_level == "admin") {
547 $allowed = true;
548 } else {
549 //check to see if this is the owner
550 $this->module('members');
551 $member_obj = $this->members->_get_member_obj($token);
552 $allowed = $this->_check_download_allowed($member_obj, $item_obj);
553 //user has a valid token END
554 }
555
556 if ($allowed !== true) {
557 $this->_download_refused('Request refused.');
558 }
559
560 }
561
562 //get the location of the target file
563 $module_file_path = $this->_get_file_path($item_obj);
564
565 if (!file_exists($module_file_path)) {
566 $this->_download_refused('File does not exist');
567 }
568
569 //alright kid, you're all clear - no let's blow this thing and go home!
570 $this->_init_download($item_obj);
571 }
572
573 function _delete_old_temp_files($code) {
574
575 //get the temp path for the module resources
576 $temp_path = 'module_resources/'.$code.'/temp';
577
578 $nowtime = time();
579 $ancient_history = $nowtime-600; //ten minutes back
580
581 $dir_contents = scandir($temp_path);
582 foreach ($dir_contents as $dir_item) {
583 $item_path = $temp_path.'/'.$dir_item;
584 $strlen = strlen($dir_item);
585
586 if ($strlen>7) {
587
588 $dir_timestamp = substr($dir_item, 0, 10);
589 if ($dir_timestamp<$ancient_history) {
590 $this->rrmdir($item_path);
591 }
592
593 }
594
595 }
596
597 }
598
599 function rrmdir($dir) {
600 if (is_dir($dir)) {
601 $objects = scandir($dir);
602 foreach ($objects as $object) {
603 if ($object != "." && $object != "..") {
604 if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object))
605 $this->rrmdir($dir. DIRECTORY_SEPARATOR .$object);
606 else
607 unlink($dir. DIRECTORY_SEPARATOR .$object);
608 }
609 }
610 rmdir($dir);
611 }
612 }
613
614 function _get_file_path($module_obj) {
615 //returns the path to the uploaded file
616 $this->module('module_uploader');
617 $source_dir = $this->module_uploader->_get_source_dir($module_obj->code, true);
618 $secret_dir = $module_obj->secret_dir;
619 $source_dir.= $secret_dir.'/';
620 $file_path = $source_dir.$module_obj->module_file_name;
621 return $file_path;
622 }
623
624 function get_file_details($code=null) {
625
626 if (!isset($code)) {
627 $code = $this->url->segment(3);
628 $return_result = false;
629 } else {
630 $return_result = true;
631 }
632
633 $module_obj = $this->model->get_one_where('code', $code, 'module_market_items');
634
635 if ($module_obj == false) {
636 http_response_code(400);
637 echo 'Invalid code';
638 die();
639 } else {
640
641 $module_file_path = $this->_get_file_path($module_obj);
642
643 if (file_exists($module_file_path)) {
644
645 http_response_code(200);
646 $file_size = filesize($module_file_path)/1000;
647 $file_size = number_format($file_size, 2);
648 $data['file_name'] = $module_obj->module_file_name;
649 $data['file_size'] = $file_size.' kB';
650 $data['uploaded_date'] = date('l jS F Y \a\t H:i:s', $module_obj->module_uploaded_date);
651
652 if ($return_result == true) {
653 return $data;
654 } else {
655 echo json_encode($data);
656 }
657
658 die();
659 } else {
660
661 //file was not found, so set the column value to zero
662 $update_id = $module_obj->id;
663 $update_data['module_file_name'] = '';
664 $update_data['module_uploaded_date'] = 0;
665 $update_data['secret_dir'] = '';
666 $this->model->update($update_id, $update_data, 'module_market_items');
667
668 http_response_code(400);
669 echo 'File not found';
670 die();
671 }
672
673 }
674
675 }
676
677 function _draw_checklist_table($data) {
678
679 $panels = $data['manage_images_panels'];
680
681 //panels contains info about cover and the icon
682 $panels_data = $this->_extract_panels_data($panels); //gives us cover and icon [bools]
683 $panels_data['basic_info'] = true;
684 $panels_data['true_html'] = '<button class="btn btn-success thumb-btn btn-action" onclick="alert(xxxx)">';
685 $panels_data['true_html'].= '<i class="icon icon-check"></i></button>';
686 $panels_data['false_html'] = str_replace('icon-check', 'icon-cross', $panels_data['true_html']);
687 $panels_data['false_html'] = str_replace('btn-success', 'btn-danger', $panels_data['false_html']);
688
689 $panels_data['code'] = $data['module_obj']->code;
690 $this->view('checklist_table', $panels_data);
691 }
692
693 function _extract_panels_data($str) {
694 $bits = explode('<span class="label ', $str);
695 unset($bits[0]);
696 $count = 0;
697 foreach($bits as $bit) {
698 $count++;
699 $bit = '<span class="label '.$bit;
700
701 $strpos = strpos($bit, '<span class="label label-rounded label-success">');
702
703 if ($count == 1) {
704 $target_property = 'cover';
705 } else {
706 $target_property = 'icon';
707 }
708
709 if (is_numeric($strpos)) {
710 $data[$target_property] = true;
711 } else {
712 $data[$target_property] = false;
713 }
714
715 }
716
717 return $data;
718 }
719
720 function _get_panels_data($data) {
721 $code = $data['code'];
722
723 $row_data['got_item_class'] = 'label label-rounded label-success';
724
725 $row_data['name'] = 'Basic Information';
726 $row_data['checked_msg'] = 'The basic information appears to be fine.';
727 $row_data['edit_url'] = BASE_URL.'module_market-submit_mod/basic_form/'.$code;
728 $row_data['instant_delete_available'] = false;
729 $row_data['missing_item_class'] = 'label label-rounded label-error';
730 $panels_data[] = $row_data;
731
732 $row_data['name'] = 'Cover';
733 $row_data['checked_msg'] = 'You have successfully uploaded a cover image.';
734 $row_data['edit_url'] = BASE_URL.'module_market-submit_mod/upload_promo_pic/'.$code;
735 $row_data['instant_delete_available'] = true;
736 $row_data['missing_item_class'] = 'label label-rounded label-error';
737 $panels_data[] = $row_data;
738
739 $row_data['name'] = 'Icon';
740 $row_data['checked_msg'] = 'You have successfully uploaded an icon.';
741 $row_data['edit_url'] = BASE_URL.'module_market-submit_mod/upload_icon/'.$code;
742 $row_data['instant_delete_available'] = true;
743 $row_data['missing_item_class'] = 'label label-rounded label-error';
744 $panels_data[] = $row_data;
745
746 $row_data['name'] = 'Features';
747 $row_data['checked_msg'] = 'Features have been successfully added.';
748 $row_data['edit_url'] = BASE_URL.'module_market-submit_mod/declare_features/'.$code;
749 $row_data['instant_delete_available'] = false;
750 $row_data['missing_item_class'] = 'label label-rounded label-error';
751 $panels_data[] = $row_data;
752
753 $row_data['name'] = 'YouTube Video (optional)';
754 $row_data['checked_msg'] = 'You have successfully assoicated your item with a YouTube video.';
755 $row_data['edit_url'] = BASE_URL.'module_market-submit_mod/create_youtube_id/'.$code;
756 $row_data['instant_delete_available'] = true;
757 $row_data['missing_item_class'] = 'label label-rounded label-warning';
758 $panels_data[] = $row_data;
759
760 $row_data['name'] = 'Screenshots (optional)';
761 $row_data['checked_msg'] = 'You have successfully added some screenshots.';
762 $row_data['edit_url'] = BASE_URL.'item_gallery/manage/'.$code;
763 $row_data['instant_delete_available'] = false;
764 $row_data['missing_item_class'] = 'label label-rounded label-warning';
765 $panels_data[] = $row_data;
766
767 //let's now check and see if we have the different things...
768 foreach($panels_data as $panel_key => $panel_array) {
769 $panel_obj = (object) $panel_array;
770 $panel_name = $panel_obj->name;
771 $panel_name = $panel_obj->name;
772 $panels_data[$panel_key]['got_item'] = $this->_got_item($data, $panel_name, $panel_obj); // true or false
773
774 }
775
776 return $panels_data;
777 }
778
779 function _got_item($data, $panel_name, $panel_obj) {
780
781 $code = $data['code'];
782
783 switch ($panel_name) {
784 case 'Basic Information':
785 $got_item = true;
786 break;
787 case 'Cover':
788 $got_item = $this->_got_cover($code, $data);
789 break;
790 case 'Icon':
791 $got_item = $this->_got_icon($code, $data);
792 break;
793 case 'Features':
794 $got_item = $this->_got_features($data['features']);
795 break;
796 case 'YouTube Video (optional)':
797 $got_item = $this->_got_youtube_video($data['module_obj']);
798 break;
799 case 'Screenshots (optional)':
800 $got_item = $this->_got_screenshots($data['screenshots']);
801 break;
802 default:
803 $got_item = false;
804 break;
805 }
806
807 return $got_item;
808
809 }
810
811 function _got_cover($code, $data) {
812
813 $got_item = false;
814 $score = 0;
815
816 $module_obj = $data['module_obj'];
817 $cover = trim($module_obj->cover);
818
819 if ($cover == '') {
820 return false;
821 } else {
822 $score++;
823 }
824
825 //get the path for the item cover
826 $this->module('module_market-submit_mod');
827 $destination = $this->submit_mod->_fetch_image_path($code, 'cover');
828 $destination = str_replace(BASE_URL, '', $destination);
829
830 //do we have an image directory?
831 $is_dir = is_dir($destination);
832
833 if ($is_dir == true) {
834 //does the directory contain an image?
835 $files = array_diff(scandir($destination), array('.', '..'));
836
837 foreach($files as $file) {
838 $last_four = substr($file, -4);
839
840 if ($last_four !== '.php') {
841 $score++;
842 }
843 }
844
845 }
846
847 if ($score>1) {
848 $got_item = true;
849 }
850
851 return $got_item;
852
853 }
854
855 function _got_icon($code, $data) {
856
857 $got_item = false;
858 $score = 0;
859
860 $module_obj = $data['module_obj'];
861 $icon = trim($module_obj->icon);
862
863 if ($icon == '') {
864 return false;
865 } else {
866 $score++;
867 }
868
869 //get the path for the item icon
870 $this->module('module_market-submit_mod');
871 $destination = $this->submit_mod->_fetch_image_path($code, 'icon');
872 $destination = str_replace(BASE_URL, '', $destination);
873
874 //do we have an image directory?
875 $is_dir = is_dir($destination);
876
877 if ($is_dir == true) {
878 //does the directory contain an image?
879 $files = array_diff(scandir($destination), array('.', '..'));
880
881 foreach($files as $file) {
882 $last_four = substr($file, -4);
883
884 if ($last_four !== '.php') {
885 $score++;
886 }
887 }
888
889 }
890
891 if ($score>1) {
892 $got_item = true;
893 }
894
895 return $got_item;
896
897 }
898
899 function _got_features($features) {
900
901 $got_item = false;
902
903 if (gettype($features) == 'array') {
904 $num_features = count($features);
905
906 if ($num_features>2) {
907 $got_item = true;
908 }
909 }
910
911 return $got_item;
912 }
913
914 function _got_youtube_video($module_obj) {
915 $youtube_video_id = $module_obj->youtube_video_id;
916
917 $char_len = strlen($youtube_video_id);
918
919 if ($char_len > 6) {
920 return true;
921 } else {
922 return false;
923 }
924
925 }
926
927 function _got_screenshots($screenshots) {
928
929 $got_item = false;
930
931 if (gettype($screenshots) == 'array') {
932
933 $num_screenshots = count($screenshots);
934 if ($num_screenshots>0) {
935 $got_item = true;
936 }
937
938 }
939
940 return $got_item;
941
942 }
943
944 function _draw_checklist_tbl($data) {
945 $data['view_module'] = 'module_market_items';
946 $this->view('checklist_tbl', $data);
947 }
948
949 function _make_sure_dir_exists($destination) {
950
951 $target_dir = APPPATH.'public/'.$destination;
952
953 if (!file_exists($target_dir)) {
954 //generate the image folder
955 mkdir($target_dir, 0777, true);
956 }
957
958 }
959
960 function _get_rand_cover() {
961 $covers[] = 'inlinepreview.jpg';
962 $covers[] = '590x300.png';
963 $covers[] = 'as-preview.jpeg';
964 $covers[] = 'inline_00000.jpeg';
965 $covers[] = 'item_preview.jpeg';
966 $covers[] = 'mouse590x300.';
967 $covers[] = 'preview.jpg';
968 $covers[] = 'preview2.jpg';
969
970 $target_index = rand(0,7);
971 $cover = $covers[$target_index];
972 return $cover;
973 }
974
975 function _get_rand_price() {
976
977 $all_price_options[] = 0;
978 $all_price_options[] = 12;
979 $all_price_options[] = 24;
980 $all_price_options[] = 45;
981 $all_price_options[] = 75;
982 $all_price_options[] = 125;
983 $all_price_options[] = 175;
984 $all_price_options[] = 240;
985
986 $target_index = rand(0,7);
987
988 $price = $all_price_options[$target_index];
989 return $price;
990 }
991
992
993 function _extract_content($string, $start, $end) {
994 $pos = stripos($string, $start);
995 $str = substr($string, $pos);
996 $str_two = substr($str, strlen($start));
997 $second_pos = stripos($str_two, $end);
998 $str_three = substr($str_two, 0, $second_pos);
999 $content = trim($str_three); // remove whitespaces
1000 return $content;
1001 }
1002
1003 function _fetch_new_items($limit) {
1004 $sql = 'SELECT * from module_market_items
1005 WHERE live=1
1006 AND approved=1
1007 ORDER BY date_created DESC
1008 LIMIT 0,'.$limit;
1009 $new_items = $this->model->query($sql, 'object');
1010 return $new_items;
1011 }
1012
1013
1014 function build_onto_results($output) {
1015 $post = file_get_contents('php://input');
1016 $params = json_decode($post, true);
1017
1018 if (isset($params['includeNewItemsToo'])) {
1019 //go fetch the new items ya bas
1020
1021 if (isset($params['limit'])) {
1022 $limit = $params['limit'];
1023
1024 if (!is_numeric($limit)) { //sql injection protection
1025 http_response_code(400);
1026 echo 'non numeric limit';
1027 die();
1028 }
1029
1030 } else {
1031 $limit = 5;
1032 }
1033
1034 $new_items = $this->_fetch_new_items($limit);
1035 }
1036
1037 $body = json_decode($output['body']);
1038 if (isset($new_items)) {
1039 $body = array_merge($body, $new_items);
1040 }
1041
1042 foreach($body as $key=>$row) {
1043 $cover = $row->cover;
1044 $code = $row->code;
1045 $cover_url = BASE_URL.'module_resources/'.$code.'/cover/'.$cover;
1046 $row->cover_url = $cover_url;
1047 $body[$key]->cover_url = $cover_url;
1048
1049 $price = $row->price;
1050 settype($price, 'double');
1051
1052 if ($row->price>0) {
1053 $item_price = number_format($price, 2);
1054 $item_price = '<span class="smaller">$</span>'.$item_price;
1055 } else {
1056 $item_price = 'FREE';
1057 }
1058
1059 $body[$key]->stars_code = '★★★★★';
1060
1061 $body[$key]->item_price = $item_price;
1062
1063 $approved = $body[$key]->approved;
1064 $live = $body[$key]->live;
1065
1066 $status = $this->_get_module_status($live, $approved);
1067
1068 $body[$key]->status = $status;
1069
1070 if ($body[$key]->approved == 1) {
1071 $body[$key]->approved = 'yes';
1072 } else {
1073 $body[$key]->approved = 'no';
1074 }
1075
1076 }
1077
1078 $output['body'] = json_encode($body);
1079 return $output;
1080 }
1081
1082 function _get_module_status($live, $approved) {
1083
1084 if ($approved == 'yes') {
1085 $approved = 1;
1086 } else {
1087 $approved = 0;
1088 }
1089
1090 if ($live == 'yes') {
1091 $live = 1;
1092 } else {
1093 $live = 0;
1094 }
1095
1096
1097 if (($approved == 1) && ($live == 1)) {
1098 $live = 'approved and live';
1099 }
1100
1101 if (($approved == 0) && ($live == 0)) {
1102 $live = 'not approved and not live';
1103 }
1104
1105 if (($approved == 0) && ($live == 1)) {
1106 $live = 'not approved and not live';
1107 }
1108
1109 if (($approved == 1) && ($live == 0)) {
1110 $live = 'approved and not live';
1111 }
1112
1113 $status = $live;
1114 return $status;
1115
1116 }
1117
1118 function _init_picture_uploader_multi_settings() {
1119 $data['targetModule'] = 'module_market_items';
1120 $data['destination'] = 'module_resources/code/picture_gallery';
1121 $data['max_file_size'] = 1200;
1122 $data['max_width'] = 2500;
1123 $data['max_height'] = 1400;
1124 return $data;
1125 }
1126
1127 function _draw_tabs($module_obj) {
1128 $data['module_obj'] = $module_obj;
1129 $this->view('module_tabs', $data);
1130 }
1131
1132 function _get_manage_images_panels($module_obj) {
1133
1134 $no_pic_warning = 'Your module currently does not have a';
1135 $this->module('module_market');
1136
1137 $row_data['update_item_url'] = BASE_URL.'module_market-submit_mod/upload_promo_pic/'.$module_obj->code;
1138 $row_data['picture_path'] = BASE_URL.'module_resources/'.$module_obj->code.'/cover/'.$module_obj->cover;
1139 $row_data['column_name'] = 'cover';
1140 $row_data['column_value'] = $module_obj->cover;
1141 $row_data['no_pic_warning'] = $no_pic_warning.' cover!';
1142 $row_data['image_exists'] = $this->module_market->_image_exists($row_data['picture_path'], $module_obj->id, $row_data['column_name'], $row_data['column_value']);
1143 $images_data[] = $row_data;
1144
1145 $row_data['update_item_url'] = BASE_URL.'module_market-submit_mod/upload_icon/'.$module_obj->code;
1146 $row_data['picture_path'] = BASE_URL.'module_resources/'.$module_obj->code.'/icon/'.$module_obj->icon;
1147 $row_data['column_name'] = 'icon';
1148 $row_data['column_value'] = $module_obj->icon;
1149 $row_data['no_pic_warning'] = $no_pic_warning.'n icon!';
1150 $row_data['image_exists'] = $this->module_market->_image_exists($row_data['picture_path'], $module_obj->id, $row_data['column_name'], $row_data['column_value']);
1151 $images_data[] = $row_data;
1152
1153 $data['images_data'] = $images_data;
1154 $html_code = $this->view('manage_images_panels', $data, true);
1155 return $html_code;
1156
1157 }
1158
1159 function summary() {
1160 $code = $this->url->segment(3);
1161 echo 'Display a summary of the module';
1162 }
1163
1164 function _init_picture_settings() {
1165 $picture_settings['targetModule'] = 'module_market_items';
1166 $picture_settings['maxFileSize'] = 2000;
1167 $picture_settings['maxWidth'] = 1200;
1168 $picture_settings['maxHeight'] = 1200;
1169 $picture_settings['resizedMaxWidth'] = 450;
1170 $picture_settings['resizedMaxHeight'] = 450;
1171 $picture_settings['destination'] = 'module_market_items_pics';
1172 $picture_settings['targetColumnName'] = 'picture';
1173 $picture_settings['thumbnailDir'] = 'module_market_items_pics_thumbnails';
1174 $picture_settings['thumbnailMaxWidth'] = 120;
1175 $picture_settings['thumbnailMaxHeight'] = 120;
1176 return $picture_settings;
1177 }
1178
1179 function manage() {
1180 $this->module('security');
1181 $data['token'] = $this->security->_make_sure_allowed();
1182 $data['order_by'] = 'title';
1183
1184 //format the pagination
1185 $data['total_rows'] = $this->model->count('module_market_items');
1186 $data['record_name_plural'] = 'module market items';
1187
1188 $data['headline'] = 'Manage Module Market Items';
1189 $data['view_module'] = 'module_market_items';
1190 $data['view_file'] = 'manage';
1191
1192 $this->template('admin', $data);
1193 }
1194
1195 function show() {
1196 $this->module('security');
1197 $token = $this->security->_make_sure_allowed();
1198
1199 $update_id = $this->url->segment(3);
1200
1201 if ((!is_numeric($update_id)) && ($update_id != '')) {
1202 redirect('module_market_items/manage');
1203 }
1204
1205 $data = $this->_get_data_from_db($update_id);
1206 $data['token'] = $token;
1207
1208 if ($data == false) {
1209 redirect('module_market_items/manage');
1210 } else {
1211 $data['form_location'] = BASE_URL.'module_market_items/submit/'.$update_id;
1212 $data['update_id'] = $update_id;
1213 $data['headline'] = 'Module Market Item Information';
1214 $data['picture_uploader_multi_settings'] = $this->_init_picture_uploader_multi_settings();
1215 $data['view_file'] = 'show';
1216 $this->template('admin', $data);
1217 }
1218 }
1219
1220 function _get_page_headline($update_id) {
1221 //figure out what the page headline should be (on the module_market_items/create page)
1222 if (!is_numeric($update_id)) {
1223 $headline = 'Create New Module Market Item Record';
1224 } else {
1225 $headline = 'Update Module Market Item Details';
1226 }
1227
1228 return $headline;
1229 }
1230
1231 function submit() {
1232 $this->module('security');
1233 $this->security->_make_sure_allowed();
1234
1235 $submit = $this->input('submit', true);
1236
1237 if ($submit == 'Submit') {
1238
1239 $this->validation_helper->set_rules('title', 'Title', 'required|min_length[2]|max_length[255]');
1240 $this->validation_helper->set_rules('description', 'Description', 'required|min_length[2]');
1241 $this->validation_helper->set_rules('youtube_video_id', 'YouTube Video ID', 'required|min_length[2]|max_length[255]');
1242 $this->validation_helper->set_rules('price', 'Price', 'required|max_length|numeric|greater_than[0]|numeric');
1243 $this->validation_helper->set_rules('item_type', 'Item Type', 'integer');
1244
1245 $result = $this->validation_helper->run();
1246
1247 if ($result == true) {
1248
1249 $update_id = $this->url->segment(3);
1250 $data = $this->_get_data_from_post();
1251 settype($data['price'], 'double');
1252 if (is_numeric($update_id)) {
1253 //update an existing record
1254 $this->model->update($update_id, $data, 'module_market_items');
1255 $flash_msg = 'The record was successfully updated';
1256 } else {
1257 //insert the new record
1258 $update_id = $this->model->insert($data, 'module_market_items');
1259 $flash_msg = 'The record was successfully created';
1260 }
1261
1262 set_flashdata($flash_msg);
1263 redirect('module_market_items/show/'.$update_id);
1264
1265 } else {
1266 //form submission error
1267 $this->create();
1268 }
1269
1270 }
1271
1272 }
1273
1274 function submit_delete() {
1275 $this->module('security');
1276 $this->security->_make_sure_allowed();
1277
1278 $submit = $this->input('submit', true);
1279
1280 if ($submit == 'Submit') {
1281 $update_id = $this->url->segment(3);
1282
1283 if (!is_numeric($update_id)) {
1284 die();
1285 } else {
1286 $data['update_id'] = $update_id;
1287
1288 //delete all of the comments associated with this record
1289 $sql = 'delete from comments where target_table = :module and update_id = :update_id';
1290 $data['module'] = $this->module;
1291 $this->model->query_bind($sql, $data);
1292
1293 //delete the record
1294 $this->model->delete($update_id, $this->module);
1295
1296 //set the flashdata
1297 $flash_msg = 'The record was successfully deleted';
1298 set_flashdata($flash_msg);
1299
1300 //redirect to the manage page
1301 redirect('module_market_items/manage');
1302 }
1303 }
1304 }
1305
1306 function _get_id_from_code($code) {
1307 $module_obj = $this->model->get_one_where('code', $code, 'module_market_items');
1308 if ($module_obj == false) {
1309 return false;
1310 } else {
1311 $id = $module_obj->id;
1312 return $id;
1313 }
1314 }
1315
1316 function _get_data_from_db($update_id) {
1317 $module_market_items = $this->model->get_where($update_id, 'module_market_items');
1318
1319 if ($module_market_items == false) {
1320 $this->template('error_404');
1321 die();
1322 } else {
1323 $data['title'] = $module_market_items->title;
1324 $data['description'] = $module_market_items->description;
1325 $data['youtube_video_id'] = $module_market_items->youtube_video_id;
1326 $data['price'] = $module_market_items->price;
1327 $data['code'] = $module_market_items->code;
1328 $data['item_type'] = $module_market_items->item_type;
1329 return $data;
1330 }
1331 }
1332
1333 function _get_data_from_post() {
1334 $data['title'] = $this->input('title', true);
1335 $data['description'] = $this->input('description', true);
1336 $data['youtube_video_id'] = $this->input('youtube_video_id', true);
1337 $data['price'] = $this->input('price', true);
1338 $data['code'] = $this->input('code', true);
1339 $data['item_type'] = $this->input('item_type', true);
1340 return $data;
1341 }
1342
1343 function test3() {
1344 $code = '0421F8RC';
1345 $this->_calc_average_rating($code);
1346 }
1347
1348 function _calc_average_rating($code) {
1349 $params['code'] = $code;
1350 $sql = 'SELECT
1351 module_market_items.id,
1352 module_market_item_reviews.rating
1353 FROM
1354 module_market_items
1355 INNER JOIN
1356 module_market_item_reviews
1357 ON
1358 module_market_items.id = module_market_item_reviews.item_id
1359 WHERE module_market_items.code = :code';
1360 $rows = $this->model->query_bind($sql, $params, 'object');
1361
1362 if (count($rows)>0) {
1363 $num_rows = count($rows);
1364 $total = 0;
1365 foreach($rows as $row) {
1366 $total = $total+$row->rating;
1367 }
1368 $average_rating = $total/$num_rows;
1369 } else {
1370 $average_rating = 0;
1371 }
1372
1373 if ($average_rating>0) {
1374 $data['average_stars'] = $average_rating;
1375 $update_id = $rows[0]->id;
1376 $this->model->update($update_id, $data, 'module_market_items');
1377 }
1378
1379 }
1380
1381}
1382
1383 function create() {
1384 $this->module('trongate_security');
1385 $this->trongate_security->_make_sure_allowed();
1386
1387 $update_id = segment(3);
1388 $submit = input('submit');
1389
1390 if (($submit == '') && (is_numeric($update_id))) {
1391 $data = $this->_get_data_from_db($update_id);
1392 } else {
1393 $data = $this->_get_data_from_post();
1394 }
1395
1396 $data['mod_market_categories_options'] = $this->_get_mod_market_categories_options($data['mod_market_categories_id']);
1397
1398 $data['publishers_options'] = $this->_get_publishers_options($data['publishers_id']);
1399
1400 if (is_numeric($update_id)) {
1401 $data['headline'] = 'Update Module Market Item Record';
1402 $data['cancel_url'] = BASE_URL.'module_market_items/show/'.$update_id;
1403 } else {
1404 $data['headline'] = 'Create New Module Market Item Record';
1405 $data['cancel_url'] = BASE_URL.'module_market_items/manage';
1406 }
1407
1408 $data['form_location'] = BASE_URL.'module_market_items/submit/'.$update_id;
1409 $data['view_file'] = 'create';
1410 $this->template('admin', $data);
1411 }
1412
1413 function manage() {
1414 $this->module('trongate_security');
1415 $this->trongate_security->_make_sure_allowed();
1416
1417 if (segment(4) !== '') {
1418 $data['headline'] = 'Search Results';
1419 $searchphrase = trim($_GET['searchphrase']);
1420 $params['title'] = '%'.$searchphrase.'%';
1421 $params['youtube_video_id'] = '%'.$searchphrase.'%';
1422 $params['code'] = '%'.$searchphrase.'%';
1423 $params['module_file_name'] = '%'.$searchphrase.'%';
1424 $params['secret_dir'] = '%'.$searchphrase.'%';
1425 $params['version'] = '%'.$searchphrase.'%';
1426 $sql = 'select * from module_market_items
1427 WHERE title LIKE :title
1428 OR youtube_video_id LIKE :youtube_video_id
1429 OR code LIKE :code
1430 OR module_file_name LIKE :module_file_name
1431 OR secret_dir LIKE :secret_dir
1432 OR version LIKE :version
1433 ORDER BY date_created desc';
1434 $all_rows = $this->model->query_bind($sql, $params, 'object');
1435 } else {
1436 $data['headline'] = 'Manage Module Market Items';
1437 $all_rows = $this->model->get('date_created desc');
1438 }
1439
1440 $pagination_data['total_rows'] = count($all_rows);
1441 $pagination_data['page_num_segment'] = 3;
1442 $pagination_data['limit'] = $this->_get_limit();
1443 $pagination_data['pagination_root'] = 'module_market_items/manage';
1444 $pagination_data['record_name_plural'] = 'module market items';
1445 $pagination_data['include_showing_statement'] = true;
1446 $data['pagination_data'] = $pagination_data;
1447
1448 $data['rows'] = $this->_reduce_rows($all_rows);
1449 $data['selected_per_page'] = $this->_get_selected_per_page();
1450 $data['per_page_options'] = $this->per_page_options;
1451 $data['view_module'] = 'module_market_items';
1452 $data['view_file'] = 'manage';
1453 $this->template('admin', $data);
1454 }
1455
1456 function show() {
1457 $this->module('trongate_security');
1458 $token = $this->trongate_security->_make_sure_allowed();
1459 $update_id = segment(3);
1460
1461 if ((!is_numeric($update_id)) && ($update_id != '')) {
1462 redirect('module_market_items/manage');
1463 }
1464
1465 $data = $this->_get_data_from_db($update_id);
1466 $data['live'] = ($data['live'] == 1 ? 'yes' : 'no');
1467 $data['approved'] = ($data['approved'] == 1 ? 'yes' : 'no');
1468 $data['token'] = $token;
1469
1470 if ($data == false) {
1471 redirect('module_market_items/manage');
1472 } else {
1473 $data['update_id'] = $update_id;
1474 $data['headline'] = 'Module Market Item Information';
1475 $data['view_file'] = 'show';
1476 $this->template('admin', $data);
1477 }
1478 }
1479
1480 function _reduce_rows($all_rows) {
1481 $rows = [];
1482 $start_index = $this->_get_offset();
1483 $limit = $this->_get_limit();
1484 $end_index = $start_index + $limit;
1485
1486 $count = -1;
1487 foreach ($all_rows as $row) {
1488 $count++;
1489 if (($count>=$start_index) && ($count<$end_index)) {
1490 $row->live = ($row->live == 1 ? 'yes' : 'no');
1491 $row->approved = ($row->approved == 1 ? 'yes' : 'no');
1492 $rows[] = $row;
1493 }
1494 }
1495
1496 return $rows;
1497 }
1498
1499 function submit() {
1500 $this->module('trongate_security');
1501 $this->trongate_security->_make_sure_allowed();
1502
1503 $submit = input('submit', true);
1504
1505 if ($submit == 'Submit') {
1506
1507 $this->validation_helper->set_rules('title', 'Title', 'required|min_length[2]|max_length[255]');
1508 $this->validation_helper->set_rules('description', 'Description', 'required|min_length[2]');
1509 $this->validation_helper->set_rules('youtube_video_id', 'YouTube Video ID', 'required|min_length[2]|max_length[255]');
1510 $this->validation_helper->set_rules('price', 'Price', 'required|max_length|numeric|greater_than[0]|numeric');
1511 $this->validation_helper->set_rules('version', 'Version', 'required|min_length[2]|max_length[255]');
1512
1513 $result = $this->validation_helper->run();
1514
1515 if ($result == true) {
1516
1517 $update_id = segment(3);
1518 $data = $this->_get_data_from_post();
1519 $data['publishers_id'] = (is_numeric($data['publishers_id']) ? $data['publishers_id'] : 0);
1520 $data['mod_market_categories_id'] = (is_numeric($data['mod_market_categories_id']) ? $data['mod_market_categories_id'] : 0);
1521 $data['live'] = ($data['live'] == 1 ? 1 : 0);
1522 $data['approved'] = ($data['approved'] == 1 ? 1 : 0);
1523
1524 if (is_numeric($update_id)) {
1525 //update an existing record
1526 $this->model->update($update_id, $data, 'module_market_items');
1527 $flash_msg = 'The record was successfully updated';
1528 } else {
1529 //insert the new record
1530 $update_id = $this->model->insert($data, 'module_market_items');
1531 $flash_msg = 'The record was successfully created';
1532 }
1533
1534 set_flashdata($flash_msg);
1535 redirect('module_market_items/show/'.$update_id);
1536
1537 } else {
1538 //form submission error
1539 $this->create();
1540 }
1541
1542 }
1543
1544 }
1545
1546 function submit_delete() {
1547 $this->module('trongate_security');
1548 $this->trongate_security->_make_sure_allowed();
1549
1550 $submit = input('submit');
1551 $params['update_id'] = segment(3);
1552
1553 if (($submit == 'Yes - Delete Now') && (is_numeric($params['update_id']))) {
1554 //delete all of the comments associated with this record
1555 $sql = 'delete from trongate_comments where target_table = :module and update_id = :update_id';
1556 $params['module'] = 'module_market_items';
1557 $this->model->query_bind($sql, $params);
1558
1559 //delete the record
1560 $this->model->delete($params['update_id'], 'module_market_items');
1561
1562 //set the flashdata
1563 $flash_msg = 'The record was successfully deleted';
1564 set_flashdata($flash_msg);
1565
1566 //redirect to the manage page
1567 redirect('module_market_items/manage');
1568 }
1569 }
1570
1571 function _get_limit() {
1572 if (isset($_SESSION['selected_per_page'])) {
1573 $limit = $this->per_page_options[$_SESSION['selected_per_page']];
1574 } else {
1575 $limit = $this->default_limit;
1576 }
1577
1578 return $limit;
1579 }
1580
1581 function _get_offset() {
1582 $page_num = segment(3);
1583
1584 if (!is_numeric($page_num)) {
1585 $page_num = 0;
1586 }
1587
1588 if ($page_num>1) {
1589 $offset = ($page_num-1)*$this->_get_limit();
1590 } else {
1591 $offset = 0;
1592 }
1593
1594 return $offset;
1595 }
1596
1597 function _get_selected_per_page() {
1598 if (!isset($_SESSION['selected_per_page'])) {
1599 $selected_per_page = $this->per_page_options[1];
1600 } else {
1601 $selected_per_page = $_SESSION['selected_per_page'];
1602 }
1603
1604 return $selected_per_page;
1605 }
1606
1607 function set_per_page($selected_index) {
1608 $this->module('trongate_security');
1609 $this->trongate_security->_make_sure_allowed();
1610
1611 if (!is_numeric($selected_index)) {
1612 $selected_index = $this->per_page_options[1];
1613 }
1614
1615 $_SESSION['selected_per_page'] = $selected_index;
1616 redirect('module_market_items/manage');
1617 }
1618
1619 function _get_data_from_db($update_id) {
1620 $record_obj = $this->model->get_where($update_id, 'module_market_items');
1621
1622 if ($record_obj == false) {
1623 $this->template('error_404');
1624 die();
1625 } else {
1626 $data = (array) $record_obj;
1627 return $data;
1628 }
1629 }
1630
1631 function _get_data_from_post() {
1632 $data['title'] = input('title', true);
1633 $data['description'] = input('description', true);
1634 $data['youtube_video_id'] = input('youtube_video_id', true);
1635 $data['price'] = input('price', true);
1636 $data['approved'] = input('approved', true);
1637 $data['live'] = input('live', true);
1638 $data['version'] = input('version', true);
1639 $data['mod_market_categories_id'] = input('mod_market_categories_id');
1640 $data['publishers_id'] = input('publishers_id');
1641 return $data;
1642 }
1643
1644 function _get_mod_market_categories_options($selected_key) {
1645 $this->module('module_relations');
1646 $options = $this->module_relations->_fetch_options($selected_key, 'module_market_items', 'mod_market_categories');
1647 return $options;
1648 }
1649
1650 function get_new_and_popular($limit, $no_display=null) {
1651 //fetch all of the new and popular items on the MM homepage
1652 if (!is_numeric($limit)) {
1653 http_response_code(400);
1654 echo 'Non numeric limit';
1655 die();
1656 }
1657
1658 $sql1 = 'SELECT
1659 module_market_items.*,
1660 publishers.publisher_name
1661 FROM
1662 module_market_items
1663 INNER JOIN
1664 publishers
1665 ON
1666 module_market_items.publishers_id = publishers.id
1667 ORDER BY estimated_downloads DESC
1668 LIMIT 0, '.$limit;
1669 $rows1 = $this->model->query($sql1, 'object');
1670
1671 $sql2 = 'SELECT
1672 module_market_items.*,
1673 publishers.publisher_name
1674 FROM
1675 module_market_items
1676 INNER JOIN
1677 publishers
1678 ON
1679 module_market_items.publishers_id = publishers.id
1680 ORDER BY date_created DESC
1681 LIMIT 0, '.$limit;
1682 $rows2 = $this->model->query($sql2, 'object');
1683
1684 $results = array_merge($rows1, $rows2);
1685
1686 foreach($results as $row_key=>$row_value) {
1687 $estimated_downloads = $row_value->estimated_downloads;
1688 $estimated_downloads = number_format($estimated_downloads, 2);
1689 $estimated_downloads = str_replace('.00', '', $estimated_downloads);
1690 $results[$row_key]->estimated_downloads = $estimated_downloads;
1691 }
1692
1693 if (isset($no_display)) {
1694 return $results;
1695 } else {
1696 http_response_code(200);
1697 echo json_encode($results);
1698 die();
1699 }
1700 }
1701
1702 function _get_publishers_options($selected_key) {
1703 $this->module('module_relations');
1704 $options = $this->module_relations->_fetch_options($selected_key, 'module_market_items', 'publishers');
1705 return $options;
1706 }
1707}