Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • None
  • system
    • base
    • caching
    • console
    • db
      • ar
      • schema
    • validators
    • web
      • actions
      • auth
      • helpers
      • widgets
        • captcha
        • pagers
  • zii
    • widgets
      • grid

Classes

  • DocsController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /*****************************************************************************************
  4:  * X2Engine Open Source Edition is a customer relationship management program developed by
  5:  * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  6:  * 
  7:  * This program is free software; you can redistribute it and/or modify it under
  8:  * the terms of the GNU Affero General Public License version 3 as published by the
  9:  * Free Software Foundation with the addition of the following permission added
 10:  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 11:  * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
 12:  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 13:  * 
 14:  * This program is distributed in the hope that it will be useful, but WITHOUT
 15:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 16:  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 17:  * details.
 18:  * 
 19:  * You should have received a copy of the GNU Affero General Public License along with
 20:  * this program; if not, see http://www.gnu.org/licenses or write to the Free
 21:  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 22:  * 02110-1301 USA.
 23:  * 
 24:  * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
 25:  * California 95067, USA. or at email address contact@x2engine.com.
 26:  * 
 27:  * The interactive user interfaces in modified source and object code versions
 28:  * of this program must display Appropriate Legal Notices, as required under
 29:  * Section 5 of the GNU Affero General Public License version 3.
 30:  * 
 31:  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 32:  * these Appropriate Legal Notices must retain the display of the "Powered by
 33:  * X2Engine" logo. If the display of the logo is not reasonably feasible for
 34:  * technical reasons, the Appropriate Legal Notices must display the words
 35:  * "Powered by X2Engine".
 36:  *****************************************************************************************/
 37: 
 38: /**
 39:  * @package application.modules.docs.controllers
 40:  */
 41: class DocsController extends x2base {
 42: 
 43:     public $modelClass = 'Docs';
 44: 
 45:     /**
 46:      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
 47:      * using two-column layout. See 'protected/views/layouts/column2.php'.
 48:      */
 49:     // public $layout='//layouts/column2';
 50: 
 51:     /**
 52:      * @return array action filters
 53:      */
 54: 
 55:     /**
 56:      * Specifies the access control rules.
 57:      * This method is used by the 'accessControl' filter.
 58:      * @return array access control rules
 59:      */
 60:     public function accessRules() {
 61:         return array(
 62:             array('allow',
 63:                 'users' => array('*'),
 64:             ),
 65:             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 66:                 'actions' => array('index', 'view', 'create', 'createEmail', 'update', 'exportToHtml', 'delete', 'getItems', 'getItem'),
 67:                 'users' => array('@'),
 68:             ),
 69:             array('allow', // allow admin user to perform 'admin' and 'delete' actions
 70:                 'actions' => array('admin'),
 71:                 'users' => array('admin'),
 72:             ),
 73:             array('deny', // deny all users
 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:      * Displays a particular model.
 92:      * @param integer $id the ID of the model to be displayed
 93:      */
 94:     public function actionView($id) {
 95:         $model = $this->loadModel($id);
 96:         if (!$this->checkPermissions($model, 'view'))
 97:             $this->denied();
 98: 
 99:         // add doc to user's recent item list
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:      * Displays a particular model.
109:      * @param integer $id the ID of the model to be displayed
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:      * Creates a new doc.
131:      * If creation is successful, the browser will be redirected to the 'view' page.
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:         // Uncomment the following line if AJAX validation is needed
147:         // $this->performAjaxValidation($model);
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:      * Creates an email template.
162:      * If creation is successful, the browser will be redirected to the 'view' page.
163:      */
164:     public function actionCreateEmail() {
165:         $model = new Docs;
166:         $model->type = 'email';
167:         $model->associationType = 'Contacts';
168: 
169:         // Uncomment the following line if AJAX validation is needed
170:         // $this->performAjaxValidation($model);
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:      * Download an exported doc file.
230:      * @param type $uid Unique ID associated with the file
231:      * @param type $id ID of the doc exported
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:      * Updates a particular model.
281:      * If update is successful, the browser will be redirected to the 'view' page.
282:      * @param integer $id the ID of the model to be updated
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:      * Deletes a particular model.
319:      * If deletion is successful, the browser will be redirected to the 'admin' page.
320:      * @param integer $id the ID of the model to be deleted
321:      */
322:     public function actionDelete($id) {
323:         if (Yii::app()->request->isPostRequest) {
324:             // we only allow deletion via POST request
325:             $model = $this->loadModel($id);
326:             $this->cleanUpTags($model);
327:             $model->delete();
328: 
329:             // if AJAX request (triggered by deletion via admin grid view), we should not redirect 
330:             // the browser
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:      * Lists all models.
340:      * @param integer $id the ID of the folder to display
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:      * Performs the AJAX validation.
451:      * @param CModel the model to be validated
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:             // $model = $this->updateChangeLog($model,'Edited');
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:      * Create a menu for Docs
490:      * @param array Menu options to remove
491:      * @param X2Model Model object passed to the view
492:      * @param array Additional menu parameters
493:      */
494:     public function insertMenu($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:          * To show all options:
502:          * $menuOptions = array(
503:          *     'index', 'create', 'createEmail', 'createQuote', 'view', 'edit', 'delete',
504:          *     'permissions', 'exportToHtml', 'import', 'export',
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: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0