예제 #1
0
 /**
  * Create a web lead form with a custom style
  *
  * Currently web forms have all options passed as GET parameters. Saved web forms
  * are saved to the table x2_web_forms. Saving, retrieving, and updating a web form
  * all happens in this function. Someday this should be updated to be it's own module.
  *
  * 
  * This get request is for weblead/service type only, marketing/weblist/view supplies 
  * the form that posts for weblist type 
  *  
  */
 public function run()
 {
     AuxLib::debugLogR($_POST);
     $modelClass = $this->controller->modelClass;
     if ($modelClass === 'Campaign') {
         $modelClass = 'Contacts';
     }
     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         // save a web form
         if (empty($_POST['name'])) {
             if ($modelClass === 'Contacts') {
                 echo json_encode(array('errors' => array('name' => Yii::t('marketing', 'Name cannot be blank.'))));
             } elseif ($modelClass === 'Services') {
                 echo json_encode(array('errors' => array('name' => Yii::t('marketing', 'Name cannot be blank.'))));
             }
             return;
         }
         if ($modelClass === 'Contacts') {
             $type = !empty($_POST['type']) ? $_POST['type'] : 'weblead';
         } elseif ($modelClass === 'Services') {
             $type = 'serviceCase';
         }
         $model = WebForm::model()->findByAttributes(array('name' => $_POST['name'], 'type' => $type));
         // check if we are updating an existing web form
         if (!isset($model)) {
             $model = new WebForm();
             $model->name = $_POST['name'];
             $model->type = $type;
             $model->modelName = $modelClass;
             $model->visibility = 1;
             $model->assignedTo = Yii::app()->user->getName();
             $model->createdBy = Yii::app()->user->getName();
             $model->createDate = time();
         }
         //grab web lead configuration and stash in 'params'
         $whitelist = array('fg', 'bgc', 'font', 'bs', 'bc', 'tags');
         $config = array_filter(array_intersect_key($_POST, array_flip($whitelist)));
         //restrict param values, alphanumeric, # for color vals, comma for tag list
         $config = preg_replace('/[^a-zA-Z0-9#,]/', '', $config);
         if (!empty($config)) {
             $model->params = $config;
         } else {
             $model->params = null;
         }
         if (isset($_POST['generateLead']) && isset($_POST['leadSource'])) {
             $model->leadSource = $_POST['leadSource'];
             $model->generateLead = 1;
         } else {
             $model->generateLead = 0;
         }
         if (isset($_POST['generateAccount'])) {
             $model->generateAccount = 1;
         } else {
             $model->generateAccount = 0;
         }
         $model->updatedBy = Yii::app()->user->getName();
         $model->lastUpdated = time();
         if ($model->save()) {
             echo json_encode($model->attributes);
         } else {
             echo json_encode(array('errors' => $model->getErrors()));
         }
     } else {
         if ($modelClass === 'Contacts') {
             $criteria = X2Model::model('Marketing')->getAccessCriteria();
             $condition = $criteria->condition;
             $forms = WebForm::model()->findAll('type="weblead" AND ' . $condition, $criteria->params);
             $this->controller->render('application.modules.marketing.views.marketing.webleadForm', array('forms' => $forms));
         } else {
             if ($modelClass === 'Services') {
                 $criteria = X2Model::model('Services')->getAccessCriteria();
                 $condition = $criteria->condition;
                 // get service web forms (other option is 'weblead' used by marketing module)
                 $forms = WebForm::model()->findAll('type="serviceCase" AND ' . $condition, $criteria->params);
                 $this->controller->render('application.modules.services.views.services.createWebFormView', array('forms' => $forms));
             }
         }
     }
 }