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