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

  • ProductsController
  • 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.products.controllers 
 39:  */
 40: class ProductsController extends x2base {
 41:     public $modelClass = 'Product';
 42: 
 43:     public function accessRules() {
 44:         return array(
 45:             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 46:                 'actions'=>array('index', 'view', 'search','getItems'),
 47:                 'users'=>array('@'),
 48:             ),
 49:             array('allow', // allow admin user to perform 'admin' and 'delete' actions
 50:                 'actions'=>array('admin','testScalability', 'create', 'update', 'delete'),
 51:                 'users'=>array('admin'),
 52:             ),
 53:             array('deny',  // deny all users
 54:                 'users'=>array('*'),
 55:             ),
 56:         );
 57:     }
 58: 
 59:     public function behaviors(){
 60:         return array_merge(parent::behaviors(), array(
 61:             'X2MobileControllerBehavior' => array(
 62:                 'class' => 
 63:                     'application.modules.mobile.components.behaviors.X2MobileControllerBehavior'
 64:             ),
 65:             'QuickCreateRelationshipBehavior' => array(
 66:                 'class' => 'QuickCreateRelationshipBehavior',
 67:             ),
 68:         ));
 69:     }
 70: 
 71:     public function actionGetItems($term){
 72:         X2LinkableBehavior::getItems ($term);
 73:     }
 74:     /**
 75:      * Displays a particular model.
 76:      * @param integer $id the ID of the model to be displayed
 77:      */
 78:     public function actionView($id) {
 79:         $model = $this->loadModel($id);
 80:         if (!$this->checkPermissions($model, 'view')) $this->denied ();
 81: 
 82:         // add product to user's recent item list
 83:         User::addRecentItem('r', $id, Yii::app()->user->getId()); 
 84: 
 85:         parent::view($model);
 86:     }
 87: 
 88:     /**
 89:      * Creates a new model.
 90:      * If creation is successful, the browser will be redirected to the 'view' page.
 91:      */
 92:     public function actionCreate() {
 93:         $model=new Product;
 94:         $users=User::getNames();
 95:         if(isset($_POST['Product'])) {
 96:             $model->setX2Fields($_POST['Product']);
 97:             // $model->price = Formatter::parseCurrency($model->price,false);
 98:             $model->createDate=time();
 99: 
100:             if(isset($_POST['x2ajax'])){
101:                 $ajaxErrors = $this->quickCreate ($model);
102:             }else{
103:                 if($model->save()) {
104:                     $this->redirect(array('view', 'id' => $model->id));
105:                 }
106:             }
107:         }
108: 
109:         if(isset($_POST['x2ajax'])){
110:             $this->renderInlineCreateForm ($model, isset ($ajaxErrors) ? $ajaxErrors : false);
111:         } else {
112:             $this->render('create',array(
113:                 'model'=>$model,
114:                 'users'=>$users,
115:             ));
116:         }
117:     }
118: 
119:     /**
120:      * Updates a particular model.
121:      * If update is successful, the browser will be redirected to the 'view' page.
122:      * @param integer $id the ID of the model to be updated
123:      */
124:     public function actionUpdate($id) {
125:         $model = $this->loadModel($id);
126:         $users=User::getNames(); 
127:         $fields=Fields::model()->findAllByAttributes(array('modelName'=>'Product'));
128:         foreach($fields as $field){
129:             if($field->type=='link'){
130:                 $fieldName=$field->fieldName;
131:                 $type=ucfirst($field->linkType);
132:                 if(is_numeric($model->$fieldName) && $model->$fieldName!=0){
133:                     eval("\$lookupModel=$type::model()->findByPk(".$model->$fieldName.");");
134:                     if(isset($lookupModel))
135:                         $model->$fieldName=$lookupModel->name;
136:                 }
137:             }
138:         }
139:         if(isset($_POST['Product'])) {
140:             $temp=$model->attributes;
141:             $model->setX2Fields($_POST['Product']);
142:             
143:             // generate history
144:             $action = new Actions;
145:             $action->associationType = 'product';
146:             $action->associationId = $model->id;
147:             $action->associationName = $model->name;
148:             $action->assignedTo = Yii::app()->user->getName();
149:             $action->completedBy=Yii::app()->user->getName();
150:             $action->dueDate = time();
151:             $action->completeDate = time();
152:             $action->visibility = 1;
153:             $action->complete='Yes';
154:         
155:             $action->actionDescription = "Update: {$model->name}
156:             Type: {$model->type}
157:             Price: {$model->price}
158:             Currency: {$model->currency}
159:             Inventory: {$model->inventory}";
160:             $action->save();         
161:             parent::update($model,$temp,'0');
162:         }
163: 
164:         $this->render('update',array(
165:             'model'=>$model,
166:             'users'=>$users,
167:         ));
168:     }
169:     /**
170:      * Deletes a particular model.
171:      * If deletion is successful, the browser will be redirected to the 'admin' page.
172:      * @param integer $id the ID of the model to be deleted
173:      */
174:     public function actionDelete($id) {
175:         if(Yii::app()->request->isPostRequest) {    // we only allow deletion via POST request
176:             $model = $this->loadModel($id);
177:             
178:             $model->clearTags();
179:             $model->delete();
180:             
181:             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
182:             if(!isset($_GET['ajax']))
183:                 $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
184:         } else {
185:             throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
186:         }
187:     }
188: 
189:     /**
190:      * Lists all models.
191:      */
192:     public function actionIndex() {
193:         $model=new Product('search');
194:         $this->render('index', array('model'=>$model));
195:     }
196: 
197:     /**
198:      * Performs the AJAX validation.
199:      * @param CModel the model to be validated
200:      */
201:     protected function performAjaxValidation($model) {
202:         if(isset($_POST['ajax']) && $_POST['ajax']==='product-form') {
203:             echo CActiveForm::validate($model);
204:             Yii::app()->end();
205:         }
206:     }
207: 
208:     /**
209:      * Retrieve items for combo box
210:      * @param string $prefix parameter to getItems2
211:      * @param int $page parameter to getItems2
212:      */
213:     public function actionGetItems2 () {
214:         $prefix = isset ($_POST['prefix']) ? $_POST['prefix'] : '';
215:         $page = isset ($_POST['page']) ? $_POST['page'] : 0;
216:         $pageSize = isset ($_POST['pageSize']) ? $_POST['pageSize'] : 20;
217: 
218:         $items = X2Model::model ($this->modelClass)->getItems2 (
219:             $prefix, $page, $pageSize, array ('id', 'description', 'price'), 'name');
220:         echo CJSON::encode ($items);
221:     }
222: 
223:     /**
224:      * Create a menu for Products
225:      * @param array Menu options to remove
226:      * @param X2Model Model object passed to the view
227:      * @param array Additional menu parameters
228:      */
229:     public function insertMenu($selectOptions = array(), $model = null, $menuParams = null) {
230:         $Products = Modules::displayName();
231:         $Product = Modules::displayName(false);
232:         $modelId = isset($model) ? $model->id : 0;
233: 
234:         /**
235:          * To show all options:
236:          * $menuOptions = array(
237:          *     'index', 'create', 'view', 'edit', 'delete', 'print', 'import', 'export',
238:          * );
239:          */
240: 
241:         $menuItems = array(
242:             array(
243:                 'name'=>'index',
244:                 'label'=>Yii::t('products','{module} List', array(
245:                     '{module}'=>$Product,
246:                 )),
247:                 'url'=>array('index')
248:             ),
249:             array(
250:                 'name'=>'create',
251:                 'label'=>Yii::t('products','Create'),
252:                 'url'=>array('create')
253:             ),
254:             RecordViewLayoutManager::getViewActionMenuListItem ($modelId),
255:             array(
256:                 'name'=>'edit',
257:                 'label'=>Yii::t('products','Update'),
258:                 'url'=>array('update', 'id'=>$modelId)
259:             ),
260:             array(
261:                 'name'=>'delete',
262:                 'label'=>Yii::t('products','Delete'),
263:                 'url'=>'#',
264:                 'linkOptions'=>array(
265:                     'submit'=>array('delete','id'=>$modelId),
266:                     'confirm'=>Yii::t('app','Are you sure you want to delete this item?')
267:                 )
268:             ),
269:             array(
270:                 'name' => 'print',
271:                 'label' => Yii::t('app', 'Print Record'),
272:                 'url' => '#',
273:                 'linkOptions' => array (
274:                     'onClick'=>"window.open('".
275:                         Yii::app()->createUrl('/site/printRecord', array (
276:                             'modelClass' => 'Product',
277:                             'id' => $modelId,
278:                             'pageTitle' => Yii::t('app', '{module}', array(
279:                                 '{module}' => $Product,
280:                             )).': '.(isset($model) ? $model->name : "")
281:                     ))."');"
282:                 ),
283:             ),
284:             array(
285:                 'name'=>'import',
286:                 'label'=>Yii::t('products', 'Import {module}', array(
287:                     '{module}' => $Products,
288:                 )),
289:                 'url'=>array('admin/importModels', 'model'=>'Product'),
290:             ),
291:             array(
292:                 'name'=>'export',
293:                 'label'=>Yii::t('products', 'Export {module}',  array(
294:                     '{module}' => $Products,
295:                 )),
296:                  'url'=>array('admin/exportModels', 'model'=>'Product'),
297:             ),
298:             RecordViewLayoutManager::getEditLayoutActionMenuListItem (),
299:         );
300: 
301:         $this->prepareMenu($menuItems, $selectOptions);
302:         $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
303:     }
304: 
305: 
306: }
307: 
X2CRM Documentation API documentation generated by ApiGen 2.8.0