move generic options into separate class
[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     public static function optsGeneric(\Console_CommandLine_Command $cmd)
10     {
11         $cmd->addOption(
12             'categories',
13             array(
14                 'short_name'  => '-c',
15                 'long_name'   => '--category',
16                 'description' => 'Category names',
17                 'help_name'   => 'CAT',
18                 'action'      => 'StoreArray',
19                 'default'     => [],
20             )
21         );
22         $cmd->addOption(
23             'files',
24             array(
25                 'short_name'  => '-f',
26                 'long_name'   => '--files',
27                 'description' => 'Files or URLs to upload',
28                 'help_name'   => 'PATH',
29                 'action'      => 'StoreArray',
30                 'default'     => [],
31             )
32         );
33         $cmd->addOption(
34             'published',
35             array(
36                 'long_name'   => '--published',
37                 'description' => 'Publish date',
38                 'help_name'   => 'DATE',
39                 'action'      => 'StoreString',
40                 'default'     => null,
41             )
42         );
43         $cmd->addOption(
44             'syndication',
45             array(
46                 'short_name'  => '-s',
47                 'long_name'   => '--syndication',
48                 'description' => 'Syndication URL(s)',
49                 'help_name'   => 'URL',
50                 'action'      => 'StoreArray',
51                 'default'     => [],
52             )
53         );
54         $cmd->addOption(
55             'x',
56             array(
57                 'short_name'  => '-x',
58                 'long_name'   => '--xprop',
59                 'description' => 'Additional property',
60                 'help_name'   => 'key=value',
61                 'action'      => 'StoreArray',
62                 'default'     => [],
63             )
64         );
65     }
66
67     protected function handleGenericOptions(
68         \Console_CommandLine_Result $cmdRes, Request $req
69     ) {
70         if ($cmdRes->options['published'] !== null) {
71             $req->req->addPostParameter(
72                 'published', $cmdRes->options['published']
73             );
74         }
75         if (count($cmdRes->options['categories'])) {
76             $req->addPostParameter(
77                 'category', $cmdRes->options['categories']
78             );
79         }
80         if (count($cmdRes->options['syndication'])) {
81             $req->addPostParameter(
82                 'syndication', $cmdRes->options['syndication']
83             );
84         }
85
86         $this->handleFiles($cmdRes, $req);
87
88         if (count($cmdRes->options['x'])) {
89             $postParams = [];
90             foreach ($cmdRes->options['x'] as $xproperty) {
91                 list($propkey, $propval) = explode('=', $xproperty, 2);
92                 if (!isset($postParams[$propkey] )) {
93                     $postParams[$propkey] = [];
94                 }
95                 $postParams[$propkey][] = $propval;
96             }
97             foreach ($postParams as $propkey => $propvals) {
98                 $req->addPostParameter($propkey, $propvals);
99             }
100         }
101     }
102
103     protected function handleFiles(
104         \Console_CommandLine_Result $cmdRes, Request $req
105     ) {
106         $files = $cmdRes->options['files'];
107         $fileList = $urlList = [
108             'audio' => [],
109             'image' => [],
110             'video' => [],
111         ];
112
113         foreach ($files as $filePath) {
114             if (strpos($filePath, '://') !== false) {
115                 //url
116                 $mte      = new \MIME_Type_Extension();
117                 $mimetype = $mte->getMIMEType($filePath);
118                 $media    = \MIME_Type::getMedia($mimetype);
119                 if (!isset($urlList[$media])) {
120                     Log::err('File type not allowed: ' . $mimetype);
121                     exit(20);
122                 }
123                 $urlList[$media][] = $filePath;
124             } else if (file_exists($filePath)) {
125                 //file
126                 $mimetype = \MIME_Type::autoDetect($filePath);
127                 $media    = \MIME_Type::getMedia($mimetype);
128                 if (!isset($urlList[$media])) {
129                     Log::err('File type not allowed: ' . $mimetype);
130                     exit(20);
131                 }
132                 $fileList[$media][] = $filePath;
133             } else {
134                 Log::err('File does not exist: ' . $filePath);
135                 exit(20);
136             }
137         }
138         foreach ($urlList as $type => $urls) {
139             if ($type == 'image') {
140                 $type = 'photo';
141             }
142             if (count($urls) == 1) {
143                 $req->req->addPostParameter($type, reset($urls));
144             } else if (count($urls) > 1) {
145                 $n = 0;
146                 foreach ($urls as $url) {
147                     $req->req->addPostParameter(
148                         $type . '[' . $n++ . ']', $url
149                     );
150                 }
151             }
152         }
153         foreach ($fileList as $type => $filePaths) {
154             if ($type == 'image') {
155                 $type = 'photo';
156             }
157             if (count($filePaths) == 1) {
158                 $req->addUpload($type, reset($filePaths));
159             } else if (count($filePaths) > 0) {
160                 $req->addUpload($type, $filePaths);
161             }
162         }
163     }
164 }
165 ?>