public function importPost($post, $dryrun = true) { // If the mapping is not defined, ignore it. if (empty($this->config['mapping'][$post['post_type']])) { return "<p>No mapping defined for posttype '" . $post['post_type'] . "'.</p>"; } // Find out which mapping we should use. $mapping = $this->config['mapping'][$post['post_type']]; // If the mapped contenttype doesn't exist in Bolt. if (!$this->app['storage']->getContentType($mapping['targetcontenttype'])) { return "<p>Bolt contenttype '" . $mapping['targetcontenttype'] . "' for posttype '" . $post['post_type'] . "' does not exist.</p>"; } // Create the new Bolt Record. $record = new \Bolt\Content($this->app, $mapping['targetcontenttype']); // 'expand' the postmeta fields to regular fields. if (!empty($post['postmeta']) && is_array($post['postmeta'])) { foreach ($post['postmeta'] as $id => $keyvalue) { $post[$keyvalue['key']] = $keyvalue['value']; } } // Iterate through the mappings, see if we can find it. foreach ($mapping['fields'] as $from => $to) { if (isset($post[$from])) { // It's present in the fields. $value = $post[$from]; switch ($from) { case "post_parent": if (!empty($value)) { $value = $mapping['fields']['post_parent_contenttype'] . "/" . $value; } break; case "post_date": if (!empty($value)) { // WXR seems to use only one date value. $record->setValue('datechanged', $value); $record->setValue('datecreated', $value); $record->setValue('datepublish', $value); } break; } switch ($to) { case "username": $value = makeSlug($value); break; case "status": if ($value == "publish") { $value = "published"; } if ($value == "future") { $value = "timed"; } break; } $record->setValue($to, $value); } } // Perhaps import the categories as well.. if (!empty($mapping['category']) && !empty($post['terms'])) { foreach ($post['terms'] as $term) { if ($term['domain'] == 'category') { $record->setTaxonomy($mapping['category'], $term['slug']); if (!in_array($term['slug'], $this->foundcategories)) { $this->foundcategories[] = $term['slug']; } } } } if ($dryrun) { $output = "<p>Original WXR Post <b>\"" . $post['post_title'] . "\"</b> -> Converted Bolt Record :</p>"; $output .= \util::var_dump($post, true); $output .= \util::var_dump($record, true); $output .= "\n<hr>\n"; } else { $this->app['storage']->saveContent($record); $output = "Import: " . $record->get('id') . " - " . $record->get('title') . " <small><em>"; $output .= $this->memUsage() . "mb.</em></small><br>"; } return $output; }
private function processForm($formconfig, $form, $formname) { $data = $form->getData(); if ($formconfig['debugmode'] == true) { \util::var_dump($formconfig); \util::var_dump($form); \util::var_dump($formname); \util::var_dump($data); \util::var_dump($this->app['request']->files); } // $data contains the posted data. For legibility, change boolean fields to "yes" or "no". foreach ($data as $key => $value) { if (gettype($value) == "boolean") { $data[$key] = $value ? "yes" : "no"; } } // Some fieldtypes (like 'date' and 'file') require post-processing. foreach ($formconfig['fields'] as $fieldname => $fieldvalues) { // Check if we have fields of type 'file'. If so, fetch them, and move them // to the designated folder. if ($fieldvalues['type'] == "file") { if (empty($formconfig['storage_location']) && $formconfig['attach_files'] === false) { die("You must set the storage_location in the field {$fieldname} if you do not use attachments."); } elseif (empty($formconfig['storage_location']) && $formconfig['attach_files'] == false) { // temporary files location will be a subdirectory of the cache $path = $this->app['paths']['apppath'] . '/cache'; $linkpath = $this->app['paths']['app'] . 'cache'; } else { // files location will be a subdirectory of the files $path = $this->app['paths']['filespath'] . "/" . $fieldvalues['storage_location']; $linkpath = $this->app['paths']['files'] . $fieldvalues['storage_location']; } if (!is_writable($path)) { die("The path {$path} is not writable."); } $files = $this->app['request']->files->get($form->getName()); $originalname = strtolower($files[$fieldname]->getClientOriginalName()); $filename = sprintf("%s-%s-%s.%s", $fieldname, date('Y-m-d'), $this->app['randomgenerator']->generateString(8, 'abcdefghijklmnopqrstuvwxyz01234567890'), getExtension($originalname)); $link = sprintf("%s%s/%s", $this->app['paths']['rooturl'], $linkpath, $filename); // Make sure the file is in the allowed extensions. if (in_array(getExtension($originalname), $fieldvalues['filetype'])) { // If so, replace the file to designated folder. $files[$fieldname]->move($path, $filename); // by default we send a link $data[$fieldname] = $link; if ($formconfig['attach_files'] == 'true') { // if there is an attachment and no saved file on the server // only send the original name and the attachment if (empty($formconfig['storage_location'])) { $data[$fieldname] = $originalname . " ({$link})"; } $attachments[] = \Swift_Attachment::fromPath($link)->setFilename($originalname); } } else { $data[$fieldname] = "Invalid upload, ignored ({$originalname})"; } } // Fields of type 'date' are \DateTime objects. Convert them to string, for sending in emails, etc. if ($fieldvalues['type'] == "date" && $data[$fieldname] instanceof \DateTime) { $format = isset($fieldvalues['format']) ? $fieldvalues['format'] : "Y-m-d"; $data[$fieldname] = $data[$fieldname]->format($format); } if ($fieldvalues['type'] == "ip") { $data[$fieldname] = $this->getRemoteAddress(); } if ($fieldvalues['type'] == "timestamp") { $format = "%F %T"; $data[$fieldname] = strftime($format); } } // Attempt to insert the data into a table, if specified.. if (!empty($formconfig['insert_into_table'])) { try { $this->app['db']->insert($formconfig['insert_into_table'], $data); } catch (\Doctrine\DBAL\DBALException $e) { // Oops. User will get a warning on the dashboard about tables that need to be repaired. $keys = array_keys($data); $this->app['log']->add("SimpleForms could not insert data into table" . $formconfig['insert_into_table'] . ' (' . join(', ', $keys) . ') - check if the table exists.', 3); echo "Couldn't insert data into table " . $formconfig['insert_into_table'] . "."; } } $mailhtml = $this->app['render']->render($formconfig['mail_template'], array('form' => $data)); if ($formconfig['debugmode'] == true) { \util::var_dump($mailhtml); } if (!empty($formconfig['mail_subject'])) { $subject = $formconfig['mail_subject']; } else { $subject = '[SimpleForms] ' . $formname; } // Compile the message.. $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom(array($formconfig['recipient_email'] => $formconfig['recipient_name']))->setTo(array($formconfig['recipient_email'] => $formconfig['recipient_name']))->setBody(strip_tags($mailhtml))->addPart($mailhtml, 'text/html'); if ($formconfig['attach_files'] == 'true' && is_array($attachments)) { foreach ($attachments as $attachment) { $message->attach($attachment); } } // check for testmode if ($formconfig['testmode'] == true) { // override recipient with debug recipient $message->setTo(array($formconfig['testmode_recipient'] => $formconfig['recipient_name'])); // do not add other cc and bcc addresses in testmode if (!empty($formconfig['recipient_cc_email']) && $formconfig['recipient_email'] != $formconfig['recipient_cc_email']) { $this->app['log']->add('Did not set Cc for ' . $formname . ' to ' . $formconfig['recipient_cc_email'] . ' (in testmode)', 3); } if (!empty($formconfig['recipient_bcc_email']) && $formconfig['recipient_email'] != $formconfig['recipient_bcc_email']) { $this->app['log']->add('Did not set Bcc for ' . $formname . ' to ' . $formconfig['recipient_bcc_email'] . ' (in testmode)', 3); } } else { // only add other recipients when not in testmode if (!empty($formconfig['recipient_cc_email']) && $formconfig['recipient_email'] != $formconfig['recipient_cc_email']) { $message->setCc($formconfig['recipient_cc_email']); $this->app['log']->add('Added Cc for ' . $formname . ' to ' . $formconfig['recipient_cc_email'], 3); } if (!empty($formconfig['recipient_bcc_email']) && $formconfig['recipient_email'] != $formconfig['recipient_bcc_email']) { $message->setBcc($formconfig['recipient_bcc_email']); $this->app['log']->add('Added Bcc for ' . $formname . ' to ' . $formconfig['recipient_bcc_email'], 3); } // check for other email addresses to be added foreach ($formconfig['fields'] as $key => $values) { if ($values['type'] == "email" && in_array($values['use_as'], array('to_email', 'from_email', 'cc_email', 'bcc_email'))) { $tmp_email = $data[$key]; if (isset($values['use_with'])) { $tmp_name = $data[$values['use_with']]; if (!$tmp_name) { $tmp_name = $tmp_email; } } else { $tmp_name = $tmp_email; } switch ($values['use_as']) { case 'from_email': // override from address //$message->setSender($formconfig['recipient_email']); // just to be clear who really sent it $message->setFrom(array($tmp_email => $tmp_name)); break; case 'to_email': // add another recipient $message->addTo($tmp_email, $tmp_name); break; case 'cc_email': // add a copy address $message->addCc($tmp_email, $tmp_name); break; case 'bcc_email': // add a blind copy address $message->addBcc($tmp_email, $tmp_name); break; } } } } $res = $this->app['mailer']->send($message); if ($res) { if ($formconfig['testmode']) { $this->app['log']->add('Sent email from ' . $formname . ' to ' . $formconfig['testmode_recipient'] . ' (in testmode) - ' . $formconfig['recipient_name'], 3); } else { $this->app['log']->add('Sent email from ' . $formname . ' to ' . $formconfig['recipient_email'] . ' - ' . $formconfig['recipient_name'], 3); } } return $res; }
/** * Output pretty-printed arrays. * * @see util::php * @see http://brandonwamboldt.github.com/utilphp/ * * @param mixed $var * return string */ public function print_dump($var) { $output = util::var_dump($var, true); return $output; }
public function test_var_dump() { $input = 'var'; $expect = '<pre style="margin-bottom: 18px;background: #f7f7f9;border: 1px solid #e1e1e8;padding: 8px;border-radius: 4px;-moz-border-radius: 4px;-webkit-border radius: 4px;display: block;font-size: 12.05px;white-space: pre-wrap;word-wrap: break-word;color: #333;font-family: Menlo,Monaco,Consolas,\'Courier New\',monospace;"><span style="color:#588bff;">string</span><span style="color:#999;">(</span>3<span style="color:#999;">)</span> <strong>"var"</strong></pre>'; // Ensure the proper output is returned $this->assertEquals($expect, Util::var_dump($input, true)); // Ensure the proper output is actually outputted $this->expectOutputString($expect); util::var_dump($input); // Ensure we avoid infinite recursion on recursive arrays $a = array('a' => 'value a', 'b' => 'value b'); $b = array('test' => &$a); $c = array('a' => &$a, 'b' => &$b); $a['c'] =& $c; $b['c'] =& $c; $this->assertContains('*RECURSION DETECTED*', Util::var_dump($c, true)); // Ensure we avoid infinite recursion on recursive objects $a = (object) array('a' => 'value a', 'b' => 'value b'); $b = (object) array('test' => &$a); $c = (object) array('a' => &$a, 'b' => &$b); $a->c =& $c; $b->c =& $c; $this->assertContains('*RECURSION DETECTED*', Util::var_dump($c, true)); // Test class scoping. $experiment = new VarDumpExperiment(); $actual = util::var_dump($experiment, true); $snippet = substr($actual, strrpos($actual, 'display:inline')); $this->assertContains('"public"', $snippet); $this->assertContains('"protected:protected"', $snippet); $this->assertContains('"private:VarDumpExperiment:private"', $actual); }