Subversion Repositories cheapmusic

Rev

Rev 103 | Details | Compare with Previous | 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
# class defines all keyword controller functions
24
class KeywordController extends Controller{
25
 
26
	# func to show keywords
27
	function listKeywords($info=''){
28
 
29
	    $userId = isLoggedIn();
30
		$websiteController = New WebsiteController();
31
 
32
		$urlParams = "";
33
		$websiteId = empty($info['website_id']) ? "" : intval($info['website_id']);
34
		$this->set('websiteList', $websiteController->__getAllWebsites($userId, true));
35
		$this->set('websiteId', $websiteId);
36
		if ($websiteId) {
37
		     $conditions = " and k.website_id=$websiteId";
38
		     $urlParams = "website_id=$websiteId";
39
		} else {
40
		    $conditions = "";
41
		}
42
 
43
		if (isset($info['status'])) {
44
		    if (($info['status']== 'active') || ($info['status']== 'inactive')) {
45
		        $statVal = ($info['status']=='active') ? 1 : 0;
46
		        $conditions .= " and k.status=$statVal";
47
		        $urlParams .= "&status=".$info['status'];
48
		    }
49
		} else {
50
		    $info['status'] = '';
51
		}
52
		$this->set('statVal', $info['status']);
53
 
54
		if (empty($info['keyword'])) {
55
		    $info['keyword'] =  '';
56
		} else {
57
		    $info['keyword'] = urldecode($info['keyword']);
58
		    $conditions .= " and k.name like '%".addslashes($info['keyword'])."%'";
59
		    $urlParams .= "&keyword=".urlencode($info['keyword']);
60
		}
61
		$this->set('keyword', $info['keyword']);
62
 
63
		$sql = "select k.*,w.name website,w.status webstatus from keywords k,websites w where k.website_id=w.id and w.status=1";
64
		$sql .= isAdmin() ? "" : " and w.user_id=$userId";
65
		$sql .= " $conditions order by k.name";
66
 
67
		# pagination setup
68
		$this->db->query($sql, true);
69
		$this->paging->setDivClass('pagingdiv');
70
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
71
		$pagingDiv = $this->paging->printPages('keywords.php', '', 'scriptDoLoad', 'content', $urlParams);
72
		$this->set('pagingDiv', $pagingDiv);
73
		$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
74
 
75
		# set keywords list
76
		$keywordList = $this->db->select($sql);
77
		$this->set('pageNo', $info['pageno']);
78
		$langCtrler = New LanguageController();
79
		$countryCtrler = New CountryController();
80
		foreach ($keywordList as $i => $keyInfo) {
81
			$info = $langCtrler->__getLanguageInfo($keyInfo['lang_code']);
82
			$keywordList[$i]['lang_name'] = $info['lang_name'];
83
			$info = $countryCtrler->__getCountryInfo($keyInfo['country_code']);
84
			$keywordList[$i]['country_name'] = $info['country_name'];
85
		}
86
		$this->set('list', $keywordList);
87
		$this->render('keyword/list');
88
	}
89
 
90
	# func to show keyword select box
91
	function showKeywordSelectBox($userId='', $websiteId='', $keywordId=''){
92
		$this->set('keywordList', $this->__getAllKeywords($userId, $websiteId, true));
93
		$this->set('keywordId', $keywordId);
94
		$this->render('keyword/keywordselectbox');
95
	}
96
 
97
	# func to change status
98
	function __changeStatus($keywordId, $status){
99
 
100
		$keywordId = intval($keywordId);
101
		$sql = "update keywords set status=$status where id=$keywordId";
102
		$this->db->query($sql);
103
	}
104
 
105
	# func to change crawled status of keyword
106
	function __changeCrawledStatus($status, $whereCond = '1=1') {
107
		$sql = "update keywords set crawled=$status where $whereCond";
108
		$this->db->query($sql);
109
	}
110
 
111
	# func to change status
112
	function __deleteKeyword($keywordId){
113
 
114
		$keywordId = intval($keywordId);
115
		$sql = "delete from keywords where id=$keywordId";
116
		$this->db->query($sql);
117
 
118
		// delete related data
119
		$sql = "delete sd.*, s.* from searchresults s, searchresultdetails sd where s.id=sd.searchresult_id and s.keyword_id=$keywordId";
120
		$this->db->query($sql);
121
	}
122
 
123
	function newKeyword(){
124
 
125
		$userId = isLoggedIn();
126
 
127
		# Validate keyword count
128
		if (!$this->validateKeywordCount($userId)) {
129
			$this->set('validationMsg', $this->spTextKeyword['Your keyword count already reached the limit']);
130
		}
131
 
132
		$websiteController = New WebsiteController();
133
		$this->set('websiteList', $websiteController->__getAllWebsites($userId, true));
134
		$langController = New LanguageController();
135
		$this->set('langList', $langController->__getAllLanguages());
136
		$this->set('langNull', true);
137
		$countryController = New CountryController();
138
		$this->set('countryList', $countryController->__getAllCountries());
139
		$this->set('countryNull', true);
140
		$seController = New SearchEngineController();
141
		$this->set('seList', $seController->__getAllSearchEngines());
142
		$this->render('keyword/new');
143
	}
144
 
145
	# create new keyword function
146
	function createKeyword($listInfo, $apiCall = false){
147
 
148
		$userId = isLoggedIn();
149
		$this->set('post', $listInfo);
150
		$errMsg['name'] = formatErrorMsg($this->validate->checkBlank($listInfo['name']));
151
		$errMsg['website_id'] = formatErrorMsg($this->validate->checkBlank($listInfo['website_id']));
152
		if (!is_array($listInfo['searchengines'])) $listInfo['searchengines'] = array();
153
		$errMsg['searchengines'] = formatErrorMsg($this->validate->checkBlank(implode('', $listInfo['searchengines'])));
154
		$statusVal = isset($listInfo['status']) ? intval($listInfo['status']) : 1;
155
		$seStr = is_array($listInfo['searchengines']) ? implode(':', $listInfo['searchengines']) : $listInfo['searchengines'];
156
 
157
		// verify the limit count for the user
158
		if (!$this->validate->flagErr) {
159
 
160
			// Get the website user id, if admin is logged in
161
			if (isAdmin() || $apiCall) {
162
				$websiteCtrler = new WebsiteController();
163
				$websiteInfo = $websiteCtrler->__getWebsiteInfo($listInfo['website_id']);
164
				$webUserId = $websiteInfo['user_id'];
165
			} else {
166
				$webUserId = $userId;
167
			}
168
 
169
			// Validate keyword count
170
			if (! $this->validateKeywordCount($webUserId)) {
171
				$validationMsg = $this->spTextKeyword['Your keyword count already reached the limit'];
172
				$this->set('validationMsg', $validationMsg);
173
				$errMsg['limit_error'] = $validationMsg;
174
				$this->validate->flagErr = true;
175
			}
176
 
177
			// Validate search engine count
178
			$seValdInfo = SearchEngineController::validateSearchEngineCount($webUserId, count($listInfo['searchengines']));
179
			if ($seValdInfo['error']) {
180
				$errMsg['searchengines'] = $seValdInfo['msg'];
181
				$this->validate->flagErr = true;
182
			}
183
 
184
		}
185
 
186
		// verify the form elements
187
		if(!$this->validate->flagErr){
188
			$keyword = addslashes(trim($listInfo['name']));
189
			if (!$this->__checkName($keyword, $listInfo['website_id'])) {
190
 
191
				$sql = "insert into keywords(name,lang_code,country_code,website_id,searchengines,status)
192
				values('$keyword', '".addslashes($listInfo['lang_code'])."', '".addslashes($listInfo['country_code'])."',
193
				".intval($listInfo['website_id']).", '".addslashes($seStr)."', $statusVal)";
194
				$this->db->query($sql);
195
 
196
				// if api call
197
				if ($apiCall) {
198
					return array('success', 'Successfully created keyword');
199
				} else {
200
					$this->listKeywords();
201
					exit;
202
				}
203
 
204
			}else{
205
				$errMsg['name'] = formatErrorMsg($this->spTextKeyword['Keyword already exist']);
206
			}
207
		}
208
 
209
		// if api call
210
		if ($apiCall) {
211
			return array('error', $errMsg);
212
		} else {
213
			$this->set('errMsg', $errMsg);
214
			$this->newKeyword();
215
		}
216
 
217
	}
218
 
219
	# function to import keywords
220
	function importKeywords(){
221
 
222
		$userId = isLoggedIn();
223
		$websiteController = New WebsiteController();
224
		$websiteList = $websiteController->__getAllWebsites($userId, true);
225
		$this->set('websiteList', $websiteList);
226
 
227
		if (empty($_POST['website_id'])) {
228
		    $listInfo['website_id'] = $websiteList[0]['id'];
229
		    $this->set('post', $listInfo);
230
		}
231
 
232
		$langController = New LanguageController();
233
		$this->set('langList', $langController->__getAllLanguages());
234
		$this->set('langNull', true);
235
 
236
		$countryController = New CountryController();
237
		$this->set('countryList', $countryController->__getAllCountries());
238
		$this->set('countryNull', true);
239
 
240
		$seController = New SearchEngineController();
241
		$this->set('seList', $seController->__getAllSearchEngines());
242
 
243
		// Check the user website count for validation
244
		if (!isAdmin()) {
245
			$this->setValidationMessageForLimit($userId);
246
		}
247
 
248
		$this->render('keyword/importkeywords');
249
	}
250
 
251
	# function to set validation message for the limit
252
	function setValidationMessageForLimit($userId) {
253
 
254
		// Check the user website count for validation
255
		$userTypeCtrlr = new UserTypeController();
256
		$userKeywordCount = count($this->__getAllKeywords($userId));
257
		$userTypeDetails = $userTypeCtrlr->getUserTypeSpecByUser($userId);
258
		$validCount = $userTypeDetails['keywordcount'] - $userKeywordCount;
259
		$validCount = $validCount > 0 ? $validCount : 0;
260
		$validationMsg = str_replace("[keywordcount]", "<b>$validCount</b>", $this->spTextKeyword['You can add only keywordcount keywords more']);
261
		$this->set('validationMsg', $validationMsg);
262
 
263
	}
264
 
265
	# function to import keywords to the seo panel
266
	function createImportedKeywords($listInfo){
267
 
268
		$userId = isLoggedIn();
269
		$this->set('post', $listInfo);
270
		$errMsg['keywords'] = formatErrorMsg($this->validate->checkBlank($listInfo['keywords']));
271
		if (!is_array($listInfo['searchengines'])) $listInfo['searchengines'] = array();
272
		$errMsg['searchengines'] = formatErrorMsg($this->validate->checkBlank(implode('', $listInfo['searchengines'])));
273
 
274
		// Check the user website count for validation
275
		if (isAdmin()) {
276
			$websiteCtrler = new WebsiteController();
277
			$websiteInfo = $websiteCtrler->__getWebsiteInfo($listInfo['website_id']);
278
			$webUserId = $websiteInfo['user_id'];
279
		} else {
280
			$webUserId = $userId;
281
		}
282
 
283
		// Validate search engine count
284
		$seValdInfo = SearchEngineController::validateSearchEngineCount($webUserId, count($listInfo['searchengines']));
285
		if ($seValdInfo['error']) {
286
			$errMsg['searchengines'] = $seValdInfo['msg'];
287
			$this->validate->flagErr = true;
288
		}
289
 
290
		if(!$this->validate->flagErr){
291
 
292
			$listInfo['website_id'] = intval($listInfo['website_id']);
293
			$keywords = explode(",", $listInfo['keywords']);
294
			$keyExist = false;
295
			$keywordList = array();
296
			foreach ($keywords as $i => $keyword) {
297
				$keyword = addslashes(trim($keyword));
298
				if ($this->__checkName($keyword, $listInfo['website_id'])) {
299
					$errMsg['keywords'] = formatErrorMsg($_SESSION['text']['common']['Keyword']." '<b>$keyword</b>' ". $_SESSION['text']['label']['already exist']);
300
					$keyExist = true;
301
					break;
302
				}
303
 
304
				// if keyword is not empty
305
				if (!empty($keyword)) {
306
					$keywordList[$i] = $keyword;
307
				}
308
			}
309
 
310
			// check whether keyword count exeeds the limit
311
			if (!$this->validateKeywordCount($webUserId, count($keywordList))) {
312
				$this->setValidationMessageForLimit($webUserId);
313
				$keyExist = true;
314
			}
315
 
316
			// if no error exists save keyword
317
			if (!$keyExist) {
318
				$listInfo['searchengines'] = is_array($listInfo['searchengines']) ? $listInfo['searchengines'] : array();
319
				foreach ($keywordList as $keyword) {
320
					$sql = "insert into keywords(name,lang_code,country_code,website_id,searchengines,status)
321
					values('$keyword','".addslashes($listInfo['lang_code'])."','".addslashes($listInfo['country_code'])."',
322
					".intval($listInfo['website_id']).",'".addslashes(implode(':', $listInfo['searchengines']))."',1)";
323
					$this->db->query($sql);
324
				}
325
 
326
				$this->listKeywords($listInfo);
327
				exit;
328
			}
329
 
330
		}
331
		$this->set('errMsg', $errMsg);
332
		$this->importKeywords();
333
	}
334
 
335
	function __checkName($name, $websiteId){
336
		$websiteId = intval($websiteId);
337
		$name = addslashes($name);
338
		$sql = "select id from keywords where name='$name' and website_id=$websiteId";
339
		$listInfo = $this->db->select($sql, true);
340
		return empty($listInfo['id']) ? false :  $listInfo['id'];
341
	}
342
 
343
	# func to get all keywords
344
	function __getAllKeywords($userId='', $websiteId='', $isAdminCheck=false, $orderByWeb=false, $orderByValue='ASC', $searchName = ''){
345
		$websiteId = intval($websiteId);
346
		$sql = "select k.*,w.name website,w.url weburl from keywords k,websites w where k.website_id=w.id and k.status=1";
347
 
348
		if(!$isAdminCheck || !isAdmin() ) {
349
			if(!empty($userId)) {
350
				$websiteCtrl = new WebsiteController();
351
				$sql .= $websiteCtrl->getWebsiteUserAccessCondition($userId);
352
			}
353
 
354
			//if(!empty($userId)) $sql .= " and w.user_id=$userId";
355
		}
356
 
357
		if(!empty($websiteId)) $sql .= " and k.website_id=$websiteId";
358
 
359
		if (!empty($searchName)) {
360
			$sql .= " and k.name like '%".addslashes($searchName)."%'";
361
		}
362
 
363
		$orderByValue = getOrderByVal($orderByValue);
364
		$sql .= $orderByWeb ? " order by w.id, k.name $orderByValue" : " order by k.name $orderByValue";
365
		$keywordList = $this->db->select($sql);
366
		return $keywordList;
367
	}
368
 
369
	function __getKeywordInfo($keywordId){
370
 
371
		$keywordId = intval($keywordId);
372
		$sql = "select * from keywords where id=$keywordId";
373
		$listInfo = $this->db->select($sql, true);
374
		return empty($listInfo['id']) ? false :  $listInfo;
375
	}
376
 
377
	function __getWebisteKeywords($websiteId, $searchInfo=[]) {
378
	    $cond = "website_id=".intval($websiteId);
379
	    $cond .= isset($searchInfo['status']) ? " and status=".intval($searchInfo['status']) : "";
380
	    $cond .= isset($searchInfo['search']) ? " and name like '%".addslashes($searchInfo['search'])."%'" : "";
381
	    return $this->dbHelper->getAllRows('keywords', $cond);
382
	}
383
 
384
	function editKeyword($keywordId, $listInfo=''){
385
 
386
		$userId = isLoggedIn();
387
		$websiteController = New WebsiteController();
388
		$this->set('websiteList', $websiteController->__getAllWebsites($userId, true));
389
		$langController = New LanguageController();
390
		$this->set('langList', $langController->__getAllLanguages());
391
		$this->set('langNull', true);
392
		$countryController = New CountryController();
393
		$this->set('countryList', $countryController->__getAllCountries());
394
		$this->set('countryNull', true);
395
		$seController = New SearchEngineController();
396
		$this->set('seList', $seController->__getAllSearchEngines());
397
		if(!empty($keywordId)){
398
			if(empty($listInfo)){
399
				$listInfo = $this->__getKeywordInfo($keywordId);
400
				$listInfo['oldName'] = $listInfo['name'];
401
				$listInfo['searchengines'] = explode(':', $listInfo['searchengines']);
402
			}
403
			$this->set('post', $listInfo);
404
			$this->render('keyword/edit');
405
			exit;
406
		}
407
		$this->listKeywords();
408
	}
409
 
410
	function updateKeyword($listInfo, $apiCall = false){
411
		$userId = isLoggedIn();
412
		$this->set('post', $listInfo);
413
 
414
		// Check the user website count for validation
415
		if (isAdmin()) {
416
			$websiteCtrler = new WebsiteController();
417
			$websiteInfo = $websiteCtrler->__getWebsiteInfo($listInfo['website_id']);
418
			$webUserId = $websiteInfo['user_id'];
419
		} else {
420
			$webUserId = $userId;
421
		}
422
 
423
		$errMsg['name'] = formatErrorMsg($this->validate->checkBlank($listInfo['name']));
424
		$errMsg['searchengines'] = formatErrorMsg($this->validate->checkBlank(implode('', $listInfo['searchengines'])));
425
		$seStr = is_array($listInfo['searchengines']) ? implode(':', $listInfo['searchengines']) : $listInfo['searchengines'];
426
		$statusVal = isset($listInfo['status']) ? "status = " . intval($listInfo['status']) ."," : "";
427
 
428
		// Validate search engine count
429
		$seValdInfo = SearchEngineController::validateSearchEngineCount($webUserId, count($listInfo['searchengines']));
430
		if ($seValdInfo['error']) {
431
			$errMsg['searchengines'] = $seValdInfo['msg'];
432
			$this->validate->flagErr = true;
433
		}
434
 
435
		//validate form
436
		if(!$this->validate->flagErr){
437
 
438
			$listInfo['website_id'] = intval($listInfo['website_id']);
439
			$listInfo['id'] = intval($listInfo['id']);
440
			$keyword = addslashes(trim($listInfo['name']));
441
			if($listInfo['name'] != $listInfo['oldName']){
442
				if ($this->__checkName($keyword, $listInfo['website_id'])) {
443
					$errMsg['name'] = formatErrorMsg($this->spTextKeyword['Keyword already exist']);
444
					$this->validate->flagErr = true;
445
				}
446
			}
447
 
448
			if (!$this->validate->flagErr) {
449
				$listInfo['searchengines'] = is_array($listInfo['searchengines']) ? $listInfo['searchengines'] : array();
450
				$sql = "update keywords set
451
						name = '$keyword',
452
						lang_code = '".addslashes($listInfo['lang_code'])."',
453
						country_code = '".addslashes($listInfo['country_code'])."',
454
						website_id = {$listInfo['website_id']},
455
						$statusVal
456
						searchengines = '".addslashes($seStr)."'
457
						where id={$listInfo['id']}";
458
				$this->db->query($sql);
459
 
460
				// if api call
461
				if ($apiCall) {
462
					return array('success', 'Successfully updated keyword');
463
				} else {
464
					$this->listKeywords();
465
					exit;
466
				}
467
 
468
			}
469
		}
470
 
471
		// if api call
472
		if ($apiCall) {
473
			return array('error', $errMsg);
474
		} else {
475
			$this->set('errMsg', $errMsg);
476
			$this->editKeyword($listInfo['id'], $listInfo);
477
		}
478
	}
479
 
480
	function showKeywordReports($keywordId) {
481
	    $keywordId = intval($keywordId);
482
		$this->checkUserIsObjectOwner($keywordId, 'keyword');
483
		echo "<script>scriptDoLoad('reports.php', 'content', 'keyword_id=$keywordId&rep=1')</script>";
484
	}
485
 
486
	// Function to check / validate the user type keyword count
487
	function validateKeywordCount($userId, $newCount = 1) {
488
		$userCtrler = new UserController();
489
 
490
		// if admin user id return true
491
		if ($userCtrler->isAdminUserId($userId)) {
492
			return true;
493
		}
494
 
495
		$userTypeCtrlr = new UserTypeController();
496
		$userKeywordCount = count($this->__getAllKeywords($userId));
497
		$userKeywordCount += $newCount;
498
		$userTypeDetails = $userTypeCtrlr->getUserTypeSpecByUser($userId);
499
 
500
		// if limit is set and not -1
501
		if (isset($userTypeDetails['keywordcount']) && $userTypeDetails['keywordcount'] >= 0) {
502
 
503
			// check whether count greater than limit
504
			if ($userKeywordCount <= $userTypeDetails['keywordcount']) {
505
				return true;
506
			} else {
507
				return false;
508
			}
509
 
510
		} else {
511
			return true;
512
		}
513
 
514
	}
515
 
516
	function getUserKeywordSearchEngineList($userId = "", $websiteId = false) {
517
 
518
		$seController = New SearchEngineController();
519
		$list = $seController->__getAllSearchEngines();
520
		$seList = array();
521
		$seDisplayList = array();
522
 
523
		foreach ($list as $listInfo) {
524
			$seList[$listInfo['id']] = $listInfo;
525
		}
526
 
527
		$websiteCtlr = new WebsiteController();
528
		$whereCond = "w.id=k.website_id and k.status=1 and w.status=1";
529
		$whereCond .= !empty($userId) ? $websiteCtlr->getWebsiteUserAccessCondition($userId) : "";
530
		$whereCond .= !empty($websiteId) ? " and w.id=".intval($websiteId) : "";
531
		$list = $this->dbHelper->getAllRows('keywords k, websites w', $whereCond, "distinct k.searchengines");
532
 
533
		// show only required search engines
534
		foreach ($list as $keywordInfo) {
535
			$keySeList = explode(":", $keywordInfo['searchengines']);
536
			foreach ($keySeList as $keySeId) {
155 - 537
				if (empty($seDisplayList[$keySeId]) && !empty($seList[$keySeId])) {
103 - 538
					$seDisplayList[$keySeId] = $seList[$keySeId];
539
				}
540
			}
541
		}
542
 
543
		return $seDisplayList;
544
 
545
	}
546
 
547
}
548
?>