1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
<?php
require_once APP . DS . 'vendors' . DS . 'shells'. DS . 'bedita_base.php';
class PublicationShell extends BeditaBaseShell {
protected $objDefaults = array(
'status' => 'on',
'user_created' => '1',
'user_modified' => '1',
'lang' => 'ita',
'ip_created' => '127.0.0.1',
'syndicate' => 'off',
'body' => 'Lorem ipsum dolor sit amet, consectetur adipisci elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisci elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisci elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
);
private $options = array(
'depth' => 3,
'sublevel-sections' => 3,
'leaf-documents' => 1
);
private $objectFakeId = 1;
public function create() {
$this->hr();
$this->trackInfo('Create start');
if (isset($this->params['d'])) {
$this->options['depth'] = $this->params['d'];
}
if (isset($this->params['ns'])) {
$this->options['sublevel-sections'] = $this->params['ns'];
}
if (isset($this->params['nd'])) {
$this->options['leaf-documents'] = $this->params['nd'];
}
$optionsString = '';
foreach ($this->options as $key => $value) {
$optionsString .= ' | ' . $key . ': ' . $value;
}
$this->trackInfo('Options: ' . $optionsString);
try {
$this->createPublication();
} catch(BeditaException $e) {
$this->trackInfo('Exception: ' . $e->getMessage());
}
$this->trackInfo('Create end');
}
public function createContents() {
$this->hr();
$this->trackInfo('Create contents start');
if (isset($this->params['t'])) {
$this->options['objectType'] = $this->params['t'];
} else {
$this->options['objectType'] = 'Document';
}
if (isset($this->params['n'])) {
$this->options['number'] = $this->params['n'];
} else {
$this->options['number'] = 1;
}
if (isset($this->params['pid'])) {
$this->options['parentId'] = $this->params['pid'];
}
if (isset($this->params['tpf'])) {
$this->options['titlePostFix'] = $this->params['tpf'];
}
if (isset($this->params['uri'])) {
$this->options['uri'] = $this->params['uri'];
}
$optionsString = '';
foreach ($this->options as $key => $value) {
$optionsString .= ' | ' . $key . ': ' . $value;
}
$this->trackInfo('Options: ' . $optionsString);
try {
$this->createMulti($this->options);
} catch(BeditaException $e) {
$this->trackInfo('Exception: ' . $e->getMessage());
}
$this->trackInfo('Create contents end');
}
private function createPublication() {
$depth = 1;
$publicationId = $this->createObject(null, 'Area', 'publication');
if ($depth == $this->options['depth']) {
for ($j=0; $j<$this->options['leaf-documents']; $j++) {
$this->createObject($publicationId, 'Document', 'publication-' . $publicationId . '-depth-' . $depth .'-document-' . ($j+1));
}
} else if ($depth < $this->options['depth']) {
$depth++;
for ($i=0; $i<$this->options['sublevel-sections']; $i++) {
$this->createSection($publicationId, $depth, 'section-' . ($i+1) . '-depth-' . $depth);
}
}
}
private function createSection($parentId, $depth = 1, $nickname = 'section-1') {
$sectionId = $this->createObject($parentId, 'Section', $nickname);
if ($depth == $this->options['depth']) {
for ($j=0; $j<$this->options['leaf-documents']; $j++) {
$this->createObject($sectionId, 'Document', 'section-' . $sectionId . '-depth-' . $depth .'-document-' . ($j+1));
}
} else if ($depth < $this->options['depth']) {
$depth++;
for ($i=0; $i<$this->options['sublevel-sections']; $i++) {
$this->createSection($sectionId, $depth, 'section-' . ($i+1) . '-depth-' . $depth);
}
}
}
private function createObject($parentId, $objectType = 'Document', $nickname = 'document-1', $title = null, $uri = null) {
$data = array(
'name' => $objectType . ' ' . $nickname,
'title' => ($title!=null) ? $title : $objectType . ' ' . $nickname,
'nickname' => $nickname
);
if ($parentId != null) {
$data['parent_id'] = $parentId;
}
if ($uri != null) {
$data['uri'] = $uri;
}
$data = array_merge($data, $this->objDefaults);
$model = ClassRegistry::init($objectType);
$model->create();
if (!$model->save($data)) {
throw new BeditaException('error saving ' . $objectType);
}
if (!empty($parentId)) {
$tree = ClassRegistry::init('Tree');
$tree->appendChild($model->id, $parentId);
}
$this->trackInfo('create' . $objectType . ':::id ' . $model->id . ' | type ' . $objectType);
return $model->id;
}
private function createMulti($params) {
$parentId = (!empty($params['parentId'])) ? $params['parentId'] : null;
$numElems = $params['number'];
for($i=0; $i<$numElems; $i++) {
$index = $i+1;
$title = $params['objectType'] . ' ' . $index;
if (!empty($params['titlePostFix'])) {
$title.= ' ' . $params['titlePostFix'];
}
$nickname = strtolower($params['objectType']) . '-' . $index;
if (!empty($params['titlePostFix'])) {
$nickname.= '-' . strtolower($params['titlePostFix']);
}
$uri = null;
if (!empty($params['uri'])) {
$uri = $params['uri'];
}
$this->createObject($parentId, $params['objectType'], $nickname, $title, $uri);
}
}
public function help() {
$this->hr();
$this->out('publication script shell usage:');
$this->out('');
$this->out('./cake.sh publication create [-d <depth> [-ns <sublevel-number-of-sections>] [-nd <leafs-number-of-documents>]');
$this->out('./cake.sh publication createContents [-t <type>] [-n <number>] [-pid <parentId>] [-tpf <titlePostFix>] [-uri <uriInsideMediaFolder>]');
$this->out('');
}
private function trackInfo($s, $param = null) {
echo $s . "\n";
}
}
?>