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

  • AccountsController
  • 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.accounts.controllers
 40:  */
 41: class AccountsController extends x2base {
 42: 
 43:     public $modelClass = 'Accounts';
 44: 
 45:     public function accessRules() {
 46:         return array(
 47:             array('allow',
 48:                 'actions' => array('getItems'),
 49:                 'users' => array('*'),
 50:             ),
 51:             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 52:                 'actions' => array('index', 'view', 'create', 'update', 'search', 'addUser', 'removeUser',
 53:                     'addNote', 'deleteNote', 'saveChanges', 'delete', 'shareAccount', 'inlineEmail', 'qtip'),
 54:                 'users' => array('@'),
 55:             ),
 56:             array('allow', // allow admin user to perform 'admin' and 'delete' actions
 57:                 'actions' => array('admin', 'testScalability'),
 58:                 'users' => array('admin'),
 59:             ),
 60:             array('deny', // deny all users
 61:                 'users' => array('*'),
 62:             ),
 63:         );
 64:     }
 65: 
 66:     public function behaviors() {
 67:         return array_merge(parent::behaviors(), array(
 68:             'X2MobileControllerBehavior' => array(
 69:                 'class' => 
 70:                     'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
 71:             ),
 72:             'QuickCreateRelationshipBehavior' => array(
 73:                 'class' => 'QuickCreateRelationshipBehavior',
 74:                 'attributesOfNewRecordToUpdate' => array(
 75:                     'Contacts' => array(
 76:                         'nameId' => 'company',
 77:                         'website' => 'website',
 78:                         'phone' => 'phone',
 79:                     ),
 80:                     'Opportunity' => array(
 81:                         'accountName' => 'id',
 82:                     )
 83:                 )
 84:             ),
 85:         ));
 86:     }
 87: 
 88:     public function actions() {
 89:         return array_merge(parent::actions(), array(
 90:             'inlineEmail' => array(
 91:                 'class' => 'InlineEmailAction',
 92:             ),
 93:             'accountsReport' => array(
 94:                 'class' => 'AccountsReportAction',
 95:             ),
 96:             'exportAccountsReport' => array(
 97:                 'class' => 'ExportAccountsReportAction',
 98:             ),
 99:             'accountsCampaign' => array(
100:                 'class' => 'AccountCampaignAction',
101:             ),
102:         ));
103:     }
104: 
105:     public function actionGetItems($term) {
106:         X2LinkableBehavior::getItems ($term);
107:     }
108: 
109:     /**
110:      * Displays a particular model.
111:      * @param integer $id the ID of the model to be displayed
112:      */
113:     public function actionView($id) {
114:         $model = $this->loadModel($id);
115:         if (!parent::checkPermissions($model, 'view'))
116:             $this->denied();
117: 
118:         // add account to user's recent item list
119:         User::addRecentItem('a', $id, Yii::app()->user->getId());
120:         if ($model->checkForDuplicates()) {
121:             $this->redirect($this->createUrl('/site/duplicateCheck', array(
122:                         'moduleName' => 'accounts',
123:                         'modelName' => 'Accounts',
124:                         'id' => $id,
125:                         'ref' => 'view',
126:             )));
127:         } else {
128:             $model->duplicateChecked();
129:             parent::view($model, 'accounts');
130:         }
131:     }
132: 
133:     public function actionShareAccount($id) {
134: 
135:         $model = $this->loadModel($id);
136:         $body = "\n\n\n\n".Yii::t('accounts', '{module} Record Details', array(
137:             '{module}'=>Modules::displayName(false)
138:         ))." <br />
139: <br />".Yii::t('accounts', 'Name').": $model->name
140: <br />".Yii::t('accounts', 'Description').": $model->description
141: <br />".Yii::t('accounts', 'Revenue').": $model->annualRevenue
142: <br />".Yii::t('accounts', 'Phone').": $model->phone
143: <br />".Yii::t('accounts', 'Website').": $model->website
144: <br />".Yii::t('accounts', 'Type').": $model->type
145: <br />".Yii::t('app', 'Link') . ": " . CHtml::link($model->name,
146:             array('/accounts/accounts/view', 'id'=>$model->id));
147:         $body = trim($body);
148: 
149:         $errors = array();
150:         $status = array();
151:         $email = array();
152:         if (isset($_POST['email'], $_POST['body'])) {
153: 
154:             $subject = Yii::t('accounts', "Account Record") . ": $model->name";
155:             $email['to'] = $this->parseEmailTo($this->decodeQuotes($_POST['email']));
156:             $body = $_POST['body'];
157:             // if(empty($email) || !preg_match("/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/",$email))
158:             if ($email['to'] === false)
159:                 $errors[] = 'email';
160:             if (empty($body))
161:                 $errors[] = 'body';
162: 
163:             if (empty($errors))
164:                 $status = $this->sendUserEmail($email, $subject, $body);
165: 
166:             if (array_search('200', $status)) {
167:                 $this->redirect(array('view', 'id' => $model->id));
168:                 return;
169:             }
170:             if ($email['to'] === false)
171:                 $email = $_POST['email'];
172:             else
173:                 $email = $this->mailingListToString($email['to']);
174:         }
175:         $this->render('shareAccount', array(
176:             'model' => $model,
177:             'body' => $body,
178:             'email' => $email,
179:             'status' => $status,
180:             'errors' => $errors
181:         ));
182:     }
183: 
184:     public function actionCreate() {
185:         $model = new Accounts;
186:         $users = User::getNames();
187:         unset($users['admin']);
188:         unset($users['']);
189:         foreach (Groups::model()->findAll() as $group)
190:             $users[$group->id] = $group->name;
191: 
192: 
193:         if (isset($_POST['Accounts'])) {
194: 
195:             $model->setX2Fields($_POST['Accounts']);
196: 
197:             if (isset($_POST['x2ajax'])) {
198:                 $ajaxErrors = $this->quickCreate($model);
199:             } else {
200:                 if ($model->validate () && $model->checkForDuplicates()) {
201:                     Yii::app()->user->setState('json_attributes', json_encode($model->attributes));
202:                     $this->redirect($this->createUrl('/site/duplicateCheck', array(
203:                                 'moduleName' => 'accounts',
204:                                 'modelName' => 'Accounts',
205:                                 'id' => null,
206:                                 'ref' => 'create',
207:                     )));
208:                 } else {
209:                     if ($model->save()) {
210:                         $this->redirect(array('view', 'id' => $model->id));
211:                     }
212:                 }
213:             }
214:         }
215: 
216:         if (isset($_POST['x2ajax'])) {
217:             $this->renderInlineCreateForm($model, isset($ajaxErrors) ? $ajaxErrors : false);
218:         } else {
219:             $this->render('create', array(
220:                 'model' => $model,
221:                 'users' => $users,
222:             ));
223:         }
224:     }
225: 
226:     // this nonsense is now done in Accounts::beforeValidate()
227:     /*         public function update($model, $oldAttributes,$api){
228:       // process currency into an INT
229:       $model->annualRevenue = Formatter::parseCurrency($model->annualRevenue,false);
230: 
231:       if($api==0)
232:       parent::update($model,$oldAttributes,$api);
233:       else
234:       return parent::update($model,$oldAttributes,$api);
235:       } */
236: 
237:     /**
238:      * Updates a particular model.
239:      * If update is successful, the browser will be redirected to the 'view' page.
240:      * @param integer $id the ID of the model to be updated
241:      */
242:     public function actionUpdate($id) {
243:         $model = $this->loadModel($id);
244:         if (isset($_POST['Accounts'])) {
245:             $model->setX2Fields($_POST['Accounts']);
246:             if ($model->save())
247:                 $this->redirect(array('view', 'id' => $model->id));
248:         }
249: 
250:         $this->render('update', array(
251:             'model' => $model,
252:         ));
253:     }
254: 
255:     /*
256:       public function actionSaveChanges($id) {
257:       $account=$this->loadModel($id);
258:       if(isset($_POST['Accounts'])) {
259:       $temp=$account->attributes;
260:       foreach($account->attributes as $field=>$value){
261:       if(isset($_POST['Accounts'][$field])){
262:       $account->$field=$_POST['Accounts'][$field];
263:       }
264:       }
265: 
266:       // process currency into an INT
267:       $account->annualRevenue = Formatter::parseCurrency($account->annualRevenue,false);
268:       $changes=$this->calculateChanges($temp,$account->attributes, $account);
269:       $account=$this->updateChangelog($account,$changes);
270:       $account->update();
271:       $this->redirect(array('view','id'=>$account->id));
272:       }
273:       }
274:      */
275: 
276:     public function actionAddUser($id) {
277:         $users = User::getNames();
278:         unset($users['admin']);
279:         unset($users['']);
280:         foreach (Groups::model()->findAll() as $group) {
281:             $users[$group->id] = $group->name;
282:         }
283:         //$contacts = Contacts::getAllNames(); // very inefficient with large table
284:         $model = $this->loadModel($id);
285:         $users = Accounts::editUserArray($users, $model);
286: 
287:         // Uncomment the following line if AJAX validation is needed
288:         // $this->performAjaxValidation($model);
289: 
290:         if (isset($_POST['Accounts'])) {
291: 
292:             $temp = $model->assignedTo;
293:             $tempArr = $model->attributes;
294:             $model->attributes = $_POST['Accounts'];
295:             $arr = $_POST['Accounts']['assignedTo'];
296:             $model->assignedTo = Fields::parseUsers($arr);
297:             if ($temp != "")
298:                 $temp.=", " . $model->assignedTo;
299:             else
300:                 $temp = $model->assignedTo;
301:             $model->assignedTo = $temp;
302:             // $changes=$this->calculateChanges($tempArr,$model->attributes);
303:             // $model=$this->updateChangelog($model,$changes);
304:             if ($model->save())
305:                 $this->redirect(array('view', 'id' => $model->id));
306:         }
307: 
308:         $this->render('addUser', array(
309:             'model' => $model,
310:             'users' => $users,
311:             //'contacts' => $contacts,
312:             'action' => 'Add'
313:         ));
314:     }
315: 
316:     public function actionRemoveUser($id) {
317: 
318:         $model = $this->loadModel($id);
319: 
320:         $pieces = explode(', ', $model->assignedTo);
321:         $pieces = Opportunity::editUsersInverse($pieces);
322: 
323:         // Uncomment the following line if AJAX validation is needed
324:         // $this->performAjaxValidation($model);
325: 
326:         if (isset($_POST['Accounts'])) {
327:             $temp = $model->attributes;
328:             $model->attributes = $_POST['Accounts'];
329:             $arr = $_POST['Accounts']['assignedTo'];
330: 
331:             foreach ($arr as $id => $user) {
332:                 unset($pieces[$user]);
333:             }
334: 
335:             $temp = Fields::parseUsersTwo($pieces);
336: 
337:             $model->assignedTo = $temp;
338:             // $changes=$this->calculateChanges($temp,$model->attributes);
339:             // $model=$this->updateChangelog($model,$changes);
340:             if ($model->save())
341:                 $this->redirect(array('view', 'id' => $model->id));
342:         }
343: 
344:         $this->render('addUser', array(
345:             'model' => $model,
346:             'users' => $pieces,
347:             'action' => 'Remove'
348:         ));
349:     }
350: 
351:     public function delete($id) {
352: 
353:         $model = $this->loadModel($id);
354:         $dataProvider = new CActiveDataProvider('Actions', array(
355:             'criteria' => array(
356:                 'condition' => 'associationId=' . $id . ' AND associationType=\'account\'',
357:         )));
358: 
359:         $actions = $dataProvider->getData();
360:         foreach ($actions as $action) {
361:             $action->delete();
362:         }
363:         $this->cleanUpTags($model);
364:         $model->delete();
365:     }
366: 
367:     /**
368:      * Deletes a particular model.
369:      * If deletion is successful, the browser will be redirected to the 'admin' page.
370:      * @param integer $id the ID of the model to be deleted
371:      */
372:     public function actionDelete($id) {
373:         if (Yii::app()->request->isPostRequest) {
374:             $model = $this->loadModel($id);
375:             Actions::model()->deleteAll('associationId=' . $id . ' AND associationType=\'account\'');
376:             $this->cleanUpTags($model);
377:             $model->delete();
378:         } else
379:             throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
380:         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
381:         if (!isset($_GET['ajax']))
382:             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
383:     }
384: 
385:     /**
386:      * Lists all models.
387:      */
388:     public function actionIndex() {
389: 
390:         $model = new Accounts('search');
391:         $this->render('index', array('model' => $model));
392:     }
393: 
394:     public function actionQtip($id) {
395:         $model = $this->loadModel($id);
396:         $this->renderPartial('qtip', array('model' => $model));
397:     }
398: 
399:     /**
400:      * Create a menu for Accounts
401:      * @param array Menu options to remove
402:      * @param X2Model Model object passed to the view
403:      * @param array Additional menu parameters
404:      */
405:     public function insertMenu($selectOptions = array(), $model = null, $menuParams = null) {
406:         $Accounts = Modules::displayName();
407:         $Account = Modules::displayName(false);
408:         $modelId = isset($model) ? $model->id : 0;
409:         $modelName = isset($model) ? $model->name : "";
410: 
411:         /**
412:          * To show all options:
413:          * $menuOptions = array(
414:          *     'all', 'create', 'report', 'import', 'export', 'view', 'edit', 'share',
415:          *     'delete', 'email', 'attach', 'quotes', 'quick', 'print',
416:          * );
417:          */
418: 
419:         $menuItems = array(
420:             array(
421:                 'name'=>'all',
422:                 'label'=>Yii::t('accounts','All {module}', array('{module}'=>$Accounts)),
423:                 'url' => array('index'),
424:             ),
425:             array(
426:                 'name'=>'create',
427:                 'label'=>Yii::t('accounts','Create {module}', array('{module}'=>$Account)),
428:                 'url'=>array('create')
429:             ),
430:             array(
431:                 'name'=>'report',
432:                 'label'=>Yii::t('accounts','{module} Report', array('{module}'=>$Accounts)),
433:                 'url'=>array('accountsReport')
434:             ),
435:             array(
436:                 'name'=>'import',
437:                 'label'=>Yii::t('accounts',"Import {module}", array('{module}'=>$Accounts)),
438:                 'url'=>array('admin/importModels', 'model'=>'Accounts')
439:             ),
440:             array(
441:                 'name'=>'export',
442:                 'label'=>Yii::t('accounts','Export {module}', array('{module}'=>$Accounts)),
443:                 'url'=>array('admin/exportModels', 'model'=>'Accounts')
444:             ),
445:             RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
446:             array(
447:                 'name'=>'edit',
448:                 'label'=>Yii::t('accounts','Edit {module}', array('{module}'=>$Account)),
449:                 'url'=>array('update', 'id'=>$modelId)
450:             ),
451:             array(
452:                 'name'=>'share',
453:                 'label'=>Yii::t('accounts','Share {module}', array('{module}'=>$Account)),
454:                 'url'=>array('shareAccount','id'=>$modelId)
455:             ),
456:             array(
457:                 'name'=>'delete',
458:                 'label'=>Yii::t('accounts','Delete {module}', array('{module}'=>$Account)),
459:                 'url'=>'#',
460:                 'linkOptions'=>array(
461:                     'submit'=>array('delete','id'=>$modelId),
462:                     'confirm'=>'Are you sure you want to delete this item?'
463:                 )
464:             ),
465:             array(
466:                 'name'=>'email',
467:                 'label' => Yii::t('app', 'Send Email'),
468:                 'url' => '#',
469:                 'linkOptions' => array('onclick' => 'toggleEmailForm(); return false;')
470:             ),
471:             ModelFileUploader::menuLink(),
472:             array(
473:                 'name'=>'quotes',
474:                 'label' => Yii::t('quotes', 'Quotes/Invoices'),
475:                 'url' => 'javascript:void(0)',
476:                 'linkOptions' => array(
477:                     'onclick' => 'x2.inlineQuotes.toggle(); return false;')
478:             ),
479:             array(
480:                 'name'=>'quick',
481:                 'label'=>Yii::t('app', 'Quick Create'),
482:                 'url'=>array('/site/createRecords', 'ret'=>'accounts'),
483:                 'linkOptions'=>array(
484:                     'id'=>'x2-create-multiple-records-button',
485:                     'class'=>'x2-hint',
486:                     'title'=>Yii::t('app', 'Create a {contact}, {account}, and {opportunity}.', array(
487:                         '{account}' => $Account,
488:                         '{contact}' => Modules::displayName(false, "Contacts"),
489:                         '{opportunity}' => Modules::displayName(false, "Opportunities"),
490:                     )))
491:             ),
492:             array(
493:                 'name'=>'print',
494:                 'label' => Yii::t('app', 'Print Record'),
495:                 'url' => '#',
496:                 'linkOptions' => array (
497:                     'onClick'=>"window.open('".
498:                         Yii::app()->createUrl('/site/printRecord', array (
499:                             'modelClass' => 'Accounts',
500:                             'id' => $modelId,
501:                             'pageTitle' => Yii::t('app', 'Account').': '.$modelName
502:                     ))."');"
503:                 ),
504:             ),
505:             RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
506:         );
507: 
508:         $this->prepareMenu($menuItems, $selectOptions);
509:         $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
510:     }
511: }
512: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0