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 addOptHtml(\Console_CommandLine_Command $cmd)
115 'short_name' => '-h',
116 'long_name' => '--html',
117 'description' => 'Text content is HTML',
118 'action' => 'StoreTrue',
124 protected static function addOptJson(\Console_CommandLine_Command $cmd)
129 'long_name' => '--json',
130 'description' => 'Send request data as JSON',
131 'action' => 'StoreTrue',
137 protected function handleGenericOptions(
138 \Console_CommandLine_Result $cmdRes, Request $req
140 $this->handleOptJson($cmdRes, $req);
142 if ($cmdRes->options['published'] !== null) {
143 $req->req->addPostParameter(
144 'published', $cmdRes->options['published']
147 if ($cmdRes->options['updated'] !== null) {
148 $req->req->addPostParameter(
149 'updated', $cmdRes->options['updated']
152 if (count($cmdRes->options['categories'])) {
153 $req->addPostParameter(
154 'category', $cmdRes->options['categories']
157 if ($cmdRes->options['name'] !== null) {
158 $req->req->addPostParameter(
159 'name', $cmdRes->options['name']
162 if ($cmdRes->options['slug'] !== null) {
163 $req->req->addPostParameter(
164 'slug', $cmdRes->options['slug']
167 if (count($cmdRes->options['syndication'])) {
168 $req->addPostParameter(
169 'syndication', $cmdRes->options['syndication']
173 $this->handleFiles($cmdRes, $req);
175 if (count($cmdRes->options['x'])) {
177 foreach ($cmdRes->options['x'] as $xproperty) {
178 list($propkey, $propval) = explode('=', $xproperty, 2);
179 if (!isset($postParams[$propkey])) {
180 $postParams[$propkey] = [];
182 $postParams[$propkey][] = $propval;
184 foreach ($postParams as $propkey => $propvals) {
185 $req->addPostParameter($propkey, $propvals);
190 protected function handleOptJson(
191 \Console_CommandLine_Result $cmdRes, Request $req
193 $req->setSendAsJson($cmdRes->options['json']);
196 protected function handleFiles(
197 \Console_CommandLine_Result $cmdRes, Request $req
199 $files = $cmdRes->options['files'];
200 $fileList = $urlList = [
206 foreach ($files as $filePath) {
207 if (strpos($filePath, '://') !== false) {
209 $mte = new \MIME_Type_Extension();
210 $mimetype = $mte->getMIMEType($filePath);
211 $media = \MIME_Type::getMedia($mimetype);
212 if (!isset($urlList[$media])) {
213 Log::err('File type not allowed: ' . $mimetype);
216 $urlList[$media][] = $filePath;
217 } else if (file_exists($filePath)) {
219 $mimetype = \MIME_Type::autoDetect($filePath);
220 $media = \MIME_Type::getMedia($mimetype);
221 if (!isset($urlList[$media])) {
222 Log::err('File type not allowed: ' . $mimetype);
225 $fileList[$media][] = $filePath;
227 Log::err('File does not exist: ' . $filePath);
231 foreach ($urlList as $type => $urls) {
232 if ($type == 'image') {
235 if (count($urls) == 1) {
236 $req->req->addPostParameter($type, reset($urls));
237 } else if (count($urls) > 1) {
239 foreach ($urls as $url) {
240 $req->req->addPostParameter(
241 $type . '[' . $n++ . ']', $url
246 foreach ($fileList as $type => $filePaths) {
247 if ($type == 'image') {
250 if (count($filePaths) == 1) {
251 $req->addUpload($type, reset($filePaths));
252 } else if (count($filePaths) > 0) {
253 $req->addUpload($type, $filePaths);