Example #1
0
 public function create($uid, $template, $content = array())
 {
     if (empty($template)) {
         throw new Exception(l('pages.add.error.template'));
     }
     $uid = empty($uid) ? str::random(32) : $uid;
     $blueprint = new Blueprint($template);
     $data = array();
     foreach ($blueprint->fields(null) as $key => $field) {
         $data[$key] = $field->default();
     }
     $data = array_merge($data, $content);
     // create the new page and convert it to a page model
     $page = new Page($this->page, parent::create($uid, $template, $data)->dirname());
     if (!$page) {
         throw new Exception(l('pages.add.error.create'));
     }
     kirby()->trigger('panel.page.create', $page);
     // subpage builder
     foreach ((array) $page->blueprint()->pages()->build() as $build) {
         $missing = a::missing($build, array('title', 'template', 'uid'));
         if (!empty($missing)) {
             continue;
         }
         $subpage = $page->children()->create($build['uid'], $build['template'], array('title' => $build['title']));
         if (isset($build['num'])) {
             $subpage->sort($build['num']);
         }
     }
     return $page;
 }
Example #2
0
 public function prepareForNewTemplate($oldTemplate, $newTemplate, $language = null)
 {
     $data = array();
     $incompatible = array();
     $content = $this->content($language);
     $oldBlueprint = new Blueprint($oldTemplate);
     $oldFields = $oldBlueprint->fields($this);
     $newBlueprint = new Blueprint($newTemplate);
     $newFields = $newBlueprint->fields($this);
     // log
     $removed = array();
     $replaced = array();
     $added = array();
     // first overwrite everything
     foreach ($oldFields as $oldField) {
         $data[$oldField->name()] = null;
     }
     // now go through all new fileds and compare them to the old field types
     foreach ($newFields as $newField) {
         $oldField = $oldFields->{$newField->name()};
         // only take data from fields with matching names and types
         if ($oldField and $oldField->type() == $newField->type()) {
             $data[$newField->name()] = $content->get($newField->name())->value();
         } else {
             $data[$newField->name()] = $newField->default();
             if ($oldField) {
                 $replaced[$newField->name()] = $newField->label();
             } else {
                 $added[$newField->name()] = $newField->label();
             }
         }
     }
     foreach ($data as $name => $content) {
         if (is_null($content)) {
             $removed[$name] = $oldFields->{$name}->label();
         }
     }
     return array('data' => $data, 'removed' => $removed, 'replaced' => $replaced, 'added' => $added);
 }