1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Pages;
13
14 use ICanBoogie\I18n\FormattedString;
15
16 class CopyOperation extends \ICanBoogie\Operation
17 {
18 protected function get_controls()
19 {
20 return array
21 (
22 self::CONTROL_PERMISSION => Module::PERMISSION_CREATE,
23 self::CONTROL_RECORD => true
24 )
25
26 + parent::get_controls();
27 }
28
29 protected function process()
30 {
31 global $core;
32
33 $record = $this->record;
34 $key = $this->key;
35 $title = $record->title;
36
37 unset($record->nid);
38 unset($record->is_online);
39 unset($record->created_at);
40 unset($record->updated_at);
41
42 $record->uid = $core->user_id;
43 $record->title .= ' (copie)';
44 $record->slug .= '-copie';
45
46 $contentsModel = $this->module->model('contents');
47 $contents = $contentsModel->where(array('pageid' => $key))->all;
48
49 $nid = $this->module->model->save((array) $record);
50
51 if (!$nid)
52 {
53 \ICanBoogie\log_error('Unable to copy page %title (#:nid)', array('title' => $title, 'nid' => $key));
54
55 return;
56 }
57
58 $this->response->message = new FormattedString('Page %title was copied to %copy', array('title' => $title, 'copy' => $record->title));
59
60 foreach ($contents as $record)
61 {
62 $record->pageid = $nid;
63 $record = (array) $record;
64
65 $contentsModel->insert
66 (
67 $record,
68
69 array
70 (
71 'on duplicate' => $record
72 )
73 );
74 }
75
76 return array($key, $nid);
77 }
78 }