Esempio n. 1
0
 public function generateContent()
 {
     Bridge::initialize();
     $repo = Repository::instance();
     $lastPosts = $repo->allTaxonomies()->orderByDescending('id')->take(25);
     //['AND', 'status !=' => 'auto-draft', 'type !=' => 'revision']
     $options = $lastPosts->select(function ($taxonomy) {
         return $taxonomy->id . ') ' . $taxonomy->taxonomy . ' - ' . $taxonomy->term->name;
     }, 'id')->toArray();
     \Sledgehammer\array_key_unshift($options, 'custom', 'Custom ID');
     $form = new Form(['method' => 'GET', 'fields' => ['taxonomy' => new Input(['name' => 'id', 'class' => 'form-control', 'type' => 'select', 'options' => $options, 'label' => 'Select post', 'value' => $lastPosts[0]->id]), 'id' => new Input(['name' => 'custom', 'class' => 'form-control', 'label' => 'Custom ID']), 'varname' => new Input(['name' => 'varname', 'class' => 'form-control', 'label' => 'Variable name', 'value' => '$taxonomy'])], 'actions' => [new Button('Export', ['class' => 'btn btn-primary'])]]);
     $values = $form->import();
     if ($form->isSent()) {
         if ($values['id'] !== '') {
             $form->setValue(['taxonomy' => $values['id']]);
             $id = $values['id'];
         } else {
             $id = $values['taxonomy'];
         }
         if ($id === 'custom') {
             return $form;
         }
         $taxonomy = $repo->getTaxonomy($id);
         $var = $values['varname'];
         $mustSave = false;
         $php = $var . ' = Sledgehammer\\Wordpress\\Migrate::taxonomy(' . var_export($taxonomy->taxonomy, true) . ', ' . var_export($taxonomy->term->name, true) . ', ' . var_export($taxonomy->term->slug, true) . ');';
         if (count($taxonomy->term->getMeta()) > 0) {
             $php .= $var . '->setMeta(' . var_export($taxonomy->term->getMeta(), true) . ");\n";
             $mustSave = true;
         }
         $php .= "\n\n";
         //            if ($taxonomy->posts->count() > 0) {
         //                $php .= "\$repo->resolveProperty(".$var.", 'posts', ['model' => 'Taxonomy']);\n";
         //                foreach ($taxonomy->posts as $post) {
         //                    $php .= $var."->posts[] = ";
         //                    $phpPost = "\$repo->onePost(['AND', 'type' => ".var_export($post->type, true).", 'slug' => ".var_export($post->slug, true)."])";
         //                    if ($post->order == '0') {
         //                        $php .= $phpPost.";\n";
         //                    } else {
         //                        $php .= "new Sledgehammer\Orm\Junction(".$phpPost.", ['order' => ".var_export($post->order, true)."]);\n";
         //                    }
         //                }
         //                $php .= $var."->count = ".$var."->posts->count();\n";
         //                $mustSave = true;
         //            }
         if ($mustSave) {
             $php .= "\$repo->saveTaxonomy(" . $var . ");\n";
         }
         return new Template('sledgehammer/wordpress/templates/export_post.php', ['form' => $form, 'php' => $php]);
     }
     return $form;
 }
 /**
  * @param ModelConfig $config
  * @param array       $items
  */
 public function __construct($config, $items)
 {
     $this->configs[$config->name] = $config;
     $config->backendConfig = array('indexed' => count($config->id) === 0);
     reset($items);
     $row = current($items);
     if ($row !== null) {
         // The $items array is NOT empty
         if (is_object($row)) {
             $row = get_object_vars($row);
             // Convert to array
         }
         if (count($config->properties) == 0) {
             // No "column => property" mapping defined?
             // Generate a direct 1 on 1 mapping based on the first item.
             $columns = array_keys($row);
             $config->properties = array_combine($columns, $columns);
         }
         if (count($config->id) === 0 && array_key_exists('id', $row) === false) {
             // 'id' field detected in the first row?
             $config->id = array('id');
             $config->backendConfig['indexed'] = true;
             foreach (array_keys($items) as $index) {
                 $items[$index]['id'] = $index;
             }
             if (array_key_exists('id', $config->properties) == false) {
                 \Sledgehammer\array_key_unshift($config->properties, 'id', 'id');
             }
             $config->backendConfig['key'] = 'id';
         } elseif (count($config->id) == 1) {
             // Only 1 field as id?
             // Copy the data using the id as index
             $config->backendConfig['indexed'] = true;
             $indexField = $config->id[0];
             $config->backendConfig['key'] = $indexField;
             $clone = array();
             foreach ($items as $row) {
                 $key = PropertyPath::get($indexField, $row);
                 $clone[$key] = $row;
             }
             $items = $clone;
         }
     }
     $this->items = $items;
 }
Esempio n. 3
0
 public function generateContent()
 {
     Bridge::initialize();
     $repo = Repository::instance();
     $lastPosts = $repo->allPosts(['AND', 'status !=' => 'auto-draft', 'type !=' => 'revision'])->orderByDescending('id')->take(25);
     $options = $lastPosts->select(function ($post) {
         return $post->id . ') ' . $post->type . ' - ' . $post->slug;
     }, 'id')->toArray();
     \Sledgehammer\array_key_unshift($options, 'custom', 'Custom ID');
     $form = new Form(['method' => 'GET', 'fields' => ['post' => new Input(['name' => 'id', 'class' => 'form-control', 'type' => 'select', 'options' => $options, 'label' => 'Select post', 'value' => $lastPosts[0]->id]), 'id' => new Input(['name' => 'custom', 'class' => 'form-control', 'label' => 'Custom ID']), 'varname' => new Input(['name' => 'varname', 'class' => 'form-control', 'label' => 'Variable name', 'value' => '$post']), 'export_defaults' => new Input(['name' => 'export_defaults', 'type' => 'checkbox', 'label' => 'Force defaults (verbose export)'])], 'actions' => [new Button('Export', ['class' => 'btn btn-primary'])]]);
     $values = $form->import();
     if ($form->isSent()) {
         if ($values['id'] !== '') {
             $form->setValue(['post' => $values['id']]);
             $id = $values['id'];
         } else {
             $id = $values['post'];
         }
         if ($id === 'custom') {
             return $form;
         }
         $post = $repo->getPost($id);
         $var = $values['varname'];
         $defaults = $repo->createPost();
         $taxonomies = [];
         $php = '';
         foreach ($post->taxonomies as $i => $taxonomy) {
             if ($taxonomy->parent_id !== '0' || $taxonomy->description !== '' || $taxonomy->term->group !== '0' || $taxonomy->order !== '0') {
                 throw new Exception('@todo Implement taxonomy feature');
             }
             if (count($taxonomy->term->getMeta()) === 0) {
                 $taxonomies[] = 'Migrate::taxonomy(' . var_export($taxonomy->taxonomy, true) . ', ' . var_export($taxonomy->term->name, true) . ', ' . var_export($taxonomy->term->slug, true) . ')';
             } else {
                 $php .= '$taxonomy' . $i . ' = Migrate::taxonomy(' . var_export($taxonomy->taxonomy, true) . ', ' . var_export($taxonomy->term->name, true) . ', ' . var_export($taxonomy->term->slug, true) . ");\n";
                 $php .= '$taxonomy' . $i . '->setMeta(' . var_export($taxonomy->term->getMeta(), true) . ");\n";
                 $taxonomies[] = '$taxonomy' . $i;
             }
         }
         $php .= $var . " = Sledgehammer\\Wordpress\\Migrate::post([\n";
         $fields = ['type', 'title', 'slug', 'excerpt', 'status', 'content', 'comment_status', 'ping_status', 'password', 'to_ping', 'pinged', 'content_filtered', 'menu_order', 'mimetype', 'comment_count', 'parent_id', 'author', 'date', 'date_gmt', 'modified', 'modified_gmt'];
         if ($post->type === 'attachment') {
             $fields[] = 'guid';
         }
         foreach ($fields as $property) {
             if ($values['export_defaults'] === false && $defaults->{$property} === $post->{$property}) {
                 continue;
                 // skip default values
             }
             if ($property === 'author') {
                 $value = "\$repo->oneUser(['login' => " . var_export($post->author->login, true) . '])';
             } elseif ($property === 'guid') {
                 $guid = var_export($post->guid, true);
                 $value = str_replace("'" . WP_HOME, 'WP_HOME.\'', $guid);
             } elseif ($property === 'parent_id') {
                 if ($post->parent_id === '0') {
                     $value = "'0'";
                 } else {
                     $parent = $repo->getPost($post->parent_id);
                     $value = "\$repo->onePost(['AND', 'type' => " . var_export($parent->type, true) . ", 'slug' => " . var_export($parent->slug, true) . '])->id';
                 }
             } else {
                 $value = var_export($post->{$property}, true);
             }
             $php .= "    '" . $property . "' => " . $value . ",\n";
         }
         if ($taxonomies) {
             $php .= "    'taxonomies' => [\n        " . implode(",\n        ", $taxonomies) . "\n    ],\n";
         }
         $php .= "],[\n";
         $meta = $post->getMeta();
         foreach ($meta as $key => $value) {
             if (in_array($key, ['_edit_lock', '_edit_last'])) {
                 continue;
             }
             if ($key === '_thumbnail_id') {
                 $attachment = $repo->getPost($value);
                 $guid = var_export($attachment->guid, true);
                 $value = "\$repo->onePost(['AND','type' => 'attachment', 'guid' => " . str_replace("'" . WP_HOME, 'WP_HOME.\'', $guid) . '])';
             } else {
                 $value = var_export($value, true);
             }
             $php .= "    '" . $key . "' => " . $value . ",\n";
         }
         $php .= '], ';
         if ($post->type === 'attachment') {
             $php .= 'false';
         } else {
             $guid = var_export($post->guid, true);
             $guid = str_replace("'" . WP_HOME, 'WP_HOME.\'', $guid);
             $php .= str_replace('=' . $post->id . "'", "='", $guid);
         }
         $php .= ");\n";
         return new Template('sledgehammer/wordpress/templates/export_post.php', ['form' => $form, 'php' => $php]);
     }
     return $form;
 }
Esempio n. 4
0
 /**
  * Return a new collection sorted by the given field in descending order.
  *
  * @param string $selector
  * @param int    $method   The sorting method, options are: SORT_REGULAR, SORT_NUMERIC, SORT_STRING or SORT_NATURAL
  *
  * @return Collection
  */
 public function orderByDescending($selector, $method = SORT_REGULAR)
 {
     if ($this->data === null && $method == SORT_REGULAR && is_string($selector) && is_string($this->sql) === false && is_array($this->sql->orderBy) && $this->sql->limit === false && $this->sql->offset == 0) {
         $sql = clone $this->sql;
         \Sledgehammer\array_key_unshift($sql->orderBy, $selector, 'DESC');
         return new self($sql, $this->dbLink);
     }
     return parent::orderByDescending($selector, $method);
 }