1 <?php
 2 
 3 /**
 4  * @package redaxo\structure\version
 5  */
 6 class rex_article_revision
 7 {
 8     const LIVE = 0; // live revision
 9     const WORK = 1; // working copy
10 
11     public static function copyContent($article_id, $clang, $from_revision_id, $to_revision_id)
12     {
13         if ($from_revision_id == $to_revision_id) {
14             return false;
15         }
16 
17         // clear the revision to which we will later copy all slices
18         $dc = rex_sql::factory();
19         // $dc->setDebug();
20         $dc->setQuery('delete from ' . rex::getTablePrefix() . 'article_slice where article_id=? and clang_id=? and revision=?', [$article_id, $clang, $to_revision_id]);
21 
22         $gc = rex_sql::factory();
23         $gc->setQuery('select * from ' . rex::getTablePrefix() . 'article_slice where article_id=? and clang_id=? and revision=? ORDER by ctype_id, priority', [$article_id, $clang, $from_revision_id]);
24 
25         $cols = rex_sql::factory();
26         $cols->setquery('SHOW COLUMNS FROM ' . rex::getTablePrefix() . 'article_slice');
27         foreach ($gc as $slice) {
28             $ins = rex_sql::factory();
29             // $ins->setDebug();
30             $ins->setTable(rex::getTablePrefix() . 'article_slice');
31 
32             foreach ($cols as $col) {
33                 $colname = $col->getValue('Field');
34                 $ins->setValue($colname, $slice->getValue($colname));
35             }
36 
37             $ins->setValue('id', 0); // trigger auto increment
38             $ins->setValue('revision', $to_revision_id);
39             $ins->addGlobalCreateFields();
40             $ins->addGlobalUpdateFields();
41             $ins->insert();
42         }
43 
44         rex_article_cache::delete($article_id);
45         return true;
46     }
47 
48     public static function clearContent($article_id, $clang, $from_revision_id)
49     {
50         if (self::WORK != $from_revision_id) {
51             throw new InvalidArgumentException(sprintf('Revision "%s" can not be cleared, only the working version (%d).', $from_revision_id, self::WORK));
52         }
53 
54         $dc = rex_sql::factory();
55         // $dc->setDebug();
56         $dc->setQuery('delete from ' . rex::getTablePrefix() . 'article_slice where article_id=? and clang_id=? and revision=?', [$article_id, $clang, $from_revision_id]);
57 
58         return true;
59     }
60 }
61