public function store(&$object, &$response = null)
 {
     $params = array('entity_type' => $object->entity_type, 'entity_ids' => $object->entity_ids);
     $xml = new EasyRedmineXMLElement('<' . $this->context_one . '/>');
     $xml->addAttribute('type', 'array');
     $object->ids = (array) $object->ids;
     foreach ($object->ids as $id) {
         $xml->addChild('id', $id);
     }
     $response = $this->_sendRequest('/' . $this->context . '.xml', 'post', $xml->asXML(), $params);
     //create
     if ($response->code != 200 and $response->code != 201) {
         if (isset($response->body->error)) {
             $this->setErrors((array) $response->body->error);
         }
         $this->_writeLog('Store() - insert for context "' . $this->context . '/' . $this->context_one . '" failed: ' . implode($this->getErrors()), JLog::ERROR);
         return false;
     }
     return true;
 }
Пример #2
0
 /**
  * Insert/Update record for actual context (I/U depends on object id property)
  *
  * @param object      $object   Object to store as Redmine entity
  * @param object|null $response Reference to rest api response object
  *
  * @returns boolean
  */
 public function store(&$object, &$response = null)
 {
     $this->errors = array();
     if (!$this->validateStore($object)) {
         $this->_writeLog('Store() - validation for context "' . $this->context . '/' . $this->context_one . '" failed: ' . implode($this->getErrors()), JLog::ERROR);
         return false;
     }
     $this->_beforeStore($object);
     $xml = new EasyRedmineXMLElement('<' . $this->context_one . '/>');
     $properties = get_object_vars($object);
     foreach ($properties as $property_name => $property_value) {
         switch ($property_name) {
             case 'custom_fields':
                 if (is_array($property_value) and !empty($property_value)) {
                     $xml_cf = $xml->addChild('custom_fields');
                     $xml_cf->addAttribute('type', 'array');
                     foreach ($property_value as $custom_field) {
                         if (isset($custom_field['id']) and (int) $custom_field['id'] and isset($custom_field['value'])) {
                             if (isset($custom_field['lookup']) and $custom_field['lookup']) {
                                 $cf = $xml_cf->addChild('custom_field');
                                 $cf->addAttribute('id', (int) $custom_field['id']);
                                 $v = $cf->addChild('value');
                                 $v2 = $v->addChild('selected_value');
                                 $v3 = $v2->addChild((int) $custom_field['value']);
                                 $v3->addChild('id', (int) $custom_field['value']);
                                 $v3->addChild('display_name', $custom_field['display_name']);
                             } else {
                                 $cf = $xml_cf->addChild('custom_field');
                                 $cf->addAttribute('id', (int) $custom_field['id']);
                                 if (is_array($custom_field['value'])) {
                                     $v0 = $cf->addChild('value');
                                     $v0->addAttribute('type', 'array');
                                     foreach ($custom_field['value'] as $cf_val) {
                                         $v0->addChildCData('value', $cf_val);
                                     }
                                 } else {
                                     $cf->addChildCData('value', $custom_field['value']);
                                 }
                             }
                         }
                     }
                 }
                 break;
             case 'uploads':
                 if (is_array($property_value) and !empty($property_value)) {
                     $xml_up = $xml->addChild('uploads');
                     $xml_up->addAttribute('type', 'array');
                     foreach ($property_value as $upload) {
                         if (isset($upload['filename']) and isset($upload['token']) and isset($upload['content_type'])) {
                             $up = $xml_up->addChild('upload');
                             $up->addChild('token', $upload['token']);
                             $up->addChild('filename', $upload['filename']);
                             $up->addChild('content_type', $upload['content_type']);
                         }
                     }
                 }
                 break;
             case 'easy_crm_case_items':
                 if (is_array($property_value) and !empty($property_value)) {
                     $xml_up = $xml->addChild('easy_crm_case_items_attributes');
                     $xml_up->addAttribute('type', 'array');
                     foreach ($property_value as $item) {
                         $it = $xml_up->addChild('easy_crm_case_item');
                         foreach ($item as $item_property_name => $item_property_value) {
                             $it->addChildCData($item_property_name, $item_property_value);
                         }
                     }
                 }
                 break;
             default:
                 if (is_array($property_value)) {
                     $xml_arr = $xml->addChild($property_name);
                     $xml_arr->addAttribute('type', 'array');
                     foreach ($property_value as $pn => $pv) {
                         if (is_array($pv)) {
                             $a_key = array_keys($pv);
                             $a_val = array_values($pv);
                             $xml_arr->addChildCData($a_key[0], (string) $a_val[0]);
                         } else {
                             $xml_arr->addChildCData($pn, (string) $pv);
                         }
                     }
                 } else {
                     $xml->addChildCData($property_name, (string) $property_value);
                 }
                 break;
         }
     }
     if (isset($object->id) and (int) $object->id > 0) {
         $response = $this->_sendRequest('/' . $this->context . '/' . (int) $object->id . '.xml', 'put', $xml->asXML());
         //edit/update
         if ($response->code != 200) {
             if (isset($response->body->error)) {
                 $this->setErrors((array) $response->body->error);
             }
             $this->_writeLog('Store() - update for context "' . $this->context . '/' . $this->context_one . '" failed: ' . implode($this->getErrors()), JLog::ERROR);
             return false;
         }
     } else {
         $response = $this->_sendRequest('/' . $this->context . '.xml', 'post', $xml->asXML());
         //create
         if ($response->code != 201) {
             if (isset($response->body->error)) {
                 $this->setErrors((array) $response->body->error);
             }
             $this->_writeLog('Store() - insert for context "' . $this->context . '/' . $this->context_one . '" failed: ' . implode($this->getErrors()), JLog::ERROR);
             return false;
         }
     }
     $object->id = (int) $response->body->id ? (int) $response->body->id : $object->id;
     return true;
 }