1: <?php
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: class DocsController extends x2base {
42:
43: public $modelClass = 'Docs';
44:
45: 46: 47: 48:
49:
50:
51: 52: 53:
54:
55: 56: 57: 58: 59:
60: public function accessRules() {
61: return array(
62: array('allow',
63: 'users' => array('*'),
64: ),
65: array('allow',
66: 'actions' => array('index', 'view', 'create', 'createEmail', 'update', 'exportToHtml', 'delete', 'getItems', 'getItem'),
67: 'users' => array('@'),
68: ),
69: array('allow',
70: 'actions' => array('admin'),
71: 'users' => array('admin'),
72: ),
73: array('deny',
74: 'users' => array('*'),
75: ),
76: );
77: }
78:
79: public function actionGetItems($term) {
80: X2LinkableBehavior::getItems($term);
81: }
82:
83: public function actionGetItem($id) {
84: $model = $this->loadModel($id);
85: if ((($model->visibility == 1 || ($model->visibility == 0 && $model->createdBy == Yii::app()->user->getName())) || Yii::app()->params->isAdmin)) {
86: echo $model->text;
87: }
88: }
89:
90: 91: 92: 93:
94: public function actionView($id) {
95: $model = $this->loadModel($id);
96: if (!$this->checkPermissions($model, 'view'))
97: $this->denied();
98:
99:
100: User::addRecentItem('d', $id, Yii::app()->user->getId());
101: X2Flow::trigger('RecordViewTrigger', array('model' => $model));
102: $this->render('view', array(
103: 'model' => $model,
104: ));
105: }
106:
107: 108: 109: 110:
111: public function actionFullView($id, $json = 0, $replace = 0) {
112: $model = $this->loadModel($id);
113: $response = array(
114: 'body' => $model->text,
115: 'subject' => $model->subject,
116: 'to' => $model->emailTo
117: );
118: if ($replace)
119: foreach (array_keys($response) as $key)
120: $response[$key] = str_replace('{signature}', Yii::app()->params->profile->signature, $response[$key]);
121: if ($json) {
122: header('Content-type: application/json');
123: echo json_encode($response);
124: } else {
125: echo $response['body'];
126: }
127: }
128:
129: 130: 131: 132:
133: public function actionCreate($duplicate = false) {
134: $model = new Docs;
135:
136: if ($duplicate) {
137: $copiedModel = Docs::model()->findByPk($duplicate);
138: if (!empty($copiedModel)) {
139: foreach ($copiedModel->attributes as $name => $value)
140: if ($name != 'id')
141: $model->$name = $value;
142: }
143: $model->name .= ' (' . Yii::t('docs', 'copy') . ')';
144: }
145:
146:
147:
148:
149: if (isset($_POST['Docs'])) {
150: $model->setX2Fields($_POST['Docs']);
151: if ($model->save())
152: $this->redirect(array('view', 'id' => $model->id));
153: }
154:
155: $this->render('create', array(
156: 'model' => $model,
157: ));
158: }
159:
160: 161: 162: 163:
164: public function actionCreateEmail() {
165: $model = new Docs;
166: $model->type = 'email';
167: $model->associationType = 'Contacts';
168:
169:
170:
171:
172: if (isset($_POST['Docs'])) {
173: $model->setX2Fields($_POST['Docs']);
174: if ($model->save()) {
175: if (isset($_GET['ajax']) && $_GET['ajax']) {
176: echo CJSON::encode($model->attributes);
177: return;
178: }
179: $this->redirect(array('view', 'id' => $model->id));
180: }
181: }
182:
183: $this->render('create', array(
184: 'model' => $model,
185: ));
186: }
187:
188: public function actionCreateQuote() {
189: $model = new Docs;
190: $model->type = 'quote';
191:
192: if (isset($_POST['Docs'])) {
193: $model->setX2Fields($_POST['Docs']);
194: if ($model->save())
195: $this->redirect(array('view', 'id' => $model->id));
196: }
197:
198: $this->render('create', array(
199: 'model' => $model,
200: ));
201: }
202:
203: public function actionExportToHtml($id) {
204: $model = $this->loadModel($id);
205: $file = $this->safePath(($uid = uniqid()) . '-doc.html');
206: $fp = fopen($file, 'w+');
207: $data = "<style>
208: #wrap{
209: width:6.5in;
210: height:9in;
211: margin-top:auto;
212: margin-left:auto;
213: margin-bottom:auto;
214: margin-right:auto;
215: }
216: </style>
217: <div id='wrap'>
218: " . $model->text . "</div>";
219: fwrite($fp, $data);
220: fclose($fp);
221: $link = CHtml::link(Yii::t('app', 'Download') . '!', array('downloadExport', 'uid' => $uid, 'id' => $id));
222: $this->render('export', array(
223: 'model' => $model,
224: 'link' => $link,
225: ));
226: }
227:
228: 229: 230: 231: 232:
233: public function actionDownloadExport($uid, $id) {
234: if (file_exists($this->safePath($filename = $uid . '-doc.html'))) {
235: $this->sendFile($filename, false);
236: } else {
237: $this->redirect(array('exportToHtml', 'id' => $id));
238: }
239: }
240:
241: public function titleUpdate($old_title, $new_title) {
242: if ((sizeof(Modules::model()->findAllByAttributes(array('name' => $new_title))) == 0) && ($old_title != $new_title)) {
243: Yii::app()->db->createCommand()->update('x2_modules', array('title' => $new_title,), 'title=:old_title', array(':old_title' => $old_title));
244: }
245: }
246:
247: public function actionGetFolderSelector ($id=null, array $selectedFolders=array ()) {
248: if (!$id) $id = 'root';
249: if (is_numeric ($id)) {
250: $folder = DocFolders::model ()->findByPk ($id);
251: if (!$folder)
252: throw new CHttpException(
253: 404, Yii::t('app', 'The requested page does not exist.'));
254: } elseif ($id === 'root') {
255: $folder = $id;
256: } else {
257: throw new CHttpException(
258: 400, Yii::t('app', 'Bad request'));
259: }
260: $children = DocFolders::model ()->findChildren ($folder, array (
261: 'folder'
262: ), array (
263: DocFolders::TEMPLATES_FOLDER_ID,
264: $id
265: ));
266: $dataProvider = new CArrayDataProvider ($children, array (
267: 'id' => 'folder-selector',
268: 'pagination' => array (
269: 'pageSize' => 10,
270: )
271: ));
272: $this->renderPartial ('_folderSelector', array (
273: 'dataProvider' => $dataProvider,
274: 'folder' => $folder,
275: 'selectedFolders' => $selectedFolders,
276: ), false, true);
277: }
278:
279: 280: 281: 282: 283:
284: public function actionUpdate($id) {
285: $model = $this->loadModel($id);
286: if ($model->type == null) {
287: $model->scenario = 'menu';
288: }
289: $old_title = $model->name;
290: $new_title = $old_title;
291:
292: if (isset($_POST['Docs'])) {
293: $new_title = $_POST['Docs']['name'];
294: }
295: if (isset($_POST['Docs'])) {
296: $model->attributes = $_POST['Docs'];
297: $model->visibility = $_POST['Docs']['visibility'];
298: if ($model->save()) {
299: $this->titleUpdate($old_title, $new_title);
300: $event = new Events;
301: $event->associationType = 'Docs';
302: $event->associationId = $model->id;
303: $event->type = 'doc_update';
304: $event->user = Yii::app()->user->getName();
305: $event->visibility = $model->visibility;
306: $event->save();
307: $this->redirect(
308: array('update', 'id' => $model->id, 'saved' => true, 'time' => time()));
309: }
310: }
311:
312: $this->render('update', array(
313: 'model' => $model,
314: ));
315: }
316:
317: 318: 319: 320: 321:
322: public function actionDelete($id) {
323: if (Yii::app()->request->isPostRequest) {
324:
325: $model = $this->loadModel($id);
326: $this->cleanUpTags($model);
327: $model->delete();
328:
329:
330:
331: if (!isset($_GET['ajax']))
332: $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
333: } else
334: throw new CHttpException(
335: 400, 'Invalid request. Please do not repeat this request again.');
336: }
337:
338: 339: 340: 341:
342: public function actionIndex($id = null) {
343: $model = new DocFolders;
344: $model->parentFolder = $id;
345: if (Yii::app()->request->isAjaxRequest && isset($_POST['DocFolders'])) {
346: $model->setAttributes($_POST['DocFolders']);
347: if($model->parentFolder == 0){
348: $model->parentFolder = null;
349: }
350: if ($model->save()) {
351: echo CJSON::encode (array (
352: 'success' => 1
353: ));
354: }else{
355: $form = $this->renderPartial ('_folderCreate', array (
356: 'model' => $model
357: ), true, true);
358: echo CJSON::encode (array (
359: 'form' => $form
360: ));
361: Yii::app()->end ();
362: }
363: }else{
364: if(empty($id)){
365: $folderDataProvider = DocFolders::model()->getRootFolderContents();
366: } elseif ($id == -1) {
367: $folderDataProvider = DocFolders::model ()->getTemplatesFolderContents();
368: } else {
369: $folder = DocFolders::model()->findByPk($id);
370: if(!$this->checkPermissions($folder,'view')){
371: $this->denied();
372: }
373: if(isset($folder)){
374: $folderDataProvider = $folder->getContents();
375: }else{
376: throw new CHttpException(
377: 404, Yii::t('app', 'The requested page does not exist.'));
378: }
379: }
380: $attachments = new CActiveDataProvider('Media', array(
381: 'criteria' => array(
382: 'order' => 'createDate DESC',
383: 'condition' => 'associationType="docs"'
384: )));
385:
386: $this->render('index', array(
387: 'currentFolder' => $id,
388: 'model' => $model,
389: 'folderDataProvider' => $folderDataProvider,
390: 'attachments' => $attachments,
391: ));
392: }
393: }
394:
395: public function actionMoveFolder($type, $objId, $destId = null){
396: if($destId == -1){
397: $destination = null;
398: } else {
399: $destination = DocFolders::model()->findByPk($destId);
400: }
401:
402: if($type==='doc'){
403: $model = Docs::model()->findByPk($objId);
404: }elseif($type==='folder'){
405: $model = DocFolders::model()->findByPk($objId);
406: }
407: if(!isset($model)){
408: throw new CHttpException(404, Yii::t('docs','Object or destination not found.'));
409: }
410: if(!$this->checkPermissions($model,'edit') ||
411: ($destination instanceof DocFolders) &&
412: !$this->checkPermissions($destination,'edit')){
413:
414: $this->denied();
415: }
416: if ($model->moveTo ($destination)) {
417: echo 1;
418: }
419:
420: }
421:
422: public function actionDeleteFileFolder() {
423: if (Yii::app()->request->isAjaxRequest && isset($_POST['type'], $_POST['id'])) {
424: if ($_POST['type'] === 'folder') {
425: $model = DocFolders::model()->findByPk($_POST['id']);
426: if (is_null($model)) {
427: throw new CHttpException(404, 'Folder not found.');
428: }
429: if (!$model->checkRecursiveDeletePermissions()) {
430: $this->denied();
431: }
432: } elseif ($_POST['type'] === 'doc') {
433: $model = Docs::model()->findByPk($_POST['id']);
434: if (is_null($model)) {
435: throw new CHttpException(404, 'File not found.');
436: }
437: if (!$this->checkPermissions($model, 'delete')) {
438: $this->denied();
439: }
440: } else {
441: throw new CHttpException(400, 'Bad request.');
442: }
443: $model->delete();
444: } else {
445: throw new CHttpException(400, 'Bad request.');
446: }
447: }
448:
449: 450: 451: 452:
453: protected function performAjaxValidation($model) {
454: if (isset($_POST['ajax']) && $_POST['ajax'] === 'docs-form') {
455: echo CActiveForm::validate($model);
456: Yii::app()->end();
457: }
458: }
459:
460: public function actionAutosave($id) {
461: $model = $this->loadModel($id);
462:
463: $old_title = $model->name;
464: $new_title = $old_title;
465: if (isset($_POST['Docs'])) {
466: $new_title = $_POST['Docs']['name'];
467: }
468:
469: if (isset($_POST['Docs'])) {
470: $model->attributes = $_POST['Docs'];
471:
472:
473: if ($model->save()) {
474: if ($old_title != $new_title) {
475: $this->titleUpdate($old_title, $new_title);
476: }
477: echo Yii::t('docs', 'Saved at') . ' ' . Yii::app()->dateFormatter->format(Yii::app()->locale->getTimeFormat('medium'), time());
478: };
479: }
480: }
481:
482: public function behaviors() {
483: return array_merge(parent::behaviors(), array(
484: 'ImportExportBehavior' => array('class' => 'ImportExportBehavior'),
485: ));
486: }
487:
488: 489: 490: 491: 492: 493:
494: public function ($selectOptions = array(), $model = null, $menuParams = null) {
495: $Docs = Modules::displayName();
496: $Doc = Modules::displayName(false);
497: $user = Yii::app()->user->name;
498: $modelId = isset($model) ? $model->id : 0;
499:
500: 501: 502: 503: 504: 505: 506:
507: $menuItems = array(
508: array(
509: 'name' => 'index',
510: 'label' => Yii::t('docs', 'List {module}', array(
511: '{module}' => $Docs,
512: )),
513: 'url' => array('index')
514: ),
515: array(
516: 'name' => 'create',
517: 'label' => Yii::t('docs', 'Create {module}', array(
518: '{module}' => $Doc,
519: )),
520: 'url' => array('create')
521: ),
522: array(
523: 'name' => 'createEmail',
524: 'label' => Yii::t('docs', 'Create Email'),
525: 'url' => array('createEmail')
526: ),
527: array(
528: 'name' => 'createQuote',
529: 'label' => Yii::t('docs', 'Create {quote}', array(
530: '{quote}' => Modules::displayName(false, "Quotes"),
531: )),
532: 'url' => array('createQuote')
533: ),
534: array(
535: 'name' => 'view',
536: 'label' => Yii::t('docs', 'View'),
537: 'url' => array('view', 'id' => $modelId)
538: ),
539: array(
540: 'name' => 'edit',
541: 'label' => Yii::t('docs', 'Edit {doc}', array(
542: '{doc}' => $Doc,
543: )),
544: 'url' => array('update', 'id' => $modelId)
545: ),
546: array(
547: 'name' => 'delete',
548: 'label' => Yii::t('docs', 'Delete {doc}', array(
549: '{doc}' => $Doc,
550: )),
551: 'url' => 'javascript:void(0);',
552: 'linkOptions' => array(
553: 'submit' => array('delete', 'id' => $modelId),
554: 'confirm' => Yii::t('docs', 'Are you sure you want to delete this item?')
555: ),
556: ),
557: array(
558: 'name' => 'exportToHtml',
559: 'label' => Yii::t('docs', 'Export {doc}', array(
560: '{doc}' => $Doc,
561: )),
562: 'url' => array('exportToHtml', 'id' => $modelId)
563: ),
564: array(
565: 'name' => 'import',
566: 'label' => Yii::t('docs', 'Import {module}', array(
567: '{module}' => $Docs,
568: )),
569: 'url' => array('admin/importModels', 'model' => 'Docs'),
570: ),
571: array(
572: 'name' => 'export',
573: 'label' => Yii::t('docs', 'Export {module}', array(
574: '{module}' => $Docs,
575: )),
576: 'url' => array('admin/exportModels', 'model' => 'Docs'),
577: ),
578: );
579:
580: $this->prepareMenu($menuItems, $selectOptions);
581: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
582: }
583:
584: }
585: