@@ -84,13 +84,85 @@ protected function partsRemove() {
8484 }
8585 }
8686
87+ protected function attachments () {
88+ $ data = $ this ->getSite ()->getSanitizedInputData ();
89+ $ this ->getPage ()->setSubTitle ('Attachments ' );
90+
91+ if ($ data ['widgetid ' ]) {
92+ $ widget = $ this ->widgetsRepo ->getById ($ data ['widgetid ' ]);
93+ if (!$ data ['pageContext ' ]) {
94+ $ data ['pageContext ' ] = null ;
95+ }
96+ $ this ->getSite ()->getViewRenderer ()->registerViewVariable ('pageContext ' , $ data ['pageContext ' ]);
97+ $ this ->getSite ()->getViewRenderer ()->registerViewVariable ('attachments ' ,$ this ->getSite ()->getDataRepository ("Files " )->getByRelatedIdAndType ($ widget ['id ' ],1 ));
98+ $ this ->getSite ()->getViewRenderer ()->registerViewVariable ('widget ' , $ widget );
99+ $ this ->setViewName ('widgets.attachments ' );
100+ }
101+ }
102+
103+ protected function attachmentsDownload () {
104+ $ data = $ this ->getSite ()->getSanitizedInputData ();
105+ if ($ data ['attachmentid ' ] && ($ file = $ this ->getSite ()->getDataRepository ("Files " )->getById ($ data ['attachmentid ' ]))) {
106+ try {
107+ $ this ->getSite ()->getHelper ("FileManager " )->getDownloadableFile ($ file );
108+ } catch (\RuntimeException $ e ) {
109+ $ this ->getLogger ()->warn ($ e ->getMessage ());
110+ $ this ->getSite ()->addSystemError ('Could not find the file requested for download ' );
111+ $ this ->loadDefault ();
112+ }
113+ }
114+ }
115+
116+ protected function attachmentsAdd () {
117+ $ data = $ this ->getSite ()->getSanitizedInputData ();
118+ if ($ data ['newFile ' ] && $ data ['widgetid ' ]) {
119+ $ widget = $ this ->widgetsRepo ->getById ($ data ['widgetid ' ]);
120+ $ fileManager = $ this ->getSite ()->getHelper ("FileManager " );
121+ $ filesRepo = $ this ->getSite ()->getDataRepository ("Files " );
122+
123+ try {
124+ $ filePath = 'attachments ' ;
125+ $ fileName = $ fileManager ->processBase64File ($ data ['newFile ' ],null ,$ filePath );
126+
127+ $ fileType = '' ;
128+ $ temp = explode ('. ' ,$ data ['fileGloss ' ]);
129+ if (count ($ temp ) > 1 ) {
130+ $ fileType = array_pop ($ temp );
131+ }
132+
133+ //add file to files repo
134+ $ filesRepo ->add (array ("name " =>$ fileName ,"path " =>$ filePath ,"file_type " =>$ fileType ,"gloss " =>$ data ['fileGloss ' ],"userid " =>$ this ->getSite ()->getGlobalUser ()->getProfileValue ("id " ),"typeid " =>1 ,"relatedid " =>$ data ['widgetid ' ]));
135+ } catch (\RuntimeException $ e ) {
136+ $ this ->getLogger ()->error ($ e ->getMessage ());
137+ http_response_code (500 );
138+ }
139+ }
140+ }
141+
142+ protected function attachmentsRemove () {
143+ $ data = $ this ->getSite ()->getSanitizedInputData ();
144+ if ($ data ['attachmentid ' ]) {
145+ $ fileManager = $ this ->getSite ()->getHelper ("FileManager " );
146+ $ filesRepo = $ this ->getSite ()->getDataRepository ("Files " );
147+ if ($ removableFile = $ filesRepo ->getById ($ data ['attachmentid ' ])) {
148+ try {
149+ $ fileManager ->removeFile ($ removableFile );
150+ $ filesRepo ->removeById ($ data ['attachmentid ' ]);
151+ } catch (\RuntimeException $ e ) {
152+ $ this ->getLogger ()->warn ($ e ->getMessage ());
153+ $ this ->getSite ()->addSystemError ('Error removing the attachment ' );
154+ }
155+ }
156+ }
157+ }
158+
87159 protected function search () {
88160 $ data = $ this ->getSite ()->getSanitizedInputData ();
89161 if (isset ($ data ['term ' ])) {
90162 $ this ->getSite ()->getViewRenderer ()->registerViewVariable ("widgets " ,$ this ->widgetsRepo ->search ($ data ['term ' ]));
91163 $ this ->setViewName ("widgets.list " );
92164 } else {
93- $ site ->addSystemError ('There was an error with the search ' );
165+ $ this -> getSite () ->addSystemError ('There was an error with the search ' );
94166 }
95167 }
96168
0 commit comments