5 * Abstract command class that handles generic properties
7 class Command_AbstractProps
14 public function __construct($cfg)
19 public static function optsGeneric(\Console_CommandLine_Command $cmd)
25 'long_name' => '--category',
26 'description' => 'Category names',
28 'action' => 'StoreArray',
36 'long_name' => '--file',
37 'description' => 'Files or URLs to upload',
38 'help_name' => 'PATH',
39 'action' => 'StoreArray',
47 'long_name' => '--name',
48 'description' => 'Post title',
49 'help_name' => 'TITLE',
50 'action' => 'StoreString',
57 'long_name' => '--published',
58 'description' => 'Publish date',
59 'help_name' => 'DATE',
60 'action' => 'StoreString',
67 'long_name' => '--updated',
68 'description' => 'Update date',
69 'help_name' => 'DATE',
70 'action' => 'StoreString',
78 'long_name' => '--slug',
79 'description' => 'URL path',
80 'help_name' => 'PATH',
81 'action' => 'StoreString',
89 'long_name' => '--syndication',
90 'description' => 'Syndication URL(s)',
92 'action' => 'StoreArray',
100 'long_name' => '--xprop',
101 'description' => 'Additional property',
102 'help_name' => 'key=value',
103 'action' => 'StoreArray',
107 static::addOptJson($cmd);
110 protected static function addOptJson(\Console_CommandLine_Command $cmd)
115 'long_name' => '--json',
116 'description' => 'Send request data as JSON',
117 'action' => 'StoreTrue',
123 protected function handleGenericOptions(
124 \Console_CommandLine_Result $cmdRes, Request $req
126 $this->handleOptJson($cmdRes, $req);
128 if ($cmdRes->options['published'] !== null) {
129 $req->req->addPostParameter(
130 'published', $cmdRes->options['published']
133 if ($cmdRes->options['updated'] !== null) {
134 $req->req->addPostParameter(
135 'updated', $cmdRes->options['updated']
138 if (count($cmdRes->options['categories'])) {
139 $req->addPostParameter(
140 'category', $cmdRes->options['categories']
143 if ($cmdRes->options['name'] !== null) {
144 $req->req->addPostParameter(
145 'name', $cmdRes->options['name']
148 if ($cmdRes->options['slug'] !== null) {
149 $req->req->addPostParameter(
150 'slug', $cmdRes->options['slug']
153 if (count($cmdRes->options['syndication'])) {
154 $req->addPostParameter(
155 'syndication', $cmdRes->options['syndication']
159 $this->handleFiles($cmdRes, $req);
161 if (count($cmdRes->options['x'])) {
163 foreach ($cmdRes->options['x'] as $xproperty) {
164 list($propkey, $propval) = explode('=', $xproperty, 2);
165 if (!isset($postParams[$propkey])) {
166 $postParams[$propkey] = [];
168 $postParams[$propkey][] = $propval;
170 foreach ($postParams as $propkey => $propvals) {
171 $req->addPostParameter($propkey, $propvals);
176 protected function handleOptJson(
177 \Console_CommandLine_Result $cmdRes, Request $req
179 $req->setSendAsJson($cmdRes->options['json']);
182 protected function handleFiles(
183 \Console_CommandLine_Result $cmdRes, Request $req
185 $files = $cmdRes->options['files'];
186 $fileList = $urlList = [
192 foreach ($files as $filePath) {
193 if (strpos($filePath, '://') !== false) {
195 $mte = new \MIME_Type_Extension();
196 $mimetype = $mte->getMIMEType($filePath);
197 $media = \MIME_Type::getMedia($mimetype);
198 if (!isset($urlList[$media])) {
199 Log::err('File type not allowed: ' . $mimetype);
202 $urlList[$media][] = $filePath;
203 } else if (file_exists($filePath)) {
205 $mimetype = \MIME_Type::autoDetect($filePath);
206 $media = \MIME_Type::getMedia($mimetype);
207 if (!isset($urlList[$media])) {
208 Log::err('File type not allowed: ' . $mimetype);
211 $fileList[$media][] = $filePath;
213 Log::err('File does not exist: ' . $filePath);
217 foreach ($urlList as $type => $urls) {
218 if ($type == 'image') {
221 if (count($urls) == 1) {
222 $req->req->addPostParameter($type, reset($urls));
223 } else if (count($urls) > 1) {
225 foreach ($urls as $url) {
226 $req->req->addPostParameter(
227 $type . '[' . $n++ . ']', $url
232 foreach ($fileList as $type => $filePaths) {
233 if ($type == 'image') {
236 if (count($filePaths) == 1) {
237 $req->addUpload($type, reset($filePaths));
238 } else if (count($filePaths) > 0) {
239 $req->addUpload($type, $filePaths);