Example #1
0
 private function putpost($method)
 {
     if ($this->_res === null) {
         return false;
     }
     $data = $this->getData();
     if ($method === RestMethodEnum::RM_POST) {
         try {
             $xml = new SimpleXMLElement($data);
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         $xp = $xml->xpath("publication:publication");
         if (count($xp) > 0) {
             $id = strval($xp[0]->attributes()->id);
         }
         if ($id == '') {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         try {
             $xml = new DOMDocument();
             $xml->loadXML(strval(RestAPIHelper::wrapResponse(strval($this->get()))));
             $xpath = new DOMXPath($xml);
             $xpres = $xpath->query('//publication:publication[@id="' . $id . '"]');
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
         if ($xpres !== false) {
             $xnode = $xpres->item(0);
             $xparent = $xnode->parentNode;
             $xparent->removeChild($xpres->item(0));
             try {
                 $xml = new SimpleXMLElement($xml->saveXML());
             } catch (Exception $e) {
                 $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
                 return false;
             }
         } else {
             $this->setError(RestErrorEnum::RE_ITEM_NOT_FOUND);
             return false;
         }
     } else {
         try {
             $xml = new SimpleXMLElement(strval(RestAPIHelper::wrapResponse($this->get())));
         } catch (Exception $e) {
             $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
             return false;
         }
     }
     try {
         $data = new SimpleXMLElement($data);
         $data = $data->xpath("//publication:publication");
     } catch (Exception $e) {
         $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
         return false;
     }
     if (count($data) > 0) {
         $data = $data[0]->asXML();
     } else {
         $this->setError(RestErrorEnum::RE_INVALID_REPRESENTATION);
         return false;
     }
     $data = array($data);
     $xp = $xml->xpath("publication:publication");
     foreach ($xp as $x) {
         $data[] = $x->asXML();
     }
     $data = '<application:application id="' . $this->_res->id . '">' . implode($data) . '</application:application>';
     $data = strval(RestAPIHelper::wrapResponse($data));
     $this->_pars['data'] = $data;
     $res = new RestAppList($this->_pars);
     $ret = $res->post();
     $ret = new SimpleXMLElement(strval($ret->finalize()));
     if ($method === RestMethodEnum::RM_POST) {
         $xp = $ret->xpath('//publication:publication[@id="' . $id . '"]');
     } else {
         // try to find the publication with the max id attribute, which should be the newest
         $xp = $ret->xpath('//publication:publication[not(@id <= preceding-sibling::publication:publication/@id) and not(@id <= following-sibling::publication:publication/@id)]');
         // if this fails, try to find the last publication, which still should be the newest,
         // since results are returned in DB (natural) order
         if (count($xp) == 0 || !is_array($xp)) {
             $xp = $ret->xpath('//publication:publication[last()]');
         }
     }
     $ret = array();
     foreach ($xp as $x) {
         $ret[] = $x->asXML();
     }
     return new XMLFragmentRestResponse($ret);
 }