Content::getContentBySlug()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 22
loc 22
ccs 3
cts 3
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 3
crap 2
1
<?php
2
namespace Chp\TextContent;
3
4 1
include_once(__DIR__ . '/../../app/config/text-content.php');
5
6
/**
7
 * Model for text content
8
 * Made by Rasmus Berg (c) 2014-2017
9
 *
10
 * @property  object  $db         PDO database-handler class
11
 */
12
Class Content extends \Anax\MVC\CDatabaseModel
13
{
14
  /**
15
   * Get "all" content from database (limit by per page)
16
   *
17
   * @param  	int	   		$page				Wich page paging are at
18
   * @param  	int	   		$perPage		Contents per page
19
   * @param  	boolean		$publish 		If it needs to be publish
20
   * @return 	object   							With "all" database content data
21
   */
22 1 View Code Duplication
  public function getAllContent($page = null, $perPage = null, $publish = true){
23
    $now    = date('Y-m-d H:i:s');
24
    $params = array();
25
26
    $this->db->select("c.*")
27
         ->from("content AS c")
28
    //   ->join("user AS u", "c.author = u.id")
29
         ->where("c.`deleted` IS NULL");
30
31
    if($publish){
32
      $this->db->andWhere("c.published <= ?");
33 1
      $params[] = $now;
34
    }
35
36
    $this->db->orderBy('c.published DESC, c.created DESC');
37
38
    /*if($perPage){
39
      $this->db->limit($perPage);
40
41
      if($page){
42
        $offset = $this->getOffset($perPage, $page);
43
        $this->db->offset($offset);
44
      }
45
    }*/
46
47
    $this->db->execute($params);
48
49
    $this->db->setFetchModeClass(__CLASS__);
50
    return $this->db->fetchAll();
51
  }
52
	
53
  /**
54
   * Get "all" content of one type from database (limit by per page)
55
   *
56
   * @param  	boolean 	$publish 	If it needs to be publish
57
   * @param  	string  	$type			Wich type of content ex. blog, page etc.
58
   * @param  	int	      $page			Wich page paging are at
59
   * @param  	int       $perPage	Contents per page
60
   * @return 	object   						With "all" database content data
61
   */
62 2 View Code Duplication
  public function getAllContentOfType($type, $page = null, $perPage = null, $publish = true){
63
    $now    = date('Y-m-d H:i:s');
64 1
    $params = array($type);
65
         
66 1
    $this->db->select("c.*")
67 1
         ->from("content AS c")
68
    //   ->join("user AS u", "c.author = u.id")
69 1
         ->where("c.`type` = ?")
70
         ->andWhere("c.`deleted` IS NULL");
71
72 1
    if($publish){
73
      $this->db->andWhere("c.published <= ?");
74 2
      $params[] = $now;
75
    }
76
77
    $this->db->orderBy('c.published DESC, c.created DESC');
78
79
    /*if($perPage){
80
      $this->db->limit($perPage);
81
82
      if($page){
83
        $offset = $this->getOffset($perPage, $page);
84
        $this->db->offset($offset);
85
      }
86
    }*/
87
88
    $this->db->execute($params);
89
90
    $this->db->setFetchModeClass(__CLASS__);
91
    return $this->db->fetchAll();
92 1
  }
93
	
94
  /**
95
   * Get "all" content of one tag and one type from database
96
   *
97
   * @param  	string  	$tag				Wich tag content should have ex. page-news etc.
98
   * @param  	string  	$type				Wich type of content ex. blog, page etc.
99
   * @param  	boolean 	$publish 		If it needs to be publish
100
   * @param  	int   	  $page				Wich page paging are at
101
   * @param  	int	      $perPage		Content per page
102
   * @return 	object   							With content data from database
103
   */
104 1
  public function getAllContentOfTag($tag, $type, $page = 0, $perPage = null, $publish = true){
105
    $now    = date('Y-m-d H:i:s');
106
    $params = array($tag, $type);
107
108
    $this->db->select("c.*")
109
         ->from("content AS c")
110
    //   ->join("user AS u", "c.author = u.id")
111
         ->join("content_tags AS t", "t.idContent = c.id")
112
         ->where("t.slug = ?")
113
         ->andWhere("c.`type` = ?")
114
         ->andWhere("c.`deleted` IS NULL");
115
116
    if($publish){
117
      $this->db->andWhere("c.published <= ?");
118 1
      $params[] = $now;
119
    }
120
121
    $this->db->orderBy('c.published DESC, c.created DESC');
122
123
    /*if($perPage){
124
      $this->db->limit($perPage);
125
126
      if($page){
127
        $offset = $this->getOffset($perPage, $page);
128
        $this->db->offset($offset);
129
      }
130
    }*/
131
132
    $this->db->execute($params);
133
134
    $this->db->setFetchModeClass(__CLASS__);
135
    return $this->db->fetchAll();
136
  }
137
	
138
  /**
139
   * Get content from database by url
140
   *
141
   * @param  	string		$url			Begin or whole url
142
   * @param  	boolean 	$publish 	If it needs to be publish
143
   * @param  	string  	$type			Wich type of content ex. blog, page etc.
144
   * @return 	object   						With content data from database
145
   */
146 1 View Code Duplication
  public function getContentByUrl($url, $type, $publish = true){
147
    $now    = date('Y-m-d H:i:s');
148
    $params = [$url, $type];
149
150
    $this->db->select("c.*")
151
             ->from("content AS c")
152
    //       ->join("user AS u", "c.author = u.id")
153
             ->where("c.url = ?")
154
             ->andWhere("c.`type` = ?")
155
             ->andWhere("c.`deleted` IS NULL");
156
157
    if($publish){
158
      $this->db->andWhere("c.published <= ?");
159 1
      $params[] = $now;
160
    }
161
162
    $this->db->orderBy('c.published DESC, c.created DESC')
163
             ->limit(1)
164
             ->execute($params);
165
166
    $this->db->setFetchModeClass(__CLASS__);
167
    return $this->db->fetchOne();
168 1
  }
169
	
170
  /**
171
   * Get content from database by slug
172
   *
173
   * @param  	string  $slug    	Unique query to content
174
   * @param  	boolean $publish 	if it needs to be publish
175
   * @param  	string  $type			Wich type of content ex. blog, page etc.
176
   * @return 	object   					With content data from database
177
   */
178 1 View Code Duplication
  public function getContentBySlug($slug, $type, $publish = true){
179
    $now = date('Y-m-d H:i:s');
180
    $params = array($slug, $type);
181
    $this->db->select("c.*")
182
             ->from("content AS c")
183
    //       ->join("user AS u", "c.author = u.id")
184
             ->where("c.slug = ?")
185
             ->andWhere("c.`type` = ?")
186
             ->andWhere("c.`deleted` IS NULL");
187
           
188
    if($publish){
189
      $this->db->andWhere("c.published <= ?");
190 1
      $params[] = $now;
191
    }
192
193
    $this->db->orderBy('c.published DESC, c.created DESC')
194
             ->limit(1)
195
             ->execute($params);
196
197
    $this->db->setFetchModeClass(__CLASS__);
198
    return $this->db->fetchOne();
199 1
  }
200
	
201
  /**
202
   * Get content from database by index
203
   *
204
   * @param  	int       $id				Index to content
205
   * @param  	boolean		$publish 	If it needs to be publish
206
   * @return 	object   					 	With content data.
207
   */
208 2
  public function getContentById($id, $publish = true){
209
    $now    = date('Y-m-d H:i:s');
210 1
    $params = array($id);
211
212 1
    $this->db->select("c.*, group_concat(t.tag, ', ') AS tags")
213 1
             ->from("content AS c")
214
    //       ->join("user AS u", "c.author = u.id")
215 1
             ->leftJoin("content_tags AS t", "t.idContent = c.id")
216 1
             ->where("c.id = ?")
217
             ->andWhere("c.`deleted` IS NULL");
218
219 1
    if($publish){
220
      $this->db->andWhere("c.published <= ?");
221 2
      $params[] = $now;
222
    }
223
224
    $this->db->execute($params);
225
    $this->db->setFetchModeClass(__CLASS__);
226
    return $this->db->fetchOne();
227 1
  }
228
	
229
  /*****
230
   ***
231
   ** GET - Counting prepare for Paging (exemple on pagecontrollers blog.php, page.php, report.php and list_content.php)
232
   ***
233
   *****/
234
   
235
  /**
236
   * Count all content from database
237
   *
238
   * @param  	boolean 	$publish 	  If it needs to be publish
239
   * @return 	int   					      With count of content in database
240
   */
241 1 View Code Duplication
  public function countAllContent($publish = true){
242
    $now    = date('Y-m-d H:i:s');
243
    $params = [];
244
245
    $this->db->select("COUNT(id) AS countAll")
246
             ->from("Content")
247
             ->where("`deleted` IS NULL");
248
249
    if($publish){
250
      $this->db->andWhere("published <= ?");
251 1
      $params[] = $now;
252
    }
253
254
    $this->db->execute($params);
255
256
    $this->db->setFetchModeClass(__CLASS__);
257
    $count = $this->db->fetchOne();
258
    return $count->countAll;
259 1
  }
260
	
261
  /**
262
   * Count all content of one type from database
263
   *
264
   * @param  	boolean 	$publish 	  If it needs to be publish
265
   * @param  	string  	$type			  Wich type of content ex. blog, page etc.
266
   * @return 	int   			    		  With count of content in database
267
   */
268 1 View Code Duplication
  public function countAllContentOfType($type, $publish = true){
269
    $now    = date('Y-m-d H:i:s');
270
    $params = [$type];
271
272
    $this->db->select("COUNT(id) AS countAll")
273
             ->from("Content")
274
             ->where("`type` = ?")
275
             ->andWhere("`deleted` IS NULL");
276
277
    if($publish){
278
      $this->db->andWhere("published <= ?");
279 1
      $params[] = $now;
280
    }
281
282
    $this->db->execute($params);
283
284
    $this->db->setFetchModeClass(__CLASS__);
285
    $count = $this->db->fetchOne();
286
    return $count->countAll;
287 1
  }
288
	
289
  /**
290
   * Count all content of one tag and one type from database
291
   *
292
   * @param  string     $tag			  Wich tag of content ex. page-news etc.
293
   * @param  string     $type		    Wich type of content ex. blog, page etc.
294
   * @param  boolean    $publish    Check if it needs to be publish
295
   * @return int   						      With count
296
   */
297 1 View Code Duplication
  public function countAllContentOfTag($tag, $type, $publish = true){
298
    $now    = date('Y-m-d H:i:s');
299
    $params = [$tag, $type];
300
    
301
    $this->db->select("COUNT(c.id) AS countAll")
302
             ->from("content AS c")
303
             ->join("content_tags AS t", "t.idContent = c.id")
304
             ->where("t.slug = ?")
305
             ->andWhere("c.`type` = ?")
306
             ->andWhere("c.`deleted` IS NULL");
307
    
308
    if($publish){
309
      $this->db->andWhere("c.published <= ?");
310 1
      $params[] = $now;
311
    }
312
    
313
    $this->db->execute($params);
314
    
315
    $this->db->setFetchModeClass(__CLASS__);
316
    $count = $this->db->fetchOne();
317
    
318
    return $count->countAll;		
319 1
  }
320
	
321
  /*****
322
   ***
323
   ** GET - Help functions (exemple on pagecontrollers blog.php, page.php, report.php and list_content.php)
324
   ***
325
   *****/
326
   
327
  /**
328
   * Get tags for a content
329
   *
330
   * @param		int     $id		  Index to content
331
   * @return	object	$res	  Tags result
332
   */
333 1
  public function getTagsForContent($id){
334
    $this->db->select("tag, slug")
335 1
             ->from("content_tags")
336 1
             ->where("idContent = ?")
337
             ->execute(array($id));
338
    
339
    $this->db->setFetchModeClass(__CLASS__);
340
    return $this->db->fetchAll();
341
  }
342
	
343
  /**
344
   * Get a tag's title by slug
345
   *
346
   * @param		string	  $slug		  URL for tag
347
   * @return	string					    Tag-title from database
348
   */
349 1
  public function getTagBySlug($slug){
350
    $this->db->select("tag")
351
             ->from("content_tags")
352
             ->where("slug = ?")
353
             ->execute(array($slug));
354
    
355
    $this->db->setFetchModeClass(__CLASS__);
356
    $tag = $this->db->fetchOne();
357
    
358
    if(isset($tag->tag))
359 1
      return 	$tag->tag;
360
    
361 1
    return null;
362
  }
363
  
364
  /**
365
   * Check if slug is available, or if not get one that is
366
   *
367
   * @param   string    $slug       Slug to validate
368
   * @param   string    $type       Type of content
369
   * @return  string    $newSlug    A available slug
370
   */
371 1
  public function makeSlugToContent($slug, $type){
372 1
    $newSlug = $slug;
373 1
    $j = 1;
374
    
375
    do{
376 1
      if($j > 1)
377 1
        $newSlug = "{$slug}_{$j}";
378
      
379
      $this->db->select("slug")
380 1
               ->from("content")
381 1
               ->where("slug = ?")
382 1
               ->andWhere("type = ?")
383 1
               ->andWhere("deleted IS NULL")
384
               ->execute(array($newSlug, $type));
385
      
386
      $this->db->setFetchModeClass(__CLASS__);
387
      $slugObj = $this->db->fetchOne();
388
      
389 1
      $j++;
390 1
    }while($slugObj);
391
    
392 1
    return $newSlug;
393
  }
394
  
395
  /**
396
   * Get sql-query offset by per page and page
397
   *
398
   *  @param    int   $perPage    Per page
399
   *  @param    int   $page       Page on paging
400
   *  @return   int   $offset     Offset for sql-query
401
   */
402
  public function getOffset($perPage, $page){
403
    $offset = null;
404
    if($perPage){
405
      $offset = ($perPage * ($page - 1));
406
      $offset = ($offset > 0) ? $offset : 0;
407
    }
408
    
409
    return $offset;
410
  }
411
}