Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/***************************************************************************
4
 *   Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in)  	           *
5
 *   sendtogeo@gmail.com   												   *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 *                                                                         *
12
 *   This program is distributed in the hope that it will be useful,       *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU General Public License for more details.                          *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU General Public License     *
18
 *   along with this program; if not, write to the                         *
19
 *   Free Software Foundation, Inc.,                                       *
20
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 ***************************************************************************/
22
 
23
include_once(SP_CTRLPATH."/components/review_base.ctrl.php");
24
 
25
# class defines all review manager controller functions
26
class ReviewManagerController extends ReviewBase{
27
 
28
    var $linkTable = "review_links";
29
    var $linkReportTable = "review_link_results";
30
    var $layout = "ajax";
31
    var $pageScriptPath = 'review.php';
32
    var $serviceList;
33
    var $colList;
34
    var $spTextRM;
35
 
36
    function __construct() {
37
        parent::__construct();
38
 
39
    	$this->set('pageScriptPath', $this->pageScriptPath);
40
    	$this->set( 'serviceList', $this->serviceList );
41
    	$this->set( 'pageNo', $_REQUEST['pageno']);
42
 
43
		$this->colList = array(
44
			'url' => $_SESSION['text']['common']['Url'],
45
			'reviews' => $_SESSION['text']['label']['Reviews'],
46
			'rating' => $_SESSION['text']['label']['Rating'],
47
		);
48
    }
49
 
50
    function showReviewLinks($searchInfo = '') {
51
    	$userId = isLoggedIn();
52
    	$this->set('searchInfo', $searchInfo);
53
    	$sql = "select l.*, w.name as website_name from $this->linkTable l, websites w where l.website_id=w.id";
54
 
55
    	if (!isAdmin()) {
56
    	    $sql .= " and w.user_id=$userId";
57
    	}
58
 
59
    	// search conditions
60
    	$sql .= !empty($searchInfo['name']) ? " and l.name like '%".addslashes($searchInfo['name'])."%'" : "";
61
    	$sql .= !empty($searchInfo['website_id']) ? " and l.website_id=".intval($searchInfo['website_id']) : "";
62
    	$sql .= !empty($searchInfo['type']) ? " and `type`='".addslashes($searchInfo['type'])."'" : "";
63
 
64
    	if (!empty($searchInfo['status'])) {
65
    	    $sql .= ($searchInfo['status'] == 'active') ? " and l.status=1" : " and l.status=0";
66
    	}
67
 
68
    	$webSiteCtrler = new WebsiteController();
69
    	$websiteList = $webSiteCtrler->__getAllWebsites($userId, true);
70
    	$this->set( 'websiteList', $websiteList );
71
 
72
    	// pagination setup
73
    	$this->db->query( $sql, true );
74
    	$this->paging->setDivClass( 'pagingdiv' );
75
    	$this->paging->loadPaging( $this->db->noRows, SP_PAGINGNO );
76
    	$pagingDiv = $this->paging->printPages( $this->pageScriptPath, 'searchForm', 'scriptDoLoadPost', 'content', '' );
77
    	$this->set( 'pagingDiv', $pagingDiv );
78
    	$sql .= " limit " . $this->paging->start . "," . $this->paging->per_page;
79
 
80
    	$linkList = $this->db->select( $sql );
81
    	$this->set( 'list', $linkList );
82
    	$this->render( 'review/show_review_links');
83
    }
84
 
85
    function __checkName($name, $websiteId, $linkId = false){
86
        $whereCond = "name='".addslashes($name)."'";
87
        $whereCond .= " and website_id='".intval($websiteId)."'";
88
        $whereCond .= !empty($linkId) ? " and id!=".intval($linkId) : "";
89
        $listInfo = $this->dbHelper->getRow($this->linkTable, $whereCond);
90
        return empty($listInfo['id']) ? false :  $listInfo['id'];
91
    }
92
 
93
    function __checkUrl($url, $websiteId, $linkId = false){
94
        $whereCond = "url='".addslashes($url)."'";
95
        $whereCond .= " and website_id=".intval($websiteId);
96
        $whereCond .= !empty($linkId) ? " and id!=".intval($linkId) : "";
97
        $listInfo = $this->dbHelper->getRow($this->linkTable, $whereCond);
98
        return empty($listInfo['id']) ? false :  $listInfo['id'];
99
    }
100
 
101
    function validateReviewLink($listInfo) {
102
        $errMsg = [];
103
        $errMsg['name'] = formatErrorMsg($this->validate->checkBlank($listInfo['name']));
104
        $errMsg['url'] = formatErrorMsg($this->validate->checkBlank($listInfo['url']));
105
        $errMsg['website_id'] = formatErrorMsg($this->validate->checkBlank($listInfo['website_id']));
106
        $errMsg['type'] = formatErrorMsg($this->validate->checkBlank($listInfo['type']));
107
 
108
        if(!$this->validate->flagErr){
109
            if ($this->__checkName($listInfo['name'], $listInfo['website_id'], $listInfo['id'])) {
110
                $errMsg['name'] = formatErrorMsg($_SESSION['text']['label']['already exist']);
111
                $this->validate->flagErr = true;
112
            }
113
        }
114
 
115
        if(!$this->validate->flagErr){
116
            if ($this->__checkUrl($listInfo['url'], $listInfo['website_id'], $listInfo['id'])) {
117
                $errMsg['url'] = formatErrorMsg($_SESSION['text']['label']['already exist']);
118
                $this->validate->flagErr = true;
119
            }
120
        }
121
 
122
        if(!$this->validate->flagErr){
123
            if (!stristr($listInfo['url'], $listInfo['type'])) {
124
                $errMsg['url'] = formatErrorMsg($_SESSION['text']['common']["Invalid value"]);
125
                $this->validate->flagErr = true;
126
            }
127
        }
128
 
129
        // Validate link count
130
        if(!$this->validate->flagErr){
131
            $websiteCtrl = new WebsiteController();
132
            $websiteInfo = $websiteCtrl->__getWebsiteInfo($listInfo['website_id']);
133
            $newCount = !empty($listInfo['id']) ? 0 : 1;
134
            if (! $this->validateReviewLinkCount($websiteInfo['user_id'], $newCount)) {
135
                $this->set('validationMsg', $this->spTextSMC['Your review link count already reached the limit']);
136
                $this->validate->flagErr = true;
137
            }
138
        }
139
 
140
        return $errMsg;
141
    }
142
 
143
    // Function to check / validate the user type review count
144
    function validateReviewLinkCount($userId, $newCount = 1) {
145
        $userCtrler = new UserController();
146
 
147
        // if admin user id return true
148
        if ($userCtrler->isAdminUserId($userId)) {
149
            return true;
150
        }
151
 
152
        $userTypeCtrlr = new UserTypeController();
153
        $userTypeDetails = $userTypeCtrlr->getUserTypeSpecByUser($userId);
154
 
155
        $whereCond = "l.website_id=w.id and w.user_id=".intval($userId);
156
        $existingInfo = $this->dbHelper->getRow("$this->linkTable l, websites w", $whereCond, "count(*) count");
157
        $userSMLinkCount = $existingInfo['count'];
158
        $userSMLinkCount += $newCount;
159
 
160
        // if limit is set and not -1
161
        if (isset($userTypeDetails['review_link_count']) && $userTypeDetails['review_link_count'] >= 0) {
162
 
163
            // check whether count greater than limit
164
            if ($userSMLinkCount <= $userTypeDetails['review_link_count']) {
165
                return true;
166
            } else {
167
                return false;
168
            }
169
 
170
        } else {
171
            return true;
172
        }
173
 
174
    }
175
 
176
    function newReviewLink($info = '') {
177
        $userId = isLoggedIn();
178
        $this->set('post', $info);
179
        $webSiteCtrler = new WebsiteController();
180
        $websiteList = $webSiteCtrler->__getAllWebsites($userId, true);
181
        $this->set( 'websiteList', $websiteList );
182
        $this->set('editAction', 'createReviewLink');
183
        $this->render( 'review/edit_review_link');
184
    }
185
 
186
    function createReviewLink($listInfo = '') {
187
 
188
        $errMsg = $this->validateReviewLink($listInfo);
189
 
190
        // if no error occured
191
        if (!$this->validate->flagErr) {
192
            $dataList = [
193
                'name' => $listInfo['name'],
194
                'url' => addHttpToUrl($listInfo['url']),
195
                'type' => $listInfo['type'],
196
                'website_id|int' => $listInfo['website_id'],
197
            ];
198
            $this->dbHelper->insertRow($this->linkTable, $dataList);
199
            $this->showReviewLinks(['name' => $listInfo['name']]);
200
            exit;
201
        }
202
 
203
        $this->set('errMsg', $errMsg);
204
        $this->newReviewLink($listInfo);
205
 
206
    }
207
 
208
    function editReviewLink($linkId, $listInfo = '') {
209
 
210
        if (!empty($linkId)) {
211
            $userId = isLoggedIn();
212
            $webSiteCtrler = new WebsiteController();
213
            $websiteList = $webSiteCtrler->__getAllWebsites($userId, true);
214
            $this->set( 'websiteList', $websiteList );
215
 
216
            if(empty($listInfo)){
217
                $listInfo = $this->__getReviewLinkInfo($linkId);
218
            }
219
 
220
            $this->set('post', $listInfo);
221
            $this->set('editAction', 'updateReviewLink');
222
            $this->render( 'review/edit_review_link');
223
        }
224
 
225
    }
226
 
227
    function updateReviewLink($listInfo) {
228
        $this->set('post', $listInfo);
229
        $errMsg = $this->validateReviewLink($listInfo);
230
 
231
        if (!$this->validate->flagErr) {
232
            $dataList = [
233
                'name' => $listInfo['name'],
234
                'url' => addHttpToUrl($listInfo['url']),
235
                'type' => $listInfo['type'],
236
                'website_id|int' => $listInfo['website_id'],
237
            ];
238
            $this->dbHelper->updateRow($this->linkTable, $dataList, "id=".intval($listInfo['id']));
239
            $this->showReviewLinks(['name' => $listInfo['name']]);
240
            exit;
241
        }
242
 
243
        $this->set('errMsg', $errMsg);
244
        $this->editReviewLink($listInfo['id'], $listInfo);
245
    }
246
 
247
    function deleteReviewLink($linkId) {
248
        $this->dbHelper->deleteRows($this->linkTable, "id=" . intval($linkId));
249
        $this->showReviewLinks();
250
    }
251
 
252
    function __changeStatus($linkId, $status){
253
        $linkId = intval($linkId);
254
        $this->dbHelper->updateRow($this->linkTable, ['status|int' => $status], "id=$linkId");
255
    }
256
 
257
    function __getReviewLinkInfo($linkId) {
258
        $whereCond = "id=".intval($linkId);
259
        $info = $this->dbHelper->getRow($this->linkTable, $whereCond);
260
        return $info;
261
    }
262
 
263
    function verifyActionAllowed($linkId) {
264
        $allowed = true;
265
 
266
        // if not admin, check the permissions
267
        if (!isAdmin()) {
268
            $userId = isLoggedIn();
269
            $linkInfo = $this->__getReviewLinkInfo($linkId);
270
            $webSiteCtrler = new WebsiteController();
271
            $webSiteInfo = $webSiteCtrler->__getWebsiteInfo($linkInfo['website_id']);
272
            $allowed = ($userId == $webSiteInfo['user_id']) ? true : false;
273
        }
274
 
275
        if (!$allowed) {
276
            showErrorMsg($_SESSION['text']['label']['Access denied']);
277
        }
278
 
279
    }
280
 
281
	function viewQuickChecker($info='') {
282
		$this->render('review/quick_checker');
283
	}
284
 
285
	function doQuickChecker($listInfo = '') {
286
 
287
		if (!stristr($listInfo['url'], $listInfo['type'])) {
288
			$errorMsg = formatErrorMsg($_SESSION['text']['common']["Invalid value"]);
289
			$this->validate->flagErr = true;
290
		}
291
 
292
		// if no error occured find review details
293
		if (!$this->validate->flagErr) {
294
			$smLink = addHttpToUrl($listInfo['url']);
295
			$result = $this->getReviewDetails($listInfo['type'], $smLink);
296
 
297
			// if call is success
298
			if ($result['status']) {
299
				$this->set('smType', $listInfo['type']);
300
				$this->set('smLink', $smLink);
301
				$this->set('statInfo', $result);
302
				$this->render('review/quick_checker_results');
303
				exit;
304
			} else {
305
				$errorMsg = $result['msg'];
306
			}
307
 
308
		}
309
 
310
		$errorMsg = !empty($errorMsg) ? $errorMsg : $_SESSION['text']['common']['Internal error occured'];
311
		showErrorMsg($errorMsg);
312
 
313
	}
314
 
315
	function getReviewDetails($smType, $smLink) {
316
		$result = ['status' => 0, 'reviews' => 0, 'rating' => 0, 'msg' => $_SESSION['text']['common']['Internal error occured']];
317
		$smInfo = $this->serviceList[$smType];
318
 
319
		if (!empty($smInfo) && !empty($smLink)) {
320
 
321
			// if params needs to be added with url
322
			if (!empty($smInfo['url_part'])) {
323
				$smLink .= stristr($smLink, '?') ? str_replace("?", "&", $smInfo['url_part']) : $smInfo['url_part'];
324
			}
325
 
326
			$smContentInfo = $this->spider->getContent($smLink);
327
 
328
			// testing val
329
			/*$myfile = fopen(SP_TMPPATH . "/gbusiness.html", "w") or die("Unable to open file!");
330
			fwrite($myfile, $smContentInfo['page']);
331
			fclose($myfile);
332
			exit;
333
 
334
			$smContentInfo = [];
335
			$myfile = fopen(SP_TMPPATH . "/gbusiness.html", "r") or die("Unable to open file!");
336
			$smContentInfo['page'] = fread($myfile,filesize(SP_TMPPATH . "/gbusiness.html"));
337
			fclose($myfile);*/
338
 
339
			if (!empty($smContentInfo['page'])) {
340
			    $matches = [];
341
 
342
				// find reviews
343
				if (!empty($smInfo['regex']['reviews'])) {
344
				    preg_match($smInfo['regex']['reviews'], $smContentInfo['page'], $matches);
345
 
346
					if (!empty($matches[1])) {
347
						$result['status'] = 1;
348
						$result['reviews'] = formatNumber($matches[1]);
349
					}
350
				}
351
 
352
				// find rating
353
				if (!empty($smInfo['regex']['rating'])) {
354
					preg_match($smInfo['regex']['rating'], $smContentInfo['page'], $matches);
355
 
356
					if (!empty($matches[1])) {
357
						$result['status'] = 1;
358
						$result['rating'] = formatNumber($matches[1]);
359
					}
360
				}
361
 
362
			} else {
363
				$result['msg'] = $smContentInfo['errmsg'];
364
			}
365
 
366
		}
367
 
368
		return $result;
369
 
370
	}
371
 
372
	/*
373
	 * function to get all links with out reports for a day
374
	 */
375
	function getAllLinksWithOutReports($websiteId, $date) {
376
		$websiteId = intval($websiteId);
377
		$date = addslashes($date);
378
		$sql = "select link.*, lr.id result_id from review_links link left join
379
			review_link_results lr on (link.id=lr.review_link_id and lr.report_date='$date')
380
			where link.status=1 and link.website_id=$websiteId and lr.id is NULL";
381
 
382
		$linkList = $this->db->select($sql);
383
		return $linkList;
384
 
385
	}
386
 
387
	function saveReviewLinkResults($linkId, $linkInfo) {
388
		$dataList = [
389
			'review_link_id|int' => $linkId,
390
			'reviews|int' => $linkInfo['reviews'],
391
			'rating|float' => $linkInfo['rating'],
392
			'report_date' => date('Y-m-d'),
393
		];
394
 
395
		$this->dbHelper->insertRow($this->linkReportTable, $dataList);
396
	}
397
 
398
	/*
399
	 * func to show report summary
400
	 */
401
	function viewReportSummary($searchInfo = '', $summaryPage = false, $cronUserId=false) {
402
 
403
		$userId = !empty($cronUserId) ? $cronUserId : isLoggedIn();
404
		$this->set('summaryPage', $summaryPage);
405
		$this->set('searchInfo', $searchInfo);
406
		$this->set('cronUserId', $cronUserId);
407
 
408
		$exportVersion = false;
409
		switch($searchInfo['doc_type']){
410
 
411
			case "export":
412
				$exportVersion = true;
413
				$exportContent = "";
414
				break;
415
 
416
			case "pdf":
417
				$this->set('pdfVersion', true);
418
				break;
419
 
420
			case "print":
421
				$this->set('printVersion', true);
422
				break;
423
		}
424
 
425
		$fromTime = !empty($searchInfo['from_time']) ? addslashes($searchInfo['from_time']) : date('Y-m-d', strtotime('-1 days'));
426
		$toTime = !empty($searchInfo['to_time']) ? addslashes($searchInfo['to_time']) : date('Y-m-d');
427
		$this->set('fromTime', $fromTime);
428
		$this->set('toTime', $toTime);
429
 
430
		$websiteController = New WebsiteController();
431
		$wList = $websiteController->__getAllWebsites($userId, true);
432
		$websiteList = [];
433
		foreach ($wList as $wInfo) $websiteList[$wInfo['id']] = $wInfo;
434
		$websiteList = count($websiteList) ? $websiteList : array(0);
435
		$this->set('websiteList', $websiteList);
436
		$websiteId = intval($searchInfo['website_id']);
437
		$this->set('websiteId', $websiteId);
438
 
439
		// to find order col
440
		if (!empty($searchInfo['order_col'])) {
441
			$orderCol = $searchInfo['order_col'];
442
			$orderVal = getOrderByVal($searchInfo['order_val']);
443
		} else {
444
			$orderCol = "rating";
445
			$orderVal = 'DESC';
446
		}
447
 
448
		$this->set('orderCol', $orderCol);
449
		$this->set('orderVal', $orderVal);
450
		$scriptName = $summaryPage ? "archive.php" : $this->pageScriptPath;
451
		$scriptPath = SP_WEBPATH . "/$scriptName?sec=reportSummary&website_id=$websiteId";
452
		$scriptPath .= "&from_time=$fromTime&to_time=$toTime&search_name=" . $searchInfo['search_name'] . "&type=" . $searchInfo['type'];
453
		$scriptPath .= "&order_col=$orderCol&order_val=$orderVal&report_type=social-media-reports";
454
 
455
		// set website id to get exact keywords of a user
456
		if (!empty($websiteId)) {
457
			$conditions = " and sml.website_id=$websiteId";
458
		} else {
459
			$conditions = " and sml.website_id in (".implode(',', array_keys($websiteList)).")";
460
		}
461
 
462
		$conditions .= !empty($searchInfo['search_name']) ? " and sml.url like '%".addslashes($searchInfo['search_name'])."%'" : "";
463
		$conditions .= !empty($searchInfo['type']) ? " and sml.type='".addslashes($searchInfo['type'])."'" : "";
464
 
465
		$subSql = "select [cols] from $this->linkTable sml, $this->linkReportTable r where sml.id=r.review_link_id
466
		and sml.status=1 $conditions and r.report_date='$toTime'";
467
 
468
		$sql = "
469
		(" . str_replace("[cols]", "sml.id,sml.url,sml.website_id,sml.type,r.reviews,r.rating", $subSql) . ")
470
			UNION
471
			(select sml.id,sml.url,sml.website_id,sml.type,0,0 from $this->linkTable sml where sml.status=1 $conditions
472
			and sml.id not in (". str_replace("[cols]", "distinct(sml.id)", $subSql) ."))
473
		order by " . addslashes($orderCol) . " " . addslashes($orderVal);
474
 
475
		if ($orderCol != 'url') $sql .= ", url";
476
 
477
		// pagination setup, if not from cron job email send function, pdf and export action
478
		if (!in_array($searchInfo['doc_type'], array("pdf", "export"))) {
479
			$this->db->query($sql, true);
480
			$this->paging->setDivClass('pagingdiv');
481
			$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
482
			$pagingDiv = $this->paging->printPages($scriptPath, '', 'scriptDoLoad', 'content', "");
483
			$this->set('pagingDiv', $pagingDiv);
484
			$this->set('pageNo', $searchInfo['pageno']);
485
			$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
486
		}
487
 
488
		# set report list
489
		$baseReportList = $this->db->select($sql);
490
		$this->set('baseReportList', $baseReportList);
491
		$this->set('colList', $this->colList);
492
 
493
		// if keywords existing
494
		if (!empty($baseReportList)) {
495
 
496
			$keywordIdList = array();
497
			foreach ($baseReportList as $info) {
498
				$keywordIdList[] = $info['id'];
499
			}
500
 
501
			$sql = "select sml.id,sml.url,sml.website_id,sml.type,r.rating,r.reviews
502
			from $this->linkTable sml, $this->linkReportTable r where sml.id=r.review_link_id
503
			and sml.status=1 $conditions and r.report_date='$fromTime'";
504
			$sql .= " and sml.id in(" . implode(",", $keywordIdList) . ")";
505
			$reportList = $this->db->select($sql);
506
			$compareReportList = array();
507
 
508
			foreach ($reportList as $info) {
509
				$compareReportList[$info['id']] = $info;
510
			}
511
 
512
			$this->set('compareReportList', $compareReportList);
513
 
514
		}
515
 
516
		if ($exportVersion) {
517
			$spText = $_SESSION['text'];
518
			$reportHeading =  $this->spTextTools['Review Report Summary']."($fromTime - $toTime)";
519
			$exportContent .= createExportContent( array('', $reportHeading, ''));
520
			$exportContent .= createExportContent( array());
521
			$headList = array($spText['common']['Website'], $spText['common']['Url']);
522
 
523
			$pTxt = str_replace("-", "/", substr($fromTime, -5));
524
			$cTxt = str_replace("-", "/", substr($toTime, -5));
525
			foreach ($this->colList as $colKey => $colLabel) {
526
				if ($colKey == 'url') continue;
527
				$headList[] = $colLabel . "($pTxt)";
528
				$headList[] = $colLabel . "($cTxt)";
529
				$headList[] = $colLabel . "(+/-)";
530
			}
531
 
532
			$exportContent .= createExportContent($headList);
533
			foreach($baseReportList as $listInfo){
534
 
535
				$valueList = array($websiteList[$listInfo['website_id']]['url'], $listInfo['url']);
536
				foreach ($this->colList as $colName => $colVal) {
537
					if ($colName == 'url') continue;
538
 
539
					$currRank = isset($listInfo[$colName]) ? $listInfo[$colName] : 0;
540
					$prevRank = isset($compareReportList[$listInfo['id']][$colName]) ? $compareReportList[$listInfo['id']][$colName] : 0;
541
					$rankDiff = "";
542
 
543
					// if both ranks are existing
544
					if ($prevRank != '' && $currRank != '') {
545
						$rankDiff = $currRank - $prevRank;
546
					}
547
 
548
					$valueList[] = $prevRank;
549
					$valueList[] = $currRank;
550
					$valueList[] = $rankDiff;
551
				}
552
 
553
				$exportContent .= createExportContent( $valueList);
554
			}
555
 
556
			if ($summaryPage) {
557
				return $exportContent;
558
			} else {
559
				exportToCsv('review_report_summary', $exportContent);
560
			}
561
 
562
		} else {
563
 
564
			// if pdf export
565
			if ($summaryPage) {
566
				return $this->getViewContent('review/review_report_summary');
567
			} else {
568
				// if pdf export
569
				if ($searchInfo['doc_type'] == "pdf") {
570
					exportToPdf($this->getViewContent('review/review_report_summary'), "review_report_summary_$fromTime-$toTime.pdf");
571
				} else {
572
					$this->set('searchInfo', $searchInfo);
573
					$this->render('review/review_report_summary');
574
				}
575
			}
576
 
577
		}
578
	}
579
 
580
	function __getReviewLinks($whereCond = false) {
581
	    $linkList = $this->dbHelper->getAllRows($this->linkTable, $whereCond);
582
	    return !empty($linkList) ? $linkList : false;
583
	}
584
 
585
	// func to show detailed reports
586
	function viewDetailedReports($searchInfo = '') {
587
 
588
		$userId = isLoggedIn();
589
 
590
		if (!empty ($searchInfo['from_time'])) {
591
			$fromTimeDate = addslashes($searchInfo['from_time']);
592
		} else {
593
			$fromTimeDate = date('Y-m-d', strtotime('-15 days'));
594
		}
595
 
596
		if (!empty ($searchInfo['to_time'])) {
597
			$toTimeDate = addslashes($searchInfo['to_time']);
598
		} else {
599
			$toTimeDate = date('Y-m-d');
600
		}
601
 
602
		$this->set('fromTime', $fromTimeDate);
603
		$this->set('toTime', $toTimeDate);
604
 
605
	    if(!empty($searchInfo['link_id']) && !empty($searchInfo['rep'])){
606
			$searchInfo['link_id'] = intval($searchInfo['link_id']);
607
			$linkInfo = $this->__getReviewLinkInfo($searchInfo['link_id']);
608
			$searchInfo['website_id'] = $linkInfo['website_id'];
609
		}
610
 
611
		$websiteController = New WebsiteController();
612
		$websiteList = $websiteController->__getAllWebsites($userId, true);
613
		$this->set('websiteList', $websiteList);
614
		$websiteId = empty ($searchInfo['website_id']) ? $websiteList[0]['id'] : intval($searchInfo['website_id']);
615
		$this->set('websiteId', $websiteId);
616
 
617
		$linkList = $this->__getReviewLinks("website_id=$websiteId and status=1 order by name");
618
		$this->set('linkList', $linkList);
619
		$linkId = empty($searchInfo['link_id']) ? $linkList[0]['id'] : intval($searchInfo['link_id']);
620
		$this->set('linkId', $linkId);
621
 
622
		$list = [];
623
		if (!empty($linkId)) {
624
 
625
    		$sql = "select s.* from $this->linkReportTable s
626
    		where report_date>='$fromTimeDate' and report_date<='$toTimeDate' and s.review_link_id=$linkId
627
    		order by s.report_date";
628
    		$reportList = $this->db->select($sql);
629
 
630
    		$colList = array_keys($this->colList);
631
    		array_shift($colList);
632
    		foreach ($colList as $col) $prevRank[$col] = 0;
633
 
634
    		# loop through rank
635
    		foreach ($reportList as $key => $repInfo) {
636
 
637
    			// if not the first row, find differences in rank
638
    			if ($key)  {
639
 
640
    				foreach ($colList as $col) $rankDiff[$col] = '';
641
 
642
    				foreach ($colList as $col) {
643
    					$rankDiff[$col] = round($repInfo[$col] - $prevRank[$col], 2);
644
    					if (empty($rankDiff[$col])) continue;
645
 
646
    					if ($col == "average_position" ) $rankDiff[$col] = $rankDiff[$col] * -1;
647
    					$rankClass = ($rankDiff[$col] > 0) ? 'green' : 'red';
648
 
649
    					$rankDiff[$col] = "<font class='$rankClass'>($rankDiff[$col])</font>";
650
    					$reportList[$key]['rank_diff_'.$col] = empty($rankDiff[$col]) ? '' : $rankDiff[$col];
651
    				}
652
 
653
    			}
654
 
655
    			foreach ($colList as $col) $prevRank[$col] = $repInfo[$col];
656
 
657
    		}
658
 
659
    		$list = array_reverse($reportList, true);
660
		}
661
 
662
		$this->set('list', $list);
663
		$this->render('review/review_reports');
664
 
665
	}
666
 
667
	// func to show review link select box
668
	function showReviewLinkSelectBox($websiteId, $linkId = ""){
669
	    $websiteId = intval($websiteId);
670
	    $this->set('linkList', $this->__getReviewLinks("website_id=$websiteId and status=1 order by name"));
671
	    $this->set('linkId', $linkId);
672
	    $this->render('review/review_link_select_box');
673
	}
674
 
675
	// func to show link search reports in graph
676
	function viewGraphReports($searchInfo = '') {
677
 
678
	    $userId = isLoggedIn();
679
 
680
	    if (!empty ($searchInfo['from_time'])) {
681
	        $fromTimeDate = addslashes($searchInfo['from_time']);
682
	    } else {
683
	        $fromTimeDate = date('Y-m-d', strtotime('-15 days'));
684
	    }
685
 
686
	    if (!empty ($searchInfo['to_time'])) {
687
	        $toTimeDate = addslashes($searchInfo['to_time']);
688
	    } else {
689
	        $toTimeDate = date('Y-m-d');
690
	    }
691
 
692
	    $this->set('fromTime', $fromTimeDate);
693
	    $this->set('toTime', $toTimeDate);
694
 
695
	    if(!empty($searchInfo['link_id']) && !empty($searchInfo['rep'])){
696
	        $searchInfo['link_id'] = intval($searchInfo['link_id']);
697
	        $linkInfo = $this->__getReviewLinkInfo($searchInfo['link_id']);
698
	        $searchInfo['website_id'] = $linkInfo['website_id'];
699
	    }
700
 
701
	    $websiteController = New WebsiteController();
702
	    $websiteList = $websiteController->__getAllWebsites($userId, true);
703
	    $this->set('websiteList', $websiteList);
704
	    $websiteId = empty ($searchInfo['website_id']) ? $websiteList[0]['id'] : intval($searchInfo['website_id']);
705
	    $this->set('websiteId', $websiteId);
706
 
707
	    $linkList = $this->__getReviewLinks("website_id=$websiteId and status=1 order by name");
708
	    $this->set('linkList', $linkList);
709
	    $linkId = empty($searchInfo['link_id']) ? $linkList[0]['id'] : intval($searchInfo['link_id']);
710
	    $this->set('linkId', $linkId);
711
 
712
	    // if reports not empty
713
	    $colList = $this->colList;
714
	    array_shift($colList);
715
	    $this->set('colList', $colList);
716
	    $this->set('searchInfo', $searchInfo);
717
 
718
	    $graphContent = showErrorMsg($_SESSION['text']['common']['No Records Found'], false, true);
719
	    if (!empty($linkId)) {
720
 
721
	        $sql = "select s.* from $this->linkReportTable s
722
    		where report_date>='$fromTimeDate' and report_date<='$toTimeDate'  and s.review_link_id=$linkId
723
    		order by s.report_date";
724
	        $reportList = $this->db->select($sql);
725
 
726
    		$graphColList = array();
727
    		if (!empty($searchInfo['attr_type'])) {
728
    			$graphColList[$searchInfo['attr_type']] = $colList[$searchInfo['attr_type']];
729
    		} else {
730
    			//array_pop($colList);
731
    			$graphColList = $colList;
732
    		}
733
 
734
    		if (!empty($reportList)) {
735
 
736
    			$dataArr = "['Date', '" . implode("', '", array_values($graphColList)) . "']";
737
 
738
    			// loop through data list
739
    			foreach ($reportList as $dataInfo) {
740
 
741
    				$valStr = "";
742
    				foreach ($graphColList as $seId => $seVal) {
743
    					$valStr .= ", ";
744
    					$valStr .= !empty($dataInfo[$seId]) ? $dataInfo[$seId] : 0;
745
    				}
746
 
747
    				$dataArr .= ", ['{$dataInfo['report_date']}' $valStr]";
748
    			}
749
 
750
    			$this->set('dataArr', $dataArr);
751
    			$this->set('graphTitle', $this->spTextTools['Graphical Reports']);
752
    			$graphContent = $this->getViewContent('report/graph');
753
    		} else {
754
    			$graphContent = showErrorMsg($_SESSION['text']['common']['No Records Found'], false, true);
755
    		}
756
 
757
	    }
758
 
759
		// get graph content
760
		$this->set('graphContent', $graphContent);
761
		$this->render('review/graphicalreport');
762
 
763
	}
764
 
765
}
766
?>