6d7ef3b8c27c346ca87f0cff8feedafbe56a70ea
[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             'published',
45             array(
46                 'long_name'   => '--published',
47                 'description' => 'Publish date',
48                 'help_name'   => 'DATE',
49                 'action'      => 'StoreString',
50                 'default'     => null,
51             )
52         );
53         $cmd->addOption(
54             'slug',
55             array(
56                 'short_name'  => '-s',
57                 'long_name'   => '--slug',
58                 'description' => 'URL path',
59                 'help_name'   => 'PATH',
60                 'action'      => 'StoreString',
61                 'default'     => null,
62             )
63         );
64         $cmd->addOption(
65             'syndication',
66             array(
67                 'short_name'  => '-s',
68                 'long_name'   => '--syndication',
69                 'description' => 'Syndication URL(s)',
70                 'help_name'   => 'URL',
71                 'action'      => 'StoreArray',
72                 'default'     => [],
73             )
74         );
75         $cmd->addOption(
76             'x',
77             array(
78                 'short_name'  => '-x',
79                 'long_name'   => '--xprop',
80                 'description' => 'Additional property',
81                 'help_name'   => 'key=value',
82                 'action'      => 'StoreArray',
83                 'default'     => [],
84             )
85         );
86     }
87
88     protected function handleGenericOptions(
89         \Console_CommandLine_Result $cmdRes, Request $req
90     ) {
91         if ($cmdRes->options['published'] !== null) {
92             $req->req->addPostParameter(
93                 'published', $cmdRes->options['published']
94             );
95         }
96         if (count($cmdRes->options['categories'])) {
97             $req->addPostParameter(
98                 'category', $cmdRes->options['categories']
99             );
100         }
101         if ($cmdRes->options['slug'] !== null) {
102             $req->req->addPostParameter(
103                 'slug', $cmdRes->options['slug']
104             );
105         }
106         if (count($cmdRes->options['syndication'])) {
107             $req->addPostParameter(
108                 'syndication', $cmdRes->options['syndication']
109             );
110         }
111
112         $this->handleFiles($cmdRes, $req);
113
114         if (count($cmdRes->options['x'])) {
115             $postParams = [];
116             foreach ($cmdRes->options['x'] as $xproperty) {
117                 list($propkey, $propval) = explode('=', $xproperty, 2);
118                 if (!isset($postParams[$propkey] )) {
119                     $postParams[$propkey] = [];
120                 }
121                 $postParams[$propkey][] = $propval;
122             }
123             foreach ($postParams as $propkey => $propvals) {
124                 $req->addPostParameter($propkey, $propvals);
125             }
126         }
127     }
128
129     protected function handleFiles(
130         \Console_CommandLine_Result $cmdRes, Request $req
131     ) {
132         $files = $cmdRes->options['files'];
133         $fileList = $urlList = [
134             'audio' => [],
135             'image' => [],
136             'video' => [],
137         ];
138
139         foreach ($files as $filePath) {
140             if (strpos($filePath, '://') !== false) {
141                 //url
142                 $mte      = new \MIME_Type_Extension();
143                 $mimetype = $mte->getMIMEType($filePath);
144                 $media    = \MIME_Type::getMedia($mimetype);
145                 if (!isset($urlList[$media])) {
146                     Log::err('File type not allowed: ' . $mimetype);
147                     exit(20);
148                 }
149                 $urlList[$media][] = $filePath;
150             } else if (file_exists($filePath)) {
151                 //file
152                 $mimetype = \MIME_Type::autoDetect($filePath);
153                 $media    = \MIME_Type::getMedia($mimetype);
154                 if (!isset($urlList[$media])) {
155                     Log::err('File type not allowed: ' . $mimetype);
156                     exit(20);
157                 }
158                 $fileList[$media][] = $filePath;
159             } else {
160                 Log::err('File does not exist: ' . $filePath);
161                 exit(20);
162             }
163         }
164         foreach ($urlList as $type => $urls) {
165             if ($type == 'image') {
166                 $type = 'photo';
167             }
168             if (count($urls) == 1) {
169                 $req->req->addPostParameter($type, reset($urls));
170             } else if (count($urls) > 1) {
171                 $n = 0;
172                 foreach ($urls as $url) {
173                     $req->req->addPostParameter(
174                         $type . '[' . $n++ . ']', $url
175                     );
176                 }
177             }
178         }
179         foreach ($fileList as $type => $filePaths) {
180             if ($type == 'image') {
181                 $type = 'photo';
182             }
183             if (count($filePaths) == 1) {
184                 $req->addUpload($type, reset($filePaths));
185             } else if (count($filePaths) > 0) {
186                 $req->addUpload($type, $filePaths);
187             }
188         }
189     }
190 }
191 ?>