11<?php
2+
23/**
34 * FlexiPeeHP - Objekt kontaktu.
45 *
56 * @author Vítězslav Dvořák <[email protected] > 6- * @copyright (C) 2015-2017 Spoje.Net
7+ * @copyright (C) 2015-2020 Spoje.Net
78 */
89
910namespace FlexiPeeHP ;
1415 * @link https://www.flexibee.eu/api/dokumentace/ref/attachments/
1516 * @link https://demo.flexibee.eu/c/demo/priloha/properties
1617 */
17- class Priloha extends FlexiBeeRW
18- {
18+ class Priloha extends FlexiBeeRW {
19+
1920 /**
2021 * Evidence užitá objektem.
2122 *
@@ -48,13 +49,12 @@ class Priloha extends FlexiBeeRW
4849 * @param string $filepath
4950 * @param array $attachmentData
5051 */
51- public function attachFile ($ filepath , $ attachmentData = [])
52- {
52+ public function attachFile ($ filepath , $ attachmentData = []) {
5353 if (file_exists ($ filepath )) {
54- $ attachmentData ['nazSoub ' ] = basename ($ filepath );
54+ $ attachmentData ['nazSoub ' ] = basename ($ filepath );
5555 $ attachmentData ['contentType ' ] = mime_content_type ($ filepath );
56- $ attachmentData ['dataSize ' ] = filesize ($ filepath );
57- $ attachmentData ['dataHash ' ] = md5_file ($ filepath );
56+ $ attachmentData ['dataSize ' ] = filesize ($ filepath );
57+ $ attachmentData ['dataHash ' ] = md5_file ($ filepath );
5858
5959 switch ($ attachmentData ['contentType ' ]) {
6060 case 'image/png ' :
@@ -72,11 +72,10 @@ public function attachFile($filepath, $attachmentData = [])
7272 * @param FlexiBeeRO $object Source object
7373 * @return string url
7474 */
75- public static function getDownloadUrl ($ object )
76- {
77- $ urlParts = parse_url ($ object ->apiURL );
75+ public static function getDownloadUrl ($ object ) {
76+ $ urlParts = parse_url ($ object ->apiURL );
7877 $ pathParts = pathinfo ($ urlParts ['path ' ]);
79- return $ urlParts ['scheme ' ]. ':// ' . $ urlParts ['host ' ] .( array_key_exists ('port ' ,$ urlParts ) ? ': ' . $ urlParts ['port ' ] : '' ) .$ pathParts ['dirname ' ]. '/ ' . $ pathParts ['filename ' ]. '/content ' ;
78+ return $ urlParts ['scheme ' ] . ':// ' . $ urlParts ['host ' ] . ( array_key_exists ('port ' , $ urlParts ) ? ': ' . $ urlParts ['port ' ] : '' ) . $ pathParts ['dirname ' ] . '/ ' . $ pathParts ['filename ' ] . '/content ' ;
8079 }
8180
8281 /**
@@ -85,8 +84,7 @@ public static function getDownloadUrl($object)
8584 * @param FlexiBeeRO $object
8685 * @return array
8786 */
88- public static function getFirstAttachment ($ object )
89- {
87+ public static function getFirstAttachment ($ object ) {
9088 $ attachments = self ::getAttachmentsList ($ object );
9189 return count ($ attachments ) ? current ($ attachments ) : null ;
9290 }
@@ -99,10 +97,9 @@ public static function getFirstAttachment($object)
9997 *
10098 * @return string
10199 */
102- public static function getAttachment ($ attachmentID ,$ options = [])
103- {
104- $ result = null ;
105- $ downloader = new Priloha ($ attachmentID ,$ options );
100+ public static function getAttachment ($ attachmentID , $ options = []) {
101+ $ result = null ;
102+ $ downloader = new Priloha ($ attachmentID , $ options );
106103 if ($ downloader ->lastResponseCode == 200 ) {
107104
108105 $ downloader ->doCurlRequest (self ::getDownloadURL ($ downloader ), 'GET ' );
@@ -120,26 +117,25 @@ public static function getAttachment($attachmentID,$options = [])
120117 * @param int|string $attachmentID
121118 */
122119 public static function download ($ object , $ format = 'pdf ' ,
123- $ attachmentID = null )
124- {
120+ $ attachmentID = null ) {
125121 $ attachments = self ::getAttachmentsList ($ object );
126122
127123 if (isset ($ attachmentID ) && !array_key_exists ($ attachmentID ,
128- $ attachments )) {
124+ $ attachments )) {
129125 $ object ->addStatusMessage (sprintf (_ ('Attagment %s does no exist ' ),
130- $ attachmentID ), 'warning ' );
126+ $ attachmentID ), 'warning ' );
131127 }
132128
133129 $ attachmentBody = $ object ->doCurlRequest (self ::getDownloadUrl ($ object ),
134- 'GET ' );
130+ 'GET ' );
135131 header ('Content-Description: File Transfer ' );
136132 header ('Content-Type: application/octet-stream ' );
137133 header ('Content-Transfer-Encoding: binary ' );
138134 header ('Expires: 0 ' );
139135 header ('Cache-Control: must-revalidate, post-check=0, pre-check=0 ' );
140136 header ('Pragma: public ' );
141- header ('Content-Disposition: attachment; filename= ' . $ object ->getEvidence (). '_ ' . $ object. '. ' . $ format );
142- header ('Content-Length: ' . strlen ($ attachmentBody ));
137+ header ('Content-Disposition: attachment; filename= ' . $ object ->getEvidence () . '_ ' . $ object . '. ' . $ format );
138+ header ('Content-Length: ' . strlen ($ attachmentBody ));
143139 echo $ attachmentBody ;
144140 }
145141
@@ -150,19 +146,18 @@ public static function download($object, $format = 'pdf',
150146 * @param string $destination directory or filename with path
151147 * @return int
152148 */
153- public static function saveToFile ($ attachmentID , $ destination )
154- {
155- $ result = 0 ;
149+ public static function saveToFile ($ attachmentID , $ destination ) {
150+ $ result = 0 ;
156151 $ downloader = new Priloha ($ attachmentID );
157152 if ($ downloader ->lastResponseCode == 200 ) {
158153
159154 $ downloader ->doCurlRequest (self ::getDownloadURL ($ downloader ), 'GET ' );
160155 if ($ downloader ->lastResponseCode == 200 ) {
161156 if (is_dir ($ destination )) {
162- $ destination .= '/ ' . $ downloader ->getDataValue ('nazSoub ' );
157+ $ destination .= '/ ' . $ downloader ->getDataValue ('nazSoub ' );
163158 }
164159 $ result = file_put_contents ($ destination ,
165- $ downloader ->lastCurlResponse );
160+ $ downloader ->lastCurlResponse );
166161 }
167162 }
168163 return $ result ;
@@ -174,12 +169,11 @@ public static function saveToFile($attachmentID, $destination)
174169 * @param FlexiBeeRW $object
175170 * @param string $filename
176171 *
177- * @return int HTTP response code
172+ * @return Priloha attached file object
178173 */
179- public static function addAttachmentFromFile ($ object , $ filename )
180- {
174+ public static function addAttachmentFromFile ($ object , $ filename ) {
181175 return self ::addAttachment ($ object , basename ($ filename ),
182- file_get_contents ($ filename ), mime_content_type ($ filename ));
176+ file_get_contents ($ filename ), mime_content_type ($ filename ));
183177 }
184178
185179 /**
@@ -190,19 +184,25 @@ public static function addAttachmentFromFile($object, $filename)
190184 * @param string $attachment Body
191185 * @param string $contentType Attachment Content-Type
192186 *
193- * @return int HTTP Response code
187+ * @return Priloha attached file object
194188 */
195189 public static function addAttachment ($ object , $ filename , $ attachment ,
196- $ contentType )
197- {
198- $ headersBackup = $ object ->defaultHttpHeaders ;
199- $ object ->postFields = $ attachment ;
190+ $ contentType ) {
191+ $ attached = new Priloha ();
192+ $ headersBackup = $ object ->defaultHttpHeaders ;
193+ $ codeBackup = $ object ->lastResponseCode ;
194+ $ responseBackup = $ object ->lastCurlResponse ;
195+ $ object ->postFields = $ attachment ;
200196 $ object ->defaultHttpHeaders ['Content-Type ' ] = $ contentType ;
201- $ url = $ object ->getFlexiBeeURL ().'/prilohy/new/ ' .$ filename ;
202- $ response = $ object ->doCurlRequest ($ url ,
203- 'PUT ' );
204- $ object ->defaultHttpHeaders = $ headersBackup ;
205- return $ response ;
197+ $ url = $ object ->getFlexiBeeURL () . '/prilohy/new/ ' . $ filename ;
198+ $ response = $ object ->performRequest ($ url , 'PUT ' );
199+ $ object ->defaultHttpHeaders = $ headersBackup ;
200+ $ attached ->setMyKey ($ response [0 ]['id ' ]);
201+ $ attached ->lastResponseCode = $ object ->lastResponseCode ;
202+ $ attached ->lastCurlResponse = $ object ->lastCurlResponse ;
203+ $ object ->lastResponseCode = $ codeBackup ;
204+ $ object ->lastCurlResponse = $ responseBackup ;
205+ return $ attached ;
206206 }
207207
208208 /**
@@ -212,21 +212,20 @@ public static function addAttachment($object, $filename, $attachment,
212212 *
213213 * @return array
214214 */
215- public static function getAttachmentsList ($ object )
216- {
217- $ fburl = $ object ->getFlexiBeeURL ();
215+ public static function getAttachmentsList ($ object ) {
216+ $ fburl = $ object ->getFlexiBeeURL ();
218217 $ attachments = [];
219218 $ oFormat = $ object ->format ;
220219 $ object ->setFormat ('json ' );
221- $ atch = $ object ->getFlexiData ($ fburl .'/prilohy ' .(count ($ object ->defaultUrlParams )
222- ? '? ' .http_build_query ($ object ->defaultUrlParams ) : '' ));
220+ $ atch = $ object ->getFlexiData ($ fburl . '/prilohy ' . (count ($ object ->defaultUrlParams ) ? '? ' . http_build_query ($ object ->defaultUrlParams ) : '' ));
223221 $ object ->setFormat ($ oFormat );
224222 if (count ($ atch ) && ($ object ->lastResponseCode == 200 )) {
225223 foreach ($ atch as $ attachmentID => $ attachmentData ) {
226- $ attachments [$ attachmentID ] = $ attachmentData ;
227- $ attachments [$ attachmentID ]['url ' ] = $ object ->url . '/c/ ' . $ object ->company . '/priloha/ ' . $ attachmentData ['id ' ];
224+ $ attachments [$ attachmentID ] = $ attachmentData ;
225+ $ attachments [$ attachmentID ]['url ' ] = $ object ->url . '/c/ ' . $ object ->company . '/priloha/ ' . $ attachmentData ['id ' ];
228226 }
229227 }
230228 return $ attachments ;
231229 }
230+
232231}
0 commit comments