C:\xampp\htdocs\kptv\admin2\services\controller\News.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
<?php
namespace controller;

class 
News {

    public function 
get()
    {
        
$db = \MysqliDb::getInstance();

        
$cols = Array ("ID""Titlu""Imagine""Datas""Activs""Stickys");
        
$ret $db->get('stiri'null$cols);

        
$ret array_map(function($item) {
            return array(
                
'id' => $item['ID'],
                
'title' => $item['Titlu'],
                
"image" => $item['Imagine'],
                
"date" => $item['Datas'],
                
"publish" => $item['Activs'],
                
"isSticky" => $item['Stickys']
            );
        }, 
$ret);

        return 
json_encode($ret);        
    }

    public function 
getNews($id
    {
        
$db = \MysqliDb::getInstance();
        
$cols = Array ("ID""Titlu""Continut""Content_Short""Imagine""Datas""Activs""Stickys");

        
$db->where ('ID'$id);
        
$ret $db->get('stiri'null$cols);

        if(
count($ret) === 0) {
            
header("Status: 404 Not Found");
            return 
"";
        }

        
$item = array(
            
'id' => $ret[0]['ID'],
            
'title' => $ret[0]['Titlu'],
            
'content' => $ret[0]['Continut'],
            
'content_short' => $ret[0]['Content_Short'],
            
"image" => $ret[0]['Imagine'], 
            
"fileName" => null,            
            
"date" => $ret[0]['Datas'],
            
"publish" => $ret[0]['Activs'],
            
"isSticky" => $ret[0]['Stickys']
        );

        return 
json_encode($item);
    }


    public function 
updateNews() 
    {        
        
$body file_get_contents('php://input');
        
$requstOb json_decode($body,true);        

        
$db = \MysqliDb::getInstance();
        
$data = Array (
            
'Titlu' => $requstOb['title'],                
            
"Continut" => $requstOb['content'],
            
"Content_Short" => $requstOb['content_short'],            
            
"Datas" => $requstOb['date'],
            
"Activs" => $requstOb['publish'],            
            
"Stickys" => $requstOb['isSticky']
        );        
        
        if(!empty(
$requstOb['fileName'])) {
            
// copy file from temp
            
$srcPath $GLOBALS['paths']['images_temp'] . $requstOb['fileName'];
            
$destPath $GLOBALS['paths']['news_images'] . $requstOb['fileName'];            
            if(!
rename($srcPath$destPath)) {
                return 
json_encode(array('status'=> 0'message'=>'Unable to move uploaded file.''item'=>null));
            }
            
$data["Imagine"] = $requstOb['fileName'];
        }

        
$db->where ('id'$requstOb['id']);
        
$db->update ('stiri'$data);        

        
$item = array(
            
'id' => $requstOb['id'],
            
'title' => $requstOb['title'],            
            
"date" => $requstOb['date'],
            
"publish" => $requstOb['publish'],
            
"isSticky" => $requstOb['isSticky']);
        
        if(isset(
$data['Imagine'])) {
            
$item["image"] = $requstOb['fileName'];
        }        

        return 
json_encode(array('status'=>1'message'=>'News updated.''item'=>$item));

    }

    public function 
addNews() 
    {        
        
$body file_get_contents('php://input');
        
$requstOb json_decode($body,true);

        
$db = \MysqliDb::getInstance();
        
$data = Array (
            
'Titlu' => $requstOb['title'],                
            
"Continut" => $requstOb['content'],
            
"Content_Short" => $requstOb['content_short'],            
            
"Datas" => $requstOb['date'],
            
"Activs" => $requstOb['publish'],            
            
"Stickys" => $requstOb['isSticky']
        );        
        
        if(!empty(
$requstOb['fileName'])) {
            
// copy file from temp
            
$srcPath $GLOBALS['paths']['images_temp'] . $requstOb['fileName'];
            
$destPath $GLOBALS['paths']['news_images'] . $requstOb['fileName'];            
            if(!
rename($srcPath$destPath)) {
                return 
json_encode(array('status'=> 0'message'=>'Unable to move uploaded file.''item'=>null));
            }
            
$data["Imagine"] = $requstOb['fileName'];
        }

        
$id $db->insert ('stiri'$data);        
        
        
$item = array(
            
'id' => $id,
            
'title' => $requstOb['title'],            
            
"date" => $requstOb['date'],
            
"publish" => $requstOb['publish'],
            
"isSticky" => $requstOb['isSticky']);
        
        if(isset(
$data['Imagine'])) {
            
$item["image"] = $requstOb['fileName'];
        }        

        return 
json_encode(array('status'=>1'message'=>'News added.''item'=>$item));
    }

    
    public function 
deleteNews()
    {        
        
$body file_get_contents('php://input');
        
$requstOb json_decode($body,true);
                
        
$db = \MysqliDb::getInstance();        
        
$db->where('ID'$requstOb['id']);        
        
$id $db->delete ('stiri');

        
// TODO: delete image

        
$item = array('id' => $requstOb['id']);

        return 
json_encode(array('status'=> 1'message'=>'News deleted.''item'=> $item));
    }  

}
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586