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

  • OpportunitiesController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /*****************************************************************************************
  3:  * X2Engine Open Source Edition is a customer relationship management program developed by
  4:  * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5:  * 
  6:  * This program is free software; you can redistribute it and/or modify it under
  7:  * the terms of the GNU Affero General Public License version 3 as published by the
  8:  * Free Software Foundation with the addition of the following permission added
  9:  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 10:  * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
 11:  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 12:  * 
 13:  * This program is distributed in the hope that it will be useful, but WITHOUT
 14:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 15:  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 16:  * details.
 17:  * 
 18:  * You should have received a copy of the GNU Affero General Public License along with
 19:  * this program; if not, see http://www.gnu.org/licenses or write to the Free
 20:  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 21:  * 02110-1301 USA.
 22:  * 
 23:  * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
 24:  * California 95067, USA. or at email address contact@x2engine.com.
 25:  * 
 26:  * The interactive user interfaces in modified source and object code versions
 27:  * of this program must display Appropriate Legal Notices, as required under
 28:  * Section 5 of the GNU Affero General Public License version 3.
 29:  * 
 30:  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 31:  * these Appropriate Legal Notices must retain the display of the "Powered by
 32:  * X2Engine" logo. If the display of the logo is not reasonably feasible for
 33:  * technical reasons, the Appropriate Legal Notices must display the words
 34:  * "Powered by X2Engine".
 35:  *****************************************************************************************/
 36: 
 37: /**
 38:  * @package application.modules.opportunities.controllers
 39:  */
 40: class OpportunitiesController extends x2base {
 41: 
 42:     public $modelClass = 'Opportunity';
 43: 
 44:     public function accessRules() {
 45:         return array(
 46:             array('allow',
 47:                 'actions'=>array('getItems'),
 48:                 'users'=>array('*'),
 49:             ),
 50:             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 51:                 'actions'=>array('index','view','create','update','search','addUser','removeUser',
 52:                                     'saveChanges','delete','shareOpportunity','inlineEmail'),
 53:                 'users'=>array('@'),
 54:             ),
 55:             array('allow', // allow admin user to perform 'admin' and 'delete' actions
 56:                 'actions'=>array('admin','testScalability'),
 57:                 'users'=>array('admin'),
 58:             ),
 59:             array('deny',  // deny all users
 60:                 'users'=>array('*'),
 61:             ),
 62:         );
 63:     }
 64: 
 65:     public function behaviors(){
 66:         return array_merge(parent::behaviors(), array(
 67:             'X2MobileControllerBehavior' => array(
 68:                 'class' => 
 69:                     'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
 70:             ),
 71:             'QuickCreateRelationshipBehavior' => array(
 72:                 'class' => 'QuickCreateRelationshipBehavior',
 73:                 'attributesOfNewRecordToUpdate' => array (
 74:                     'Contacts' => array (
 75:                         'accountName' => 'company',
 76:                     ),
 77:                 )
 78:             ),
 79:         ));
 80:     }
 81: 
 82:     public function actions() {
 83:         return array_merge(parent::actions(), array(
 84:         ));
 85:     }
 86: 
 87:     public function actionGetItems($term){
 88:         X2LinkableBehavior::getItems ($term);
 89:     }
 90: 
 91:     /**
 92:      * Displays a particular model.
 93:      * @param integer $id the ID of the model to be displayed
 94:      */
 95:     public function actionView($id) {
 96:         $type = 'opportunities';
 97:         $model = $this->loadModel($id);
 98:         $model->associatedContacts = Contacts::getContactLinks($model->associatedContacts);
 99:         if($this->checkPermissions($model,'view')){
100: 
101:             // add opportunity to user's recent item list
102:             User::addRecentItem('o', $id, Yii::app()->user->getId()); 
103: 
104:             parent::view($model, $type);
105:         }else{
106:             $this->redirect('index');
107:         }
108:     }
109: 
110:     public function actionShareOpportunity($id){
111: 
112:         $model=$this->loadModel($id);
113:         $body="\n\n\n\n".Yii::t('opportunities','Opportunity Record Details')." <br />
114: <br />".Yii::t('opportunities','Name').": $model->name
115: <br />".Yii::t('opportunities','Description').": $model->description
116: <br />".Yii::t('opportunities','Quote Amount').": $model->quoteAmount
117: <br />".Yii::t('opportunities','Opportunities Stage').": $model->salesStage
118: <br />".Yii::t('opportunities','Lead Source').": $model->leadSource
119: <br />".Yii::t('opportunities','Probability').": $model->probability
120: <br />".Yii::t('app','Link').": ".CHtml::link($model->name,'http://'.Yii::app()->request->getServerName().$this->createUrl('/opportunities/'.$model->id));
121: 
122:         $body = trim($body);
123: 
124:         $errors = array();
125:         $status = array();
126:         $email = array();
127:         if(isset($_POST['email'], $_POST['body'])){
128: 
129:             $subject = Yii::t('opportunities','Opportunity Record Details');
130:             $email['to'] = $this->parseEmailTo($this->decodeQuotes($_POST['email']));
131:             $body = $_POST['body'];
132:             // if(empty($email) || !preg_match("/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/",$email))
133:             if($email['to'] === false)
134:                 $errors[] = 'email';
135:             if(empty($body))
136:                 $errors[] = 'body';
137: 
138:             if(empty($errors))
139:                 $status = $this->sendUserEmail($email,$subject,$body);
140: 
141:             if(array_search('200',$status)) {
142:                 $this->redirect(array('view','id'=>$model->id));
143:                 return;
144:             }
145:             if($email['to'] === false)
146:                 $email = $_POST['email'];
147:             else
148:                 $email = $this->mailingListToString($email['to']);
149:         }
150:         $this->render('shareOpportunity',array(
151:             'model'=>$model,
152:             'body'=>$body,
153:             'currentWorkflow'=>$this->getCurrentWorkflow($model->id,'opportunities'),
154:             'email'=>$email,
155:             'status'=>$status,
156:             'errors'=>$errors
157:         ));
158:     }
159: 
160:     /**
161:      * Creates a new model.
162:      * If creation is successful, the browser will be redirected to the 'view' page.
163:      */
164:     public function actionCreate() {
165:         $model = new Opportunity;
166:         $users = User::getNames();
167:         foreach(Groups::model()->findAll() as $group){
168:             $users[$group->id]=$group->name;
169:         }
170:         unset($users['admin']);
171:         unset($users['']);
172: 
173:         // Uncomment the following line if AJAX validation is needed
174:         // $this->performAjaxValidation($model);
175: 
176:         if(isset($_POST['Opportunity'])) {
177:             $temp=$model->attributes;
178: 
179:             $model->setX2Fields($_POST['Opportunity']);
180: 
181:             if(isset($_POST['x2ajax'])) {
182:                 $ajaxErrors = $this->quickCreate ($model);
183:             } else {
184:                 if($model->save())
185:                     $this->redirect(array('view','id'=>$model->id));
186:             }
187:         }
188: 
189:         if(isset($_POST['x2ajax'])){
190:             $this->renderInlineForm ($model);
191:         } else {
192:             $this->render('create',array(
193:                 'model'=>$model,
194:                 'users'=>$users,
195:             ));
196:         }
197:     }
198: 
199:     /**
200:      * Updates a particular model.
201:      * If update is successful, the browser will be redirected to the 'view' page.
202:      * @param integer $id the ID of the model to be updated
203:      */
204:     public function actionUpdate($id) {
205:         $model=$this->loadModel($id);
206:         if(!empty($model->associatedContacts))
207:             $model->associatedContacts = explode(' ',$model->associatedContacts);
208: 
209:         if(isset($_POST['Opportunity'])) {
210:             $model->setX2Fields($_POST['Opportunity']);
211:             if(!empty($model->associatedContacts))
212:                 $model->associatedContacts=implode(', ',$model->associatedContacts);
213: 
214:             // $this->update($model,$temp);
215:             if($model->save())
216:                 $this->redirect(array('view','id'=>$model->id));
217:         }
218:         /* Set assignedTo back into an array only before re-rendering the input box with assignees 
219:            selected */
220:         $model->assignedTo = array_map(function($n){
221:             return trim($n,',');
222:         },explode(' ',$model->assignedTo));
223: 
224:         $this->render('update',array(
225:             'model'=>$model,
226:         ));
227:     }
228:   
229:     public function actionAddUser($id) {
230:         $users=User::getNames();
231:         unset($users['admin']);
232:         unset($users['']);
233:         foreach(Groups::model()->findAll() as $group){
234:             $users[$group->id]=$group->name;
235:         }
236:         $model=$this->loadModel($id);
237:         $users=Opportunity::editUserArray($users,$model);
238: 
239:         // Uncomment the following line if AJAX validation is needed
240:         // $this->performAjaxValidation($model);
241: 
242:         if(isset($_POST['Opportunity'])) {
243:             $temp=$model->assignedTo;
244:                         $tempArr=$model->attributes;
245:             $model->attributes=$_POST['Opportunity'];
246:             $arr=$_POST['Opportunity']['assignedTo'];
247: 
248: 
249:             $model->assignedTo=Opportunity::parseUsers($arr);
250:             if($temp!="")
251:                 $temp.=", ".$model->assignedTo;
252:             else
253:                 $temp=$model->assignedTo;
254:             $model->assignedTo=$temp;
255:             // $changes=$this->calculateChanges($tempArr,$model->attributes);
256:             // $model=$this->updateChangelog($model,$changes);
257:             if($model->save())
258:                 $this->redirect(array('view','id'=>$model->id));
259:         }
260: 
261:         $this->render('addUser',array(
262:             'model'=>$model,
263:             'users'=>$users,
264:             'action'=>'Add'
265:         ));
266:     }
267: 
268:     public function actionRemoveUser($id) {
269: 
270:         $model=$this->loadModel($id);
271: 
272:         $pieces=explode(', ',$model->assignedTo);
273:         $pieces=Opportunity::editUsersInverse($pieces);
274: 
275:         // Uncomment the following line if AJAX validation is needed
276:         // $this->performAjaxValidation($model);
277: 
278:         if(isset($_POST['Opportunity'])) {
279:                         $temp=$model->attributes;
280:             $model->attributes=$_POST['Opportunity'];
281:             $arr=$_POST['Opportunity']['assignedTo'];
282: 
283: 
284:             foreach($arr as $id=>$user){
285:                 unset($pieces[$user]);
286:             }
287: 
288:             $temp=Opportunity::parseUsersTwo($pieces);
289: 
290:             $model->assignedTo=$temp;
291:             // $changes=$this->calculateChanges($temp,$model->attributes, $model);
292:             // $model=$this->updateChangelog($model,$changes);
293:             if($model->save())
294:                 $this->redirect(array('view','id'=>$model->id));
295:         }
296: 
297:         $this->render('addUser',array(
298:             'model'=>$model,
299:             'users'=>$pieces,
300:             'action'=>'Remove'
301:         ));
302:     }
303: 
304:     /**
305:      * Lists all models.
306:      */
307:     public function actionIndex() {
308:         $model=new Opportunity('search');
309:         $this->render('index', array('model'=>$model));
310:     }
311: 
312:     public function delete($id) {
313:         $model = $this->loadModel($id);
314: 
315:         CActiveDataProvider::model('Actions')->deleteAllByAttributes(
316:             array('associationType'=>'opportunities','associationId'=>$id));
317: 
318:         $this->cleanUpTags($model);
319:         $model->delete();
320:     }
321: 
322:     public function actionDelete($id) {
323:         $model=$this->loadModel($id);
324: 
325:         if(Yii::app()->request->isPostRequest) {
326:             $event=new Events;
327:             $event->type='record_deleted';
328:             $event->associationType=$this->modelClass;
329:             $event->associationId=$model->id;
330:             $event->text=$model->name;
331:             $event->user=Yii::app()->user->getName();
332:             $event->save();
333:             Actions::model()->deleteAll('associationId='.$id.' AND associationType=\'opportunities\'');
334:             $this->cleanUpTags($model);
335:             $model->delete();
336:         } else
337:             throw new CHttpException(400,Yii::t('app','Invalid request. Please do not repeat this request again.'));
338:             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
339: 
340:         if(!isset($_GET['ajax']))
341:             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
342:     }
343: 
344:     public function actionGetTerms(){
345:         $sql = 'SELECT id, name as value FROM x2_accounts WHERE name LIKE :qterm ORDER BY name ASC';
346:         $command = Yii::app()->db->createCommand($sql);
347:         $qterm = $_GET['term'].'%';
348:         $command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
349:         $result = $command->queryAll();
350:         echo CJSON::encode($result); exit;
351:     }
352: 
353:     public function actionQtip($id){
354:         $model = $this->loadModel($id);
355:         $this->renderPartial('qtip', array('model' => $model));
356:     }
357: 
358:     /**
359:      * Create a menu for Opportunities
360:      * @param array Menu options to remove
361:      * @param X2Model Model object passed to the view
362:      * @param array Additional menu parameters
363:      */
364:     public function insertMenu($selectOptions = array(), $model = null, $menuParams = null) {
365:         $Opportunities = Modules::displayName();
366:         $Opportunity = Modules::displayName(false);
367:         $modelId = isset($model) ? $model->id : 0;
368: 
369:         /**
370:          * To show all options:
371:          * $menuOptions = array(
372:          *     'index', 'create', 'view', 'edit', 'share', 'delete', 'attach', 'import', 'export', 'quick',
373:          * );
374:          */
375: 
376:         $menuItems = array(
377:             array(
378:                 'name'=>'index',
379:                 'label'=>Yii::t('opportunities','{opportunities} List', array(
380:                     '{opportunities}'=>$Opportunities,
381:                 )),
382:                 'url'=>array('index')
383:             ),
384:             array(
385:                 'name'=>'create',
386:                 'label'=>Yii::t('opportunities','Create {opportunity}', array(
387:                     '{opportunity}'=>$Opportunity,
388:                 )),
389:                 'url'=>array('create')
390:             ),
391:             RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
392:             array(
393:                 'name'=>'edit',
394:                 'label'=>Yii::t('opportunities','Edit {opportunity}', array(
395:                     '{opportunity}'=>$Opportunity,
396:                 )),
397:                 'url'=>array('update', 'id'=>$modelId)
398:             ),
399:             array(
400:                 'name'=>'share',
401:                 'label'=>Yii::t('accounts','Share {opportunity}', array(
402:                     '{opportunity}'=>$Opportunity,
403:                 )),
404:                 'url'=>array('shareOpportunity','id'=>$modelId)
405:             ),
406:             array(
407:                 'name'=>'delete',
408:                 'label'=>Yii::t('opportunities','Delete'),
409:                 'url'=>'#',
410:                 'linkOptions'=>array(
411:                     'submit'=>array('delete','id'=>$modelId),
412:                     'confirm'=>'Are you sure you want to delete this item?')
413:             ),
414:             ModelFileUploader::menuLink(),
415:             array(
416:                 'name'=>'quotes',
417:                 'label' => Yii::t('quotes', 'Quotes/Invoices'), 'url' => 'javascript:void(0)',
418:                 'linkOptions' => array('onclick' => 'x2.inlineQuotes.toggle(); return false;')),
419:             array(
420:                 'name'=>'import',
421:                 'label'=>Yii::t('opportunities', 'Import {opportunities}', array(
422:                     '{opportunities}'=>$Opportunities,
423:                 )),
424:                 'url'=>array('admin/importModels', 'model'=>'Opportunity'),
425:             ),
426:             array(
427:                 'name'=>'export',
428:                 'label'=>Yii::t('opportunities', 'Export {opportunities}', array(
429:                     '{opportunities}'=>$Opportunities,
430:                 )),
431:                 'url'=>array('admin/exportModels', 'model'=>'Opportunity'),
432:             ),
433:             array(
434:                 'name'=>'quick',
435:                 'label'=>Yii::t('app', 'Quick Create'),
436:                 'url'=>array('/site/createRecords', 'ret'=>'opportunities'),
437:                 'linkOptions'=>array(
438:                     'id'=>'x2-create-multiple-records-button',
439:                     'class'=>'x2-hint',
440:                     'title'=>Yii::t('app', 'Create a {contact}, {account}, and {opportunity}.', array(
441:                         '{opportunity}'=>$Opportunity,
442:                         '{contact}'=>Modules::displayName(false, "Contacts"),
443:                         '{account}'=>Modules::displayName(false, "Accounts"),
444:                     )))
445:             ),
446:             array(
447:                 'name' => 'print',
448:                 'label' => Yii::t('app', 'Print Record'),
449:                 'url' => '#',
450:                 'linkOptions' => array (
451:                     'onClick'=>"window.open('".
452:                         Yii::app()->createUrl('/site/printRecord', array (
453:                             'modelClass' => 'Opportunity',
454:                             'id' => $modelId,
455:                             'pageTitle' => Yii::t('app', 'Opportunity').': '.(isset($model) ? 
456:                                 $model->name : "")
457:                         ))."');"
458:                 )
459:             ),
460:             RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
461:         );
462: 
463:         $this->prepareMenu($menuItems, $selectOptions);
464:         $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
465:     }
466: }
467: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0