Пример #1
0
 /**
  * Restore an older version of a page.
  *
  * Creates a new version based on the old one.
  *
  * @param Model $version
  *
  * @return Model
  */
 public function restore(Model $version)
 {
     $attrs = $version->toArray();
     $newVersion = new Model($attrs);
     $newVersion->setEditedAt(new DateTime('now'))->setEditedBy(Auth::user())->setRestoredFrom($version)->save();
     $types = Chunk::since($version);
     foreach ($types as $type => $chunks) {
         $className = Chunk::getModelName($type);
         foreach ($chunks as $chunk) {
             $old = Chunk::find($type, $chunk->slotname, $version);
             $new = new $className();
             $new->page_id = $newVersion->getPageId();
             $new->slotname = $chunk->slotname;
             $new->page_vid = $newVersion->getId();
             $new->save();
             if ($old !== null) {
                 $new->fill(array_except($old->toArray(), ['page_vid']));
                 $new->save();
             }
         }
     }
     return $newVersion;
 }