b560fbefa45fcce9563ef46353eada8e74ebebb0
[shpub.git] / src / shpub / Command / AbstractProps.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Abstract command class that handles generic properties
6  */
7 class Command_AbstractProps
8 {
9     /**
10      * @var Config
11      */
12     protected $cfg;
13
14     public function __construct($cfg)
15     {
16         $this->cfg = $cfg;
17     }
18
19     public static function optsGeneric(\Console_CommandLine_Command $cmd)
20     {
21         $cmd->addOption(
22             'categories',
23             array(
24                 'short_name'  => '-c',
25                 'long_name'   => '--category',
26                 'description' => 'Category names',
27                 'help_name'   => 'CAT',
28                 'action'      => 'StoreArray',
29                 'default'     => [],
30             )
31         );
32         $cmd->addOption(
33             'files',
34             array(
35                 'short_name'  => '-f',
36                 'long_name'   => '--files',
37                 'description' => 'Files or URLs to upload',
38                 'help_name'   => 'PATH',
39                 'action'      => 'StoreArray',
40                 'default'     => [],
41             )
42         );
43         $cmd->addOption(
44             'name',
45             array(
46                 'short_name'  => '-n',
47                 'long_name'   => '--name',
48                 'description' => 'Post title',
49                 'help_name'   => 'TITLE',
50                 'action'      => 'StoreString',
51                 'default'     => null,
52             )
53         );
54         $cmd->addOption(
55             'published',
56             array(
57                 'long_name'   => '--published',
58                 'description' => 'Publish date',
59                 'help_name'   => 'DATE',
60                 'action'      => 'StoreString',
61                 'default'     => null,
62             )
63         );
64         $cmd->addOption(
65             'slug',
66             array(
67                 'short_name'  => '-s',
68                 'long_name'   => '--slug',
69                 'description' => 'URL path',
70                 'help_name'   => 'PATH',
71                 'action'      => 'StoreString',
72                 'default'     => null,
73             )
74         );
75         $cmd->addOption(
76             'syndication',
77             array(
78                 'short_name'  => '-s',
79                 'long_name'   => '--syndication',
80                 'description' => 'Syndication URL(s)',
81                 'help_name'   => 'URL',
82                 'action'      => 'StoreArray',
83                 'default'     => [],
84             )
85         );
86         $cmd->addOption(
87             'x',
88             array(
89                 'short_name'  => '-x',
90                 'long_name'   => '--xprop',
91                 'description' => 'Additional property',
92                 'help_name'   => 'key=value',
93                 'action'      => 'StoreArray',
94                 'default'     => [],
95             )
96         );
97     }
98
99     protected function handleGenericOptions(
100         \Console_CommandLine_Result $cmdRes, Request $req
101     ) {
102         if ($cmdRes->options['published'] !== null) {
103             $req->req->addPostParameter(
104                 'published', $cmdRes->options['published']
105             );
106         }
107         if (count($cmdRes->options['categories'])) {
108             $req->addPostParameter(
109                 'category', $cmdRes->options['categories']
110             );
111         }
112         if ($cmdRes->options['name'] !== null) {
113             $req->req->addPostParameter(
114                 'name', $cmdRes->options['name']
115             );
116         }
117         if ($cmdRes->options['slug'] !== null) {
118             $req->req->addPostParameter(
119                 'slug', $cmdRes->options['slug']
120             );
121         }
122         if (count($cmdRes->options['syndication'])) {
123             $req->addPostParameter(
124                 'syndication', $cmdRes->options['syndication']
125             );
126         }
127
128         $this->handleFiles($cmdRes, $req);
129
130         if (count($cmdRes->options['x'])) {
131             $postParams = [];
132             foreach ($cmdRes->options['x'] as $xproperty) {
133                 list($propkey, $propval) = explode('=', $xproperty, 2);
134                 if (!isset($postParams[$propkey] )) {
135                     $postParams[$propkey] = [];
136                 }
137                 $postParams[$propkey][] = $propval;
138             }
139             foreach ($postParams as $propkey => $propvals) {
140                 $req->addPostParameter($propkey, $propvals);
141             }
142         }
143     }
144
145     protected function handleFiles(
146         \Console_CommandLine_Result $cmdRes, Request $req
147     ) {
148         $files = $cmdRes->options['files'];
149         $fileList = $urlList = [
150             'audio' => [],
151             'image' => [],
152             'video' => [],
153         ];
154
155         foreach ($files as $filePath) {
156             if (strpos($filePath, '://') !== false) {
157                 //url
158                 $mte      = new \MIME_Type_Extension();
159                 $mimetype = $mte->getMIMEType($filePath);
160                 $media    = \MIME_Type::getMedia($mimetype);
161                 if (!isset($urlList[$media])) {
162                     Log::err('File type not allowed: ' . $mimetype);
163                     exit(20);
164                 }
165                 $urlList[$media][] = $filePath;
166             } else if (file_exists($filePath)) {
167                 //file
168                 $mimetype = \MIME_Type::autoDetect($filePath);
169                 $media    = \MIME_Type::getMedia($mimetype);
170                 if (!isset($urlList[$media])) {
171                     Log::err('File type not allowed: ' . $mimetype);
172                     exit(20);
173                 }
174                 $fileList[$media][] = $filePath;
175             } else {
176                 Log::err('File does not exist: ' . $filePath);
177                 exit(20);
178             }
179         }
180         foreach ($urlList as $type => $urls) {
181             if ($type == 'image') {
182                 $type = 'photo';
183             }
184             if (count($urls) == 1) {
185                 $req->req->addPostParameter($type, reset($urls));
186             } else if (count($urls) > 1) {
187                 $n = 0;
188                 foreach ($urls as $url) {
189                     $req->req->addPostParameter(
190                         $type . '[' . $n++ . ']', $url
191                     );
192                 }
193             }
194         }
195         foreach ($fileList as $type => $filePaths) {
196             if ($type == 'image') {
197                 $type = 'photo';
198             }
199             if (count($filePaths) == 1) {
200                 $req->addUpload($type, reset($filePaths));
201             } else if (count($filePaths) > 0) {
202                 $req->addUpload($type, $filePaths);
203             }
204         }
205     }
206 }
207 ?>