-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
executable file
·136 lines (113 loc) · 3.69 KB
/
Model.php
File metadata and controls
executable file
·136 lines (113 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Plugin\Diary;
/**
* This class is the Diary Database that will store and save information for the table Diary
*
* @author dojoVader
*
*/
use Plugin\Diary\Helper as Helper;
class Model extends BaseModel {
public $name="diary_blog";
public $id;
public $author, $date, $content, $title, $status;
public $modified;
public $comment;
public $category_id;
public $alias;
public function save() {
$this->beforeSave ();
// Save Data
$saveData = array (
"author" => $this->author,
"date" => $this->date,
"content" => $this->content,
"title" => $this->title,
"status" => $this->status,
"modified" => $this->modified,
"comment" => $this->comment,
"category_id" => $this->category_id,
"alias"=>$this->alias
);
return ipDb ()->insert ( "diary_blog", $saveData );
}
public function getPaginator($table, $currentPageIdx,$pageSize,$front=false) {
// let's fetch the total from the Database first
/**
@todo hardcoded value, change later
*/
$psize=$pageSize;
$recordCount = (int)ipDb ()->fetchValue ( sprintf ( "SELECT COUNT(*) from %s", ipTable ($table) ) );
$totalPages =(int) ceil ( $recordCount / $psize);
$currentPage = $currentPageIdx;
if ($currentPage > $totalPages) {
$currentPage = $totalPages;
}
$from = (abs($currentPage - 1)) * $psize;
//Empty Result
$pagination = new \Ip\Pagination\Pagination ( array (
'data'=>($front === true) ? $this->fetch($from, $psize,"status='1'") : $this->fetch($from, $psize),
'currentPage' => $currentPage,
'totalPages' => $totalPages,
'pagerSize' => $psize
) );
return $pagination;
}
private function fetch($from, $count, $where = 1) {
$sortField = 'date DESC';
// select ip_diary_blog.author,ip_diary_blog.date,ip_diary_blog.content,ip_diary_blog.id,title,ip_diary_blog.status,ip_diary_blog.category_id,dc.id as dcid ,dc.name
// from ip_diary_blog INNER JOIN ip_diary_category dc ON ip_diary_blog.category_id=dc.id
$sql = "select ip_diary_blog.author,ip_diary_blog.date,ip_diary_blog.content,
ip_diary_blog.id,ip_diary_blog.modified,ip_diary_blog.alias,title,ip_diary_blog.status,ip_diary_blog.category_id,dc.id as dcid ,dc.name
from ip_diary_blog INNER JOIN ip_diary_category dc ON ip_diary_blog.category_id=dc.id WHERE $where ORDER BY " . $sortField . "
LIMIT
$from, $count
";
$result = ipDb ()->fetchAll ( $sql );
return $result;
}
public function update() {
$this->isNewRecord=true;
$this->beforeSave ();
$updateData = array (
"author" => $this->author,
"content" => $this->content,
"title" => $this->title,
"status" => $this->status,
"modified" => $this->modified,
"comment" => $this->comment,
"category_id" => $this->category_id,
"alias"=>str_replace(" ","_",strip_tags($this->title))
);
$this->isNewRecord=false;
return ipDb ()->update ( "diary_blog", $updateData, array (
"id" => $this->id
) );
}
/**
* Returns Articles from the Database
*
* @return multitype:
*/
public function getArticles() {
return Helper::getList ( ipGetOption ( 'Diary.diaryPosts' ) );
}
public function getArticleById($id) {
return Helper::getArticleById ( $id );
}
public function getArticleByAlias($id) {
return Helper::getArticleByAlias( $id );
}
public function Delete($id) {
return Helper::DeleteNote ( $id );
}
private function beforeSave() {
if($this->isNewRecord):
$this->modified = date('Y-m-d H:i:s',time());
$this->author = Helper::getAuthor ();
//Create alias for the page
$this->alias=str_replace(" ","_",strip_tags($this->title));
endif;
}
}
?>