Merge branch '2015.11-develop' into 2016.11
[tine20] / tine20 / Filemanager / Frontend / Json.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Filemanager
6  * @subpackage  Frontend
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2010-2014 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * backend class for Zend_Json_Server
15  *
16  * This class handles all Json requests for the Filemanager application
17  *
18  * @package     Filemanager
19  * @subpackage  Frontend
20  */
21 class Filemanager_Frontend_Json extends Tinebase_Frontend_Json_Abstract
22 {
23     /**
24      * app name
25      * 
26      * @var string
27      */
28     protected $_applicationName = 'Filemanager';
29     
30     /**
31      * search file/directory nodes
32      *
33      * @param  array $filter
34      * @param  array $paging
35      * @return array
36      */
37     public function searchNodes($filter, $paging)
38     {
39         $result = $this->_search($filter, $paging, Filemanager_Controller_Node::getInstance(), 'Filemanager_Model_NodeFilter');
40         $this->_removeAppIdFromPathFilter($result);
41         
42         return $result;
43     }
44     
45     /**
46      * remove app id (base path) from filter
47      * 
48      * @param array $_result
49      * 
50      * @todo is this really needed? perhaps we can set the correct path in Tinebase_Model_Tree_Node_PathFilter::toArray
51      */
52     protected function _removeAppIdFromPathFilter(&$_result)
53     {
54         $app = Tinebase_Application::getInstance()->getApplicationByName($this->_applicationName);
55         
56         foreach ($_result['filter'] as $idx => &$filter) {
57             if ($filter['field'] === 'path') {
58                 if (is_array($filter['value'])) {
59                     $filter['value']['path'] = Tinebase_Model_Tree_Node_Path::removeAppIdFromPath($filter['value']['path'], $app);
60                 } else {
61                     $filter['value'] = Tinebase_Model_Tree_Node_Path::removeAppIdFromPath($filter['value'], $app);
62                 }
63             }
64         }
65     }
66
67     /**
68      * create node
69      * 
70      * @param array $filename
71      * @param string $type directory or file
72      * @param string $tempFileId
73      * @param boolean $forceOverwrite
74      * @return array
75      */
76     public function createNode($filename, $type, $tempFileId = array(), $forceOverwrite = false)
77     {
78         $nodes = Filemanager_Controller_Node::getInstance()->createNodes((array)$filename, $type, (array)$tempFileId, $forceOverwrite);
79         $result = (count($nodes) === 0) ? array() : $this->_recordToJson($nodes->getFirstRecord());
80         
81         return $result;
82     }
83
84     /**
85      * create nodes
86      * 
87      * @param string|array $filenames
88      * @param string $type directory or file
89      * @param string|array $tempFileIds
90      * @param boolean $forceOverwrite
91      * @return array
92      */
93     public function createNodes($filenames, $type, $tempFileIds = array(), $forceOverwrite = false)
94     {
95         $nodes = Filemanager_Controller_Node::getInstance()->createNodes((array)$filenames, $type, (array)$tempFileIds, $forceOverwrite);
96         
97         return $this->_multipleRecordsToJson($nodes);
98     }
99     
100     /**
101      * copy node(s)
102      * 
103      * @param string|array $sourceFilenames string->single file, array->multiple
104      * @param string|array $destinationFilenames string->singlefile OR directory, array->multiple files
105      * @param boolean $forceOverwrite
106      * @return array
107      */
108     public function copyNodes($sourceFilenames, $destinationFilenames, $forceOverwrite)
109     {
110         $nodes = Filemanager_Controller_Node::getInstance()->copyNodes((array)$sourceFilenames, $destinationFilenames, $forceOverwrite);
111         
112         return $this->_multipleRecordsToJson($nodes);
113     }
114
115     /**
116      * move node(s)
117      * 
118      * @param string|array $sourceFilenames string->single file, array->multiple
119      * @param string|array $destinationFilenames string->singlefile OR directory, array->multiple files
120      * @param boolean $forceOverwrite
121      * @return array
122      */
123     public function moveNodes($sourceFilenames, $destinationFilenames, $forceOverwrite)
124     {
125         $nodes = Filemanager_Controller_Node::getInstance()->moveNodes((array)$sourceFilenames, $destinationFilenames, $forceOverwrite);
126         
127         return $this->_multipleRecordsToJson($nodes);
128     }
129
130     /**
131      * delete node(s)
132      * 
133      * @param string|array $filenames string->single file, array->multiple
134      * @return array
135      */
136     public function deleteNodes($filenames)
137     {
138         Filemanager_Controller_Node::getInstance()->deleteNodes((array)$filenames);
139         
140         return array(
141             'status'    => 'success'
142         );
143     }
144
145     /**
146      * returns the node record
147      * @param string $id
148      * @return array
149      */
150     public function getNode($id)
151     {
152         return $this->_get($id, Filemanager_Controller_Node::getInstance());
153     }
154     
155     /**
156      * save node
157      * save node here in json fe just updates meta info (name, description, relations, customfields, tags, notes),
158      * if record already exists (after it had been uploaded)
159      * @param array with record data 
160      * @return array
161      */
162     public function saveNode($recordData)
163     {
164         if((isset($recordData['created_by']) || array_key_exists('created_by', $recordData))) {
165             return $this->_save($recordData, Filemanager_Controller_Node::getInstance(), 'Node');
166         } else {    // on upload complete
167             return $recordData;
168         }
169     }
170     
171     /**
172      * Search for records matching given arguments
173      *
174      * @param  array $filter
175      * @param  array $paging
176      * @return array
177      */
178     public function searchDownloadLinks($filter, $paging)
179     {
180         return $this->_search($filter, $paging, Filemanager_Controller_DownloadLink::getInstance(), 'Filemanager_Model_DownloadLinkFilter');
181     }
182     
183     /**
184      * Return a single record
185      *
186      * @param   string $id
187      * @return  array record data
188      */
189     public function getDownloadLink($id)
190     {
191         return $this->_get($id, Filemanager_Controller_DownloadLink::getInstance());
192     }
193     
194     /**
195      * creates/updates a record
196      *
197      * @param  array $recordData
198      * @return array created/updated record
199      */
200     public function saveDownloadLink($recordData)
201     {
202         return $this->_save($recordData, Filemanager_Controller_DownloadLink::getInstance(), 'DownloadLink');
203     }
204     
205     /**
206      * deletes existing records
207      *
208      * @param  array $ids
209      * @return array
210      */
211     public function deleteDownloadLinks($ids)
212     {
213         return $this->_delete($ids, Filemanager_Controller_DownloadLink::getInstance());
214     }
215 }