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: 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',
51: 'actions'=>array('index','view','create','update','search','addUser','removeUser',
52: 'saveChanges','delete','shareOpportunity','inlineEmail'),
53: 'users'=>array('@'),
54: ),
55: array('allow',
56: 'actions'=>array('admin','testScalability'),
57: 'users'=>array('admin'),
58: ),
59: array('deny',
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: 93: 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:
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:
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: 162: 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:
174:
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: 201: 202: 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:
215: if($model->save())
216: $this->redirect(array('view','id'=>$model->id));
217: }
218: 219:
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:
240:
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:
256:
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:
276:
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:
292:
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: 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:
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: 360: 361: 362: 363:
364: public function ($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: 371: 372: 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: