Example #1
0
 public function validateProperties($attribute)
 {
     //only validate the field "article"
     if ($attribute !== 'properties') {
         return true;
     }
     $properties = $this->{$attribute};
     if (!is_array($properties)) {
         $this->addError($attribute, 'properties should be an array');
     }
     $requiredFields = ['id', 'name'];
     $memberProperty = [];
     //formate result
     //validate each field in properties
     foreach ($properties as $property) {
         //validate the required fields in properties
         foreach ($requiredFields as $field) {
             if (empty($property[$field])) {
                 $this->addError($attribute, 'properties.' . $field . ' is required');
             }
         }
         // formate property id string to mongoId
         if (!empty($property['id'])) {
             $property['id'] = new \MongoId($property['id']);
             if (empty(MemberProperty::findByPk($property['id']))) {
                 $this->addError($attribute, 'properties.id is not exist');
             }
             $memberProperty[] = $property;
         }
         if ($property['name'] == self::DEFAULT_PROPERTIES_BIRTHDAY) {
             //get month like 1, 2, 3 ... 12
             $month = date('n', TimeUtil::ms2sTime($property['value']));
             //get day like 1, 2, 3, ... 31
             $day = date('j', TimeUtil::ms2sTime($property['value']));
             $this->birth = $month * 100 + $day;
         }
     }
     $this->{$attribute} = $memberProperty;
 }
 /**
  * Update the member property
  *
  * <b>Request Type:</b> PUT<br/>
  * <b>Request Endpoint:</b> http://{{server-domain}}/api/common/member-property/{{id}}
  * <b>Content-type:</b> application/json<br/>
  * <b>Summary</b> This api is for update member property<br/>
  *
  * <b>Request Parameters</b><br/>
  * <pre>
  *     {
  *         "order": 7,
  *         "name": "name123",
  *         "defaultValue": "Devin Jin 1",
  *         "isRequired": true,
  *         "isUnique": true,
  *         "isVisible": true
  *       }
  * </pre>
  */
 public function actionUpdate($id)
 {
     $propertyId = new \MongoId($id);
     $property = MemberProperty::findByPk($propertyId);
     $oldOptions = $property->options;
     $property->load($this->getParams(), '');
     if ($property->save()) {
         //update all the members of the account
         $modifier = ['$set' => ['properties.$.name' => $property->name]];
         if (($property->type == MemberProperty::TYPE_CHECKBOX || $property->type == MemberProperty::TYPE_RADIO) && $property->options != $oldOptions) {
             $modifier['$set']['properties.$.value'] = $property->defaultValue;
         }
         if (!$property->isVisible) {
             $scoreRuleModifier = ['$pull' => ['properties' => $property->_id]];
             $scoreRuleCondition = ['accountId' => $property->accountId, 'name' => ScoreRule::NAME_PERFECT_INFO, 'properties' => $property->_id];
             ScoreRule::updateAll($scoreRuleModifier, $scoreRuleCondition);
         }
         Member::updateAll($modifier, ['properties.id' => $propertyId]);
         return $property;
     } else {
         if ($property->hasErrors()) {
             throw new BadRequestHttpException(Json::encode($property->errors));
         }
     }
     throw new ServerErrorHttpException('Failed to save the object for unknown reason.');
 }