· 6 years ago · Aug 14, 2019, 08:34 AM
1
2class BrowserInfoExtension extends SimpleExtension
3{
4 public $isMobile;
5 public $ip;
6 public $userAgent;
7 public $tableName;
8
9
10 public function getDisplayName()
11 {
12 return 'Popular Posts';
13 }
14
15 public function registerTwigFunctions()
16 {
17 $options = ['is_safe' => ['html'], 'safe' => true];
18 return [
19 'popcon_recordContentView' => ['twig_popcon_recordContentView', $options],
20 'popcon_getPopularContent' => ['twig_popcon_getPopularContent', $options],
21 ];
22 }
23
24 public function initialize() {
25 $dsn = "mysql:host=localhost;dbname=sonata";
26 $user = "root";
27 $passwd = "123";
28 $pdo = new \PDO($dsn, $user, $passwd);
29 $this->tableName = 'bolt_content_views';
30
31 $query = "CREATE TABLE IF NOT EXISTS `bolt_content_views`
32 (
33 `view_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
34 `content_id` INTEGER UNSIGNED NOT NULL,
35 `contenttype` VARCHAR(256) NOT NULL,
36 `view_ip_address` VARCHAR(45) NOT NULL,
37 `view_browser` VARCHAR(256),
38 `view_is_mobile` TINYINT(1) NOT NULL DEFAULT 0,
39 `view_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
40 PRIMARY KEY (`view_id`),
41 INDEX content_id (`content_id`),
42 INDEX contenttype (`contenttype`),
43 INDEX view_ip_address (`view_ip_address`),
44 INDEX view_browser (`view_browser`)
45 )";
46 $stmt = $this->$pdo->prepare($query);
47 $res = $stmt->execute();
48 $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
49 $this->ip = $_SERVER['REMOTE_ADDR'];
50 $this->isMobile = $this->popcon_isMobile();
51 }
52
53
54 public function twig_popcon_recordContentView($recordOb)
55 {
56 $dsn = "mysql:host=localhost;dbname=sonata";
57 $user = "root";
58 $passwd = "123";
59 $pdo = new \PDO($dsn, $user, $passwd);
60 try {
61
62 $stmt = $pdo->prepare("INSERT INTO `bolt_content_views` (`content_id`, `contenttype`, `view_ip_address`, `view_browser`, `view_is_mobile`)
63 VALUES(?, ?, ?, ?, ?)");
64 $stmt->bindValue(1, $recordOb->id);
65 $stmt->bindValue(2, $recordOb->contenttype['name']);
66 $stmt->bindValue(3, $this->ip);
67 $stmt->bindValue(4, $this->userAgent);
68 $stmt->bindValue(5, $this->isMobile);
69 $res = $stmt->execute();
70 } catch(PDOException $exception){
71 echo "Error: " . $exception->getMessage();
72 }
73 }
74
75 protected function popcon_isMobile()
76 {
77 $mobUAs = array(
78 '/iphone/i' => 'iPhone',
79 '/ipod/i' => 'iPod',
80 '/ipad/i' => 'iPad',
81 '/android/i' => 'Android',
82 '/blackberry/i' => 'BlackBerry',
83 '/BB/i' => 'BlackBerry',
84 '/webos/i' => 'Mobile'
85 );
86
87 //Return true if Mobile User Agent is detected
88 foreach($mobUAs as $key => $value)
89 {
90 if(preg_match($key, $_SERVER['HTTP_USER_AGENT']))
91 {
92 return TRUE;
93 }
94 }
95 //Otherwise return false..
96 return FALSE;
97 }
98
99 public function twig_popcon_getPopularContent($numResults = 10, $contenttype = NULL)
100 {
101 $dsn = "mysql:host=localhost;dbname=sonata";
102 $user = "root";
103 $passwd = "123";
104 $pdo = new \PDO($dsn, $user, $passwd);
105 $this->tableName = 'bolt_content_views';
106 $ctype = ($contenttype) ? $contenttype : '%';
107 $numRes = (is_numeric($numResults)) ? (int) $numResults : 10;
108 $query = "SELECT COUNT(pcv.view_id) AS `viewCnt`, pcv.contenttype, pcv.content_id
109 FROM `bolt_content_views` pcv
110 WHERE pcv.contenttype LIKE ?
111 GROUP BY pcv.contenttype, pcv.content_id ORDER BY COUNT(pcv.view_id) DESC LIMIT $numRes";
112 $stmt = $this->$pdo->prepare($query);
113 $stmt->bindValue(1, $ctype);
114 $han = $stmt->execute();
115 $res = $stmt->fetchAll();
116 return $res;
117 }
118
119
120}