Exemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function postStore($data = null)
 {
     $input = Request::segment(1) == 'phone_number' ? Input::only('description', 'sip_password', 'user_id') : $data;
     $input['extension'] = Cookie::get('rndext') ? Cookie::get('rndext') : $this->generate_extension();
     $path = Request::segment(2) == 'manage' ? "phone_number/manage/" . Request::segment(3) : "phone_number";
     $rules = array('extension' => 'unique:phone_numbers,extension,NULL,id,deleted_at,NULL', 'sip_password' => 'required|min:6|alpha_num');
     if (Request::segment(2) == 'manage') {
         $rules['user_id'] = 'required';
     }
     $v = Validator::make($input, $rules);
     if ($v->fails()) {
         return Output::push(array('path' => $path . '/add', 'errors' => $v, 'input' => TRUE));
     }
     $phone_number = new PhoneNumber(['user_id' => $input['user_id'] ? $input['user_id'] : Auth::user()->id, 'extension' => $input['extension'], 'sip_password' => $input['sip_password'], 'description' => $input['description']]);
     $phone_number->save();
     Event::fire('logger', array(array('phone_number_add', array('id' => $phone_number->id, 'extension' => $input['extension']), 2)));
     if ($phone_number->id) {
         $cookie = Cookie::forget('rndext');
         $user = $input['user_id'] ? User::find($input['user_id']) : Auth::user();
         $data = array('phone_number_e164' => '+' . Config::get('settings.global_prefix') . '-' . $user->domain->prefix . '-' . $input['extension'], 'local_phone_number' => $input['extension'], 'domain_sip_server' => $user->domain->sip_server, 'domain_control_panel' => $user->domain->domain, 'sip_password' => $input['sip_password']);
         $mail_to = $input['user_id'] ? User::find($input['user_id'])->email : Auth::user()->email;
         Mail::send('emails.phone_number', $data, function ($message) use($mail_to) {
             $message->from(Config::get('mail.from.address'), Config::get('mail.from.name'))->to($mail_to)->subject(_('New phone number'));
         });
         return Redirect::to($path)->with('success', _('You have added Phone Number successfully'))->withCookie($cookie);
     } else {
         return Output::push(array('path' => $path . '/add', 'messages' => array('fail' => _('Fail to add Phone Number')), 'input' => TRUE));
     }
 }
Exemplo n.º 2
0
 /**
  * Runs when a model is saved.
  * Scans attributes for phone numbers and index them in <tt>x2_phone_numbers</tt>.
  * Updates <tt>x2_relationships</tt> table based on link type fields.
  * Fires onAfterSave event.
  */
 public function afterSave()
 {
     if ($this->_runAfterCreate) {
         $this->afterCreate();
     } else {
         $this->afterUpdate();
     }
     $phoneFields = array();
     $linkFields = array();
     // look through fields for phone numbers and relationships
     foreach (self::$_fields[$this->tableName()] as &$_field) {
         if ($_field->type === 'phone') {
             $phoneFields[$_field->fieldName] = $this->getAttribute($_field->fieldName);
         } elseif ($_field->type === 'link') {
             $nameAndId = Fields::nameAndId($this->getAttribute($_field->fieldName));
             $linkFields[$_field->fieldName] = array('id' => $nameAndId[1], 'type' => $_field->linkType);
         }
     }
     // deal with phone numbers
     if (count($phoneFields)) {
         // clear out old phone numbers
         X2Model::model('PhoneNumber')->deleteAllByAttributes(array('modelId' => $this->id, 'modelType' => get_class($this)));
     }
     // create new entries in x2_phone_numbers
     foreach ($phoneFields as $field => &$number) {
         if (!empty($number)) {
             $num = new PhoneNumber();
             // eliminate everything other than digits
             $num->number = preg_replace('/\\D/', '', $number);
             $num->modelId = $this->id;
             $num->modelType = get_class($this);
             $num->fieldName = $field;
             $num->save();
         }
     }
     parent::afterSave();
     // raise onAfterSave event for behaviors, such as X2ChangeLogBehavior
 }
Exemplo n.º 3
0
 /**
  * Runs when a model is saved.
  * Scans attributes for phone numbers and index them in <tt>x2_phone_numbers</tt>.
  * Updates <tt>x2_relationships</tt> table based on link type fields.
  * Fires onAfterSave event.
  */
 public function afterSave()
 {
     if ($this->_runAfterCreate) {
         $this->afterCreate();
     } else {
         $this->afterUpdate();
     }
     $phoneFields = array();
     $linkFields = array();
     // look through fields for phone numbers and relationships
     foreach (self::$_fields[$this->tableName()] as &$_field) {
         if ($_field->type === 'phone') {
             $phoneFields[$_field->fieldName] = $this->getAttribute($_field->fieldName);
         } elseif ($_field->type === 'link') {
             $nameAndId = Fields::nameAndId($this->getAttribute($_field->fieldName));
             $linkFields[$_field->fieldName] = array('id' => $nameAndId[1], 'type' => $_field->linkType);
         }
     }
     // deal with phone numbers
     if (count($phoneFields)) {
         X2Model::model('PhoneNumber')->deleteAllByAttributes(array('modelId' => $this->id, 'modelType' => get_class($this)));
     }
     // clear out old phone numbers
     foreach ($phoneFields as $field => &$number) {
         // create new entries in x2_phone_numbers
         if (!empty($number)) {
             $num = new PhoneNumber();
             $num->number = preg_replace('/\\D/', '', $number);
             // eliminate everything other than digits
             $num->modelId = $this->id;
             $num->modelType = get_class($this);
             $num->fieldName = $field;
             $num->save();
         }
     }
     /////////////// deal with relationships ///////////////
     $oldAttributes = $this->getOldAttributes();
     $relationSql = '(firstType=:type1 AND firstId=:id1 AND secondType=:type2 AND secondId=:id2) OR
          (firstType=:type2 AND firstId=:id2 AND secondType=:type1 AND secondId=:id1)';
     foreach ($linkFields as $fieldName => &$relation) {
         list($oldLinkName, $oldLinkId) = Fields::nameAndId(isset($oldAttributes[$fieldName]) ? $oldAttributes[$fieldName] : '');
         if ($relation['id'] == $oldLinkId) {
             // skip field if it hasn't changed
             continue;
         }
         // forget old relationship (wouldn't it be nice...)
         if (!empty($oldLinkId)) {
             CActiveRecord::model('Relationships')->deleteAll($relationSql, array(':type1' => get_class($this), ':id1' => $this->id, ':type2' => $relation['type'], ':id2' => $oldLinkId));
         }
         // save new relationship
         if (!empty($relation['id']) && ctype_digit((string) $relation['id'])) {
             if (!CActiveRecord::model('Relationships')->exists($relationSql, array(':type1' => get_class($this), ':id1' => $this->id, ':type2' => $relation['type'], ':id2' => $relation['id']))) {
                 $rel = new Relationships();
                 $rel->firstType = get_class($this);
                 $rel->secondType = $relation['type'];
                 $rel->firstId = $this->id;
                 $rel->secondId = $relation['id'];
                 $rel->save();
             }
         }
     }
     parent::afterSave();
     // raise onAfterSave event for behaviors, such as X2ChangeLogBehavior
 }