forked from dojoVader-dev/Diary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminController.php
executable file
·383 lines (352 loc) · 10.6 KB
/
AdminController.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
namespace Plugin\Diary;
/**
*
* @todo Add Explicit Namespace for Model to avoid Collision
* @author x64
*
*/
use \Plugin\Diary\Import\ImportDriver;
class AdminController extends \Ip\GridController {
public $model;
/**
* The Url to create the Note
*/
public function create() {
$categoryModel = new CategoryModel ();
ipAddCss ( "assets/css/sdk.css" );
ipAddJs ( "assets/js/ckeditor.js", null, 50 );
ipAddJsContent("create","
require(['Diary/create'],function(create){
create.init();
jQuery(document).ready(function(){
for(var i in CKEDITOR.instances) {
CKEDITOR.instances[i].on('blur', function() { this.updateElement(); });
}
});
});
");
$noteform = Helper::getNoteForm ();
$noteform->getField ( 'category_id' )->setValues ( $categoryModel->getCategoryOptions () );
$formView = $noteform->render ( ipView ( "view/backend/form/add.php" ) );
return ipView ( "view/backend/add.php", array (
'form' => $formView
) );
}
public function index() {
$model = new Model ();
$currentPageIdx=ipRequest()->getQuery("current",1);
$posts = $model->getPaginator("diary_blog", $currentPageIdx,(int)ipGetOption ( 'Diary.diaryPosts' ));
$articles=$posts->render(__DIR__."/view/backend/listArticles.php");
return ipView ( "view/backend/ui.php", array (
'articles' => ($articles === null ) ? "<h1>No Notes</h1>" : $articles
) );
}
public function grid()
{
$worker = new \Ip\Internal\Grid\Worker($this->config());
$result = $worker->handleMethod(ipRequest());
if (is_array($result) && !empty($result['error']) && !empty($result['errors'])) {
return new \Ip\Response\Json($result);
}
return new \Ip\Response\JsonRpc($result);
}
public function edit() {
$id = ( int ) ipRequest ()->getQuery ( 'id' );
if (! is_int ( $id )) {
throw new \Ip\Exception\Plugin ( 'Integer Parameter is Required' );
}
$model = $this->loadModel ();
$edit = $model->getArticleById ( $id );
$categoryModel = new CategoryModel ();
ipAddCss ( "assets/css/sdk.css" );
ipAddJs ( "assets/js/ckeditor.js", null, 50 );
ipAddJsContent("create","
require(['Diary/create'],function(create){
create.init();
jQuery(document).ready(function(){
for(var i in CKEDITOR.instances) {
CKEDITOR.instances[i].on('blur', function() { this.updateElement(); });
}
});
});
");
$noteform = Helper::getNoteForm ();
//Set the fields
// $noteform->getField('author')->setValue($edit['author']);
$noteform->getField('title')->setValue($edit['title']);
$noteform->getField('content')->setValue($edit['content']);
$noteform->getField('aa')->setValue('Diary.updatenote');
$noteform->getField('create')->setValue('Update');
$noteform->getField('status')->setValue($edit['status']);
$noteform->getField('updateField')->setValue($edit['id']);
$noteform->getField ( 'category_id' )->setValues ( $categoryModel->getCategoryOptions () );
$noteform->getField('category_id')->setValue($edit['category_id']);
$formView = $noteform->render ( ipView ( "view/backend/form/edit.php"));
return ipView ( "view/backend/add.php", array (
'form' => $formView,
'data'=>$edit
) );
}
public function importWordPress(){
//If Post has been set
$form=Helper::getUploadForm();
if(ipRequest()->isPost()){
$images = \Ip\Form\Field\File::getFiles(ipRequest()->getPost(), 'wordpress_file');
//Process the file
if(!isset($images)){
throw new Ip\Exception("Image is null");
}
$import=new ImportDriver($images[0]);
$import->import();
return new \Ip\Response\Redirect(ipActionUrl(array("aa"=>"Diary.index")));
}
return ipView("view/backend/import.php",array("form"=>$form));
}
private function loadModel() {
if (! $this->model instanceof \Plugin\Diary\Model && $this->model == null) {
$this->model = new Model ();
}
return $this->model;
}
public function createCategory() {
$articles = Helper::getEditMenu ();
$form = Helper::getCategoryForm ();
return ipView ( "view/backend/category.php", array (
"articles" => $articles,
"form" => $form
) );
}
public function AjaxArticles() {
$model = new Model ();
$id = ( int ) ipRequest ()->getPost ( 'id' );
return new \Ip\Response\Json ( $model->getArticleById ( $id ) );
}
/**
* Ajax method to create Note for the Diary
*/
public function createnote() {
if (ipRequest ()->isPost ()) {
$form = Helper::getNoteForm ();
$errors = $form->validate ( ipRequest ()->getPost () );
if ($errors) {
// error
$data = array (
'status' => 'error',
'errors' => $errors
);
} else {
// success
$note = new Model ();
$note->content = ipRequest ()->getPost ( 'content' );
$note->status = ipRequest ()->getPost ( 'status' );
$note->comment = 0;
$note->category_id = ipRequest ()->getPost ( 'category_id' );
$note->date=date('Y-m-d H:i:s',time());
$note->title = ipRequest ()->getPost ( 'title' );
$note->alias=str_replace(" ","_",strip_tags($note->title));
if ($id = $note->save ()) {
// Redirect to the Edit Page
//Success
$data=array(
"status"=>"success",
"message"=>sprintf("The Note has been created successfully #%d",$id),
"redirect"=>true,
"recordID"=>$id
);
}
}
}
$_SESSION['notice']=$data;
return new \Ip\Response\Redirect(ipActionUrl(array('aa'=>'Diary.edit','id'=>$id )));
}
/**
* Deletes the Note from the Database
* @return \Ip\Response\Json Array of Result or Status ['status'=>'','message'=>'']
*/
public function AjaxNoteDelete(){
$message=array();
if(ipRequest()->isPost()){
$id=(int)ipRequest()->getPost('id');
if(!is_integer($id)){
$message['status']='error';
$message['message']="The Post ID is not an integer";
}
else{
$model=new Model();
if($model->Delete($id)){
$message['status']='success';
$message['message']=sprintf("The Note %d was successfully deleted from the Database",$id);
}
else{
$message['status']='error';
$message['message']=sprintf("MySQL Says: %s",mysql_error());
}
}
}
return new \Ip\Response\Json($message);
}
public function updatenote() {
if (ipRequest ()->isPost ()) {
$form = Helper::getNoteForm ();
$errors = $form->validate ( ipRequest ()->getPost () );
if ($errors) {
// error
$data = array (
'status' => 'error',
'errors' => $errors,
'message'=>"An Error occurred on the Server"
);
} else {
// success
$note = new Model ();
$note->content = ipRequest ()->getPost ( 'content' );
$note->status = ipRequest ()->getPost ( 'status' );
$note->category_id = ipRequest ()->getPost ( 'category_id' );
$note->title = ipRequest ()->getPost ( 'title' );
$note->id=ipRequest()->getPost('updateField');
if ($id = $note->update()) {
// Redirect to the Edit Page
//Success
$data=array(
"status"=>"success",
"message"=>sprintf("The Note %s has been updated successfully ",$note->title)
);
}
}
}
$_SESSION['notice']=$data;
return new \Ip\Response\Redirect(ipActionUrl(array('aa'=>'Diary.edit','id'=>$note->id)));
}
/**
* The Ajax Method to Save the Content
*/
public function AjaxCreateCategory() {
if (ipRequest ()->isPost ()) {
$form = Helper::getForm ();
$errors = $form->validate ( ipRequest ()->getPost () );
if ($errors) {
// error
$data = array (
'status' => 'error',
'errors' => $errors
);
} else {
// success
$category = new CategoryModel ();
$saveData = array (
"name" => ipRequest ()->getPost ( 'name' ),
"description" => ipRequest ()->getPost ( 'description' )
);
$category->save ( $saveData );
$data = array (
'status' => 'ok'
);
}
return new \Ip\Response\Json ( $data );
}
}
public function comment() {
return $this->manageCategory ();
}
public function manageCategory() {
// HEhehehehe Borrowed Code from GridController
ipAddJsVariable('GridAction',ipRequest ()->getRequest ( 'aa' ));
ipAddJs('assets/js/Grid.js');
ipAddJs ( 'Ip/Internal/Grid/assets/gridInit.js' );
$controllerClass = get_class ( $this );
$controllerClassParts = explode ( '\\', $controllerClass );
$aa = $controllerClassParts [count ( $controllerClassParts ) - 2] . '.grid';
$gateway = array (
'aa' => $aa,
'action' => ipRequest ()->getRequest ( 'aa' )
);
$variables = array (
'gateway' => $gateway
);
$content = ipView ( 'view/backend/placeholder.php', $variables )->render ();
return $content;
}
public function config() {
switch (ipRequest ()->getRequest ( "action" )) {
case "Diary.manageCategory" :
return array (
'title' => 'Category List',
'type'=>'table',
'table' => 'diary_category',
'deleteWarning' => __ ( 'Do you really want to delete this item?', 'FormExample' ),
'sortField' => 'id',
'createPosition' => 'top',
'pageSize' => 25,
'fields' => array (
array (
'label' => __ ( 'ID title', 'Category' ),
'field' => 'id'
),
array (
'label' => __ ( 'Name', 'Category' ),
'field' => 'name',
'validators' => array (
'Required'
)
),
array (
'label' => __ ( 'Category Description', 'Category' ),
'field' => 'description',
'validators' => array (
'Required'
)
),
array (
'label' => __ ( 'Attached Articles', 'Category' ),
'field' => 'count'
)
)
)
;
break;
case "Diary.comment" :
return array (
'title' => 'Comment List',
'type'=>'table',
'table' => 'diary_comments',
'deleteWarning' => __ ( 'Do you really want to delete this item?', 'FormExample' ),
'sortField' => 'id',
'createPosition' => 'top',
'pageSize' => 25,
'fields' => array (
array (
'label' => __ ( 'Name', 'Comment' ),
'field' => 'author',
'validators' => array (
'Required'
)
),
array (
'label' => __ ( 'Comment', 'Comment' ),
'field' => 'content',
'validators' => array (
'Required'
)
),
array(
'type'=>'Select',
'label'=>'Enable Comments',
'field'=>'approved',
'values'=>array(
array(CommentModel::APPROVED,"Approve"),
array(CommentModel::PENDING,"Pending")
)
),
array (
'label' => __ ( 'Date', 'Comment' ),
'field' => 'date'
)
)
)
;
break;
}
}
}
?>