public function begin() { $this->writer->openMemory(); $this->writer->setIndent(true); $this->writer->setIndentString(str_repeat(' ', 4)); $this->writer->startDocument('1.0', 'UTF-8'); }
public function execute($input) { $this->isRoot = true; $this->writer = new XMLWriter(); $this->writer->openMemory(); $this->writer->startDocument('1.0', 'UTF-8'); $this->writer->setIndent(true); $this->run($input); return $this->writer->flush(); }
private function initialize() { if ($this->streamUri !== null) { $this->cursor->openUri($this->streamUri); } else { $this->cursor->openMemory(); } $this->cursor->startDocument($this->marshaller->getSchemaVersion(), $this->marshaller->getEncoding()); if ($this->marshaller->getIndent() > 0) { $this->cursor->setIndent((int) $this->marshaller->getIndent()); } }
/** * writeChannel writes a Core\Channel instance feed. * * @param Channel $channel * * @return mixed Similar to XMLWriter::flush * * @see http://php.net/manual/function.xmlwriter-flush.php */ public function writeChannel(Channel $channel) { $this->xmlWriter->startDocument(); if ($this->flushEarly) { $this->xmlWriter->flush(); } $this->xmlWriter->startElement('rss'); foreach ($this->namespaces as $ns => $url) { $this->xmlWriter->writeAttribute(sprintf('xmlns:%s', $ns), $url); } $this->xmlWriter->writeAttribute('version', '2.0'); $this->writeObject($channel); $this->xmlWriter->endElement(); return $this->xmlWriter->flush(); }
protected function render(Response $response) { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $response = clone $response; foreach ($response->data as $key => $value) { if ($value instanceof ModelSet) { $response->data[$key] = $value->toArray(); } if ($value instanceof Form) { unset($response->data[$key]); } if (substr($key, 0, 1) == '_') { unset($response->data[$key]); } } if (isset($response->data['application'])) { unset($response->data['application']); } if (isset($response->data['controller'])) { unset($response->data['controller']); } foreach ($response->data as $key => $value) { $xml->startElement($key); $this->createXML($xml, $value); $xml->endElement(); } echo $xml->outputMemory(true); }
public function encode(array $data) { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('api_answer'); $this->write($xml, $data); $xml->endElement(); return $xml->outputMemory(true); }
public function serializeData(DataContainer $data) { $xml = new \XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('root'); $this->recursiveSerialize($xml, $data); $xml->endElement(); $xml->endDocument(); return $xml->outputMemory(); }
private function initialise() { /* Setting XML header */ @header("content-type: application/vnd.google-earth.kml+xml; charset=UTF-8"); /* Initializing the XML Object */ $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->setIndentString(' '); $xml->startDocument('1.0', 'UTF-8'); return $xml; }
function criaXML($response) { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('bvs'); write($xml, $response); $xml->endElement(); header("Content-Type:text/xml"); echo $xml->outputMemory(true); die; }
/** * Converts a PHP array to XML (via XMLWriter) * * @param $array PHP Array * @return xml-string */ public static function arrayToXml($array) { // initalize new XML Writer in memory $xml = new \XmlWriter(); $xml->openMemory(); // with <root> element as top level node $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('root'); // add the $array data in between $this->writeArray($xml, $array); // close with </root> $xml->endElement(); // dump memory return $xml->outputMemory(true); }
public static function output_xml($data, $version = '0.1', $root = 'root', $parameters = array(), $sItemName = 'item') { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement($root); $xml->setIndent(true); if (!empty($version)) { $xml->writeAttribute('version', $version); } foreach ($parameters as $paramk => $paramv) { $xml->writeAttribute($paramk, $paramv); } self::writexml($xml, $data, $sItemName); $xml->endElement(); return $xml->outputMemory(true); }
public function outputSitemap(Kwf_Component_Data $page) { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('urlset'); $xml->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); foreach (array_unique($this->_getSitemap($page)) as $url) { $xml->startElement('url'); $xml->writeElement('loc', $url); $xml->endElement(); } $xml->endElement(); $xml->endDocument(); header('Content-Type: text/xml; charset=utf-8'); echo $xml->outputMemory(true); exit; }
function getXml($results) { $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); // new $xml->setIndentString(" "); // new $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('root'); write($xml, $results); $xml->endElement(); $xml->endDocument(); // new $data = $xml->outputMemory(true); //$file = file_put_contents(APPPATH . '../uploads/data.xml', $data); return $data; }
function exportJournal() { $writer = new XmlWriter(); $writer->openURI($this->outputFolder . "/journal.xml"); $writer->startDocument('1.0', 'utf-8'); $writer->startElement('journal'); $writer->setIndent(true); $this->exportJournalConfig($writer); $this->exportAnnouncements($writer); $this->exportReviewForms($writer); $this->exportUsers($writer); $this->exportGroups($writer); $this->exportSections($writer); $this->exportIssues($writer); $this->exportArticles($writer); $writer->endElement(); $writer->flush(); return $this->outputFolder . "/journal.xml"; }
function outputXml($results, $xsltPath) { /* Setting XML header */ @header("content-type: text/xml; charset=UTF-8"); /* Initializing the XML Object */ $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->setIndentString(' '); $xml->startDocument('1.0', 'UTF-8'); if (isset($xsltPath)) { $xml->WritePi('xml-stylesheet', 'type="text/xsl" href="' . $xsltPath . '"'); } $xml->startElement('callback'); $xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $xml->writeAttribute('xsi:noNamespaceSchemaLocation', 'schema.xsd'); /* Function that converts each array element to an XML node */ function write(XMLWriter $xml, $data) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { #The only time a numeric key would be used is if it labels an array with non-uniqe keys write($xml, $value); continue; } else { $xml->startElement($key); write($xml, $value); $xml->endElement(); continue; } } $xml->writeElement($key, $value); } } /* Calls previously declared function, passing our results array as parameter */ write($xml, $results); /* Closing last XML node */ $xml->endElement(); /* Printing the XML */ echo $xml->outputMemory(true); }
public function executeXml(AgaviRequestDataHolder $request_data) { $report = $this->prepareReport($request_data); $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('application'); $xml->writeAttribute('name', $report['application']); $xml->startElement('status'); $xml->text($report['status']); $xml->endElement(); $connections = $report['connections']; $xml->startElement('connections'); foreach ($connections['stats'] as $name => $value) { $xml->writeAttribute($name, $value); } $xml->writeElement('status', $connections['status']); $xml->startElement('stats'); foreach ($connections['stats'] as $name => $value) { $xml->writeElement($name, $value); } $xml->endElement(); // connections/stats foreach ($connections['details'] as $name => $value) { $xml->startElement('connection'); $xml->writeAttribute('name', $name); if (is_array($value)) { $this->array2xml($value, $xml); } else { $xml->writeCData((string) $value); } $xml->endElement(); } $xml->endElement(); // connections $xml->endElement(); // application $xml->endDocument(); return $xml->outputMemory(); }
public function render($response, $viewModel) { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->setIndent(true); $data = $viewModel->toArray(); $rootNode = $viewModel->getRootNodeName(); if (!$rootNode) { $rootNode = key($data); $data = $data[$rootNode]; } if (is_array($data)) { $xml->startElement($rootNode); $this->write($xml, $data); $xml->endElement(); } else { $xml->writeElement($rootNode, utf8_encode($data)); } $response->headers->set('Content-Type', 'application/xml;charset=utf-8'); $response->setContent($xml->outputMemory(true)); }
public static function encode(array $data, $rootNodeName = 'response') { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement($rootNodeName); $data = self::arrayify($data); function write(XMLWriter $xml, $data) { foreach ($data as $_key => $value) { // check the key isnt a number, (numeric keys invalid in XML) if (is_numeric($_key)) { $key = 'element'; } else { if (!is_string($_key) || empty($_key) || strncmp($_key, '_', 1) === 0) { continue; } else { $key = $_key; } } $xml->startElement($key); // if the key is numeric, add an ID attribute to make tags properly unique if (is_numeric($_key)) { $xml->writeAttribute('id', $_key); } // if the value is an array recurse into it if (is_array($value)) { write($xml, $value); } else { $xml->text($value); } $xml->endElement(); } } // start the writing process write($xml, $data); $xml->endElement(); return $xml->outputMemory(); }
/** * Build an XML Data Set * * @param array $data * Associative Array containing values to be parsed into an XML Data Set(s) * @param string $startElement * Root Opening Tag, default data * @return string XML String containig values * @return mixed Boolean false on failure, string XML result on success */ public function buildXML($data, $startElement = 'data') { if (!is_array($data)) { $err = 'Invalid variable type supplied, expected array not found on line ' . __LINE__ . " in Class: " . __CLASS__ . " Method: " . __METHOD__; trigger_error($err); // if($this->_debug) echo $err; return false; // return false error occurred } $xml = new \XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->setIndentString(' '); if ($this->version || $this->encoding) { $xml->startDocument($this->version, $this->encoding); } $xml->startElement($startElement); $this->writeEl($xml, $data); $xml->endElement(); // write end element // returns the XML results return $xml->outputMemory(true); }
/** * generic function to get incidents by given set of parameters */ function _getIncidents($where = '', $limit = '') { $items = array(); //will hold the items from the query $data = array(); //items to parse to json $json_incidents = array(); //incidents to parse to json $media_items = array(); //incident media $json_incident_media = array(); //incident media $retJsonOrXml = ''; //will hold the json/xml string to return $replar = array(); //assists in proper xml generation // Doing this manaully. It was wasting my time trying modularize it. // Will have to visit this again after a good rest. I mean a good rest. //XML elements $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('response'); $xml->startElement('payload'); $xml->startElement('incidents'); //find incidents $query = "SELECT i.id AS incidentid,i.incident_title AS incidenttitle," . "i.incident_description AS incidentdescription, i.incident_date AS " . "incidentdate, i.incident_mode AS incidentmode,i.incident_active AS " . "incidentactive, i.incident_verified AS incidentverified, l.id AS " . "locationid,l.location_name AS locationname,l.latitude AS " . "locationlatitude,l.longitude AS locationlongitude FROM incident AS i " . "INNER JOIN location as l on l.id = i.location_id " . "{$where} {$limit}"; $items = $this->db->query($query); $i = 0; foreach ($items as $item) { if ($this->responseType == 'json') { $json_incident_media = array(); } //build xml file $xml->startElement('incident'); $xml->writeElement('id', $item->incidentid); $xml->writeElement('title', $item->incidenttitle); $xml->writeElement('description', $item->incidentdescription); $xml->writeElement('date', $item->incidentdate); $xml->writeElement('mode', $item->incidentmode); $xml->writeElement('active', $item->incidentactive); $xml->writeElement('verified', $item->incidentverified); $xml->startElement('location'); $xml->writeElement('id', $item->locationid); $xml->writeElement('name', $item->locationname); $xml->writeElement('latitude', $item->locationlatitude); $xml->writeElement('longitude', $item->locationlongitude); $xml->endElement(); $xml->startElement('categories'); //fetch categories $query = " SELECT c.category_title AS categorytitle, c.id AS cid " . "FROM category AS c INNER JOIN incident_category AS ic ON " . "ic.category_id = c.id WHERE ic.incident_id =" . $item->incidentid . " LIMIT 0 , 20"; $category_items = $this->db->query($query); foreach ($category_items as $category_item) { $xml->startElement('category'); $xml->writeElement('id', $category_item->cid); $xml->writeElement('title', $category_item->categorytitle); $xml->endElement(); } $xml->endElement(); //end categories //fetch media associated with an incident $query = "SELECT m.id as mediaid, m.media_title AS mediatitle, " . "m.media_type AS mediatype, m.media_link AS medialink, " . "m.media_thumb AS mediathumb FROM media AS m " . "INNER JOIN incident AS i ON i.id = m.incident_id " . "WHERE i.id =" . $item->incidentid . " LIMIT 0 , 20"; $media_items = $this->db->query($query); if (count($media_items) > 0) { $xml->startElement('mediaItems'); foreach ($media_items as $media_item) { if ($this->responseType == 'json') { $json_incident_media[] = $media_item; } else { $xml->startElement('media'); $xml->writeElement('id', $media_item->mediaid); $xml->writeElement('title', $media_item->mediatitle); $xml->writeElement('type', $media_item->mediatype); $xml->writeElement('link', $media_item->medialink); $xml->writeElement('thumb', $media_item->mediathumb); $xml->endElement(); } } $xml->endElement(); // media } $xml->endElement(); // end incident //needs different treatment depending on the output if ($this->responseType == 'json') { $json_incidents[] = array("incident" => $item, "media" => $json_incident_media); } } //create the json array $data = array("payload" => array("incidents" => $json_incidents), "error" => $this->_getErrorMsg(0)); if ($this->responseType == 'json') { $retJsonOrXml = $this->_arrayAsJSON($data); return $retJsonOrXml; } else { $xml->endElement(); //end incidents $xml->endElement(); // end payload $xml->startElement('error'); $xml->writeElement('code', 0); $xml->writeElement('message', 'No Error'); $xml->endElement(); //end error $xml->endElement(); // end response return $xml->outputMemory(true); } //return $retJsonOrXml; }
/** * Serializes given array. The array indices must be string to use them as * as element name. * * @param array $array The object to serialize represented in array. * @param array $properties The used properties in the serialization process. * * @return string */ public function serialize($array, $properties = null) { $xmlVersion = '1.0'; $xmlEncoding = 'UTF-8'; $standalone = Utilities::tryGetValue($properties, self::STANDALONE); $defaultTag = Utilities::tryGetValue($properties, self::DEFAULT_TAG); $rootName = Utilities::tryGetValue($properties, self::ROOT_NAME); $docNamespace = Utilities::tryGetValue($array, Resources::XTAG_NAMESPACE, null); if (!is_array($array)) { return false; } $xmlw = new \XmlWriter(); $xmlw->openMemory(); $xmlw->setIndent(true); $xmlw->startDocument($xmlVersion, $xmlEncoding, $standalone); if (is_null($docNamespace)) { $xmlw->startElement($rootName); } else { foreach ($docNamespace as $uri => $prefix) { $xmlw->startElementNS($prefix, $rootName, $uri); break; } } unset($array[Resources::XTAG_NAMESPACE]); self::_arr2xml($xmlw, $array, $defaultTag); $xmlw->endElement(); return $xmlw->outputMemory(true); }
/** * Construct the whole DCAT-AP document given an array of dump info * * @param array $data data-blob of i18n and config variables * @return string: xmldata */ function outputXml(array $data) { // Initializing the XML Object $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->setIndentString(' '); // set namespaces $xml->startDocument('1.0', 'UTF-8'); $xml->startElementNS('rdf', 'RDF', null); $xml->writeAttributeNS('xmlns', 'rdf', null, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); $xml->writeAttributeNS('xmlns', 'dcterms', null, 'http://purl.org/dc/terms/'); $xml->writeAttributeNS('xmlns', 'dcat', null, 'http://www.w3.org/ns/dcat#'); $xml->writeAttributeNS('xmlns', 'foaf', null, 'http://xmlns.com/foaf/0.1/'); $xml->writeAttributeNS('xmlns', 'adms', null, 'http://www.w3.org/ns/adms#'); $xml->writeAttributeNS('xmlns', 'vcard', null, 'http://www.w3.org/2006/vcard/ns#'); // Calls previously declared functions to construct xml writePublisher($xml, $data); writeContactPoint($xml, $data); $dataset = array(); // Live dataset and distributions $liveDistribs = writeDistribution($xml, $data, 'ld', null); if ($data['config']['api-enabled']) { $liveDistribs = array_merge($liveDistribs, writeDistribution($xml, $data, 'api', null)); } array_push($dataset, writeDataset($xml, $data, null, $liveDistribs)); // Dump dataset and distributions if ($data['config']['dumps-enabled']) { foreach ($data['dumps'] as $key => $value) { $distIds = writeDistribution($xml, $data, 'dump', $key); array_push($dataset, writeDataset($xml, $data, $key, $distIds)); } } writeCatalog($xml, $data, $dataset); // Closing last XML node $xml->endElement(); // Printing the XML return $xml->outputMemory(true); }
// -->g_h // -->g_a // -->p_h // -->p_a // -->m_parts // -->p1(h,a) // -->p2(h,a) // -->odds // -->bo(id)[] // -->o1 // -->ox // -->o2 // $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('root'); function write(XMLWriter $xml, $data) { foreach ($data as $key => $value) { if (is_array($value)) { $xml->startElement($key); write($xml, $value); $xml->endElement(); continue; } $xml->writeElement($key, $value); } } write($xml, $data); $xml->endElement();
/** * Function builds OpenPayU Xml Document * @access public * @param string $data * @param string $start_element * @param integer $request * @param string $xml_version * @param string $xml_encoding * @return string $xml */ public static function buildOpenPayUDocument($data, $start_element, $request = 1, $xml_version = '1.0', $xml_encoding = 'UTF-8') { if (!is_array($data)) { return false; } $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument($xml_version, $xml_encoding); $xml->startElementNS(null, 'OpenPayU', 'http://www.openpayu.com/openpayu.xsd'); $header = $request == 1 ? 'HeaderRequest' : 'HeaderResponse'; $xml->startElement($header); $xml->writeElement('Algorithm', 'MD5'); $xml->writeElement('SenderName', 'POSID=' . OpenPayUConfiguration::getMerchantPosid() . ';CUSTOM_PLUGIN=PRESTASHOP'); $xml->writeElement('Version', $xml_version); $xml->endElement(); // domain level - open $xml->startElement(OpenPayUDomain::getDomain4Message($start_element)); // message level - open $xml->startElement($start_element); self::arr2xml($xml, $data); // message level - close $xml->endElement(); // domain level - close $xml->endElement(); // document level - close $xml->endElement(); return $xml->outputMemory(true); }
/** * Function builds OpenPayU Xml Document * @access public * @param string $data * @param string $rootElement * @param string $version * @param string $encoding * @param string $rootElementXsi * @return string $xml */ public static function buildXmlFromArray($data, $rootElement, $version = '1.0', $encoding = 'UTF-8', $rootElementXsi = null) { if (!is_array($data)) { return null; } $xml = new XmlWriter(); $xml->openMemory(); $xml->setIndent(true); $xml->startDocument($version, $encoding); $xml->startElementNS(null, 'OpenPayU', 'http://www.openpayu.com/20/openpayu.xsd'); $xml->startElement($rootElement); if (!empty($rootElementXsi)) { $xml->startAttributeNs('xsi', 'type', 'http://www.w3.org/2001/XMLSchema-instance'); $xml->text($rootElementXsi); $xml->endAttribute(); } self::convertArrayToXml($xml, $data); $xml->endElement(); $xml->endElement(); $xml->endDocument(); return trim($xml->outputMemory(true)); }
/** * Generic function to get reports by given set of parameters * * @param string $where SQL where clause * @param int $limit No. of records to return - set to 20 by default * @return string XML or JSON string */ public function _get_incidents($where = array()) { // STEP 1. // Get the incidents $items = Incident_Model::get_incidents($where, $this->list_limit, $this->order_field, $this->sort); //No record found. if ($items->count() == 0) { return $this->response(4, $this->error_messages); } // Records found - proceed // Set the no. of records returned $this->record_count = $items->count(); // Will hold the XML/JSON string to return $ret_json_or_xml = ''; $json_reports = array(); $json_report_media = array(); $json_report_categories = array(); $json_incident_media = array(); $upload_path = str_replace("media/uploads/", "", Kohana::config('upload.relative_directory') . "/"); //XML elements $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('response'); $xml->startElement('payload'); $xml->writeElement('domain', $this->domain); $xml->startElement('incidents'); // Records found, proceed // Store the incident ids $incidents_ids = array(); foreach ($items as $item) { $incident_ids[] = $item->incident_id; } // // STEP 2. // Fetch the incident categories // $this->query = "SELECT c.category_title AS categorytitle, ic.incident_id, " . "c.id AS cid, c.category_image_thumb AS categorythumb, " . "d.decayimage_thumb AS decayimagethumb " . "FROM " . $this->table_prefix . "category AS c " . "INNER JOIN " . $this->table_prefix . "incident_category AS ic ON ic.category_id = c.id " . "LEFT JOIN " . $this->table_prefix . "decayimage as d ON c.id = d.category_id " . "WHERE ic.incident_id IN (" . implode(',', $incident_ids) . ")"; // Execute the query $incident_categories = $this->db->query($this->query); // To hold the incident category items $category_items = array(); // Temporary counter $i = 1; // Fetch items into array foreach ($incident_categories as $incident_category) { $category_items[$incident_category->incident_id][$i]['cid'] = $incident_category->cid; $category_items[$incident_category->incident_id][$i]['categorytitle'] = $incident_category->categorytitle; $category_items[$incident_category->incident_id][$i]['categorythumb'] = $incident_category->categorythumb; $category_items[$incident_category->incident_id][$i]['decayimagethumb'] = $incident_category->decayimagethumb; $i++; } // Free temporary variables from memory unset($incident_categories); // // STEP 3. // Fetch the media associated with all the incidents // $this->query = "SELECT i.id AS incident_id, m.id AS mediaid, m.media_title AS mediatitle, " . "m.media_type AS mediatype, m.media_link AS medialink, m.media_thumb AS mediathumb " . "FROM " . $this->table_prefix . "media AS m " . "INNER JOIN " . $this->table_prefix . "incident AS i ON i.id = m.incident_id " . "WHERE i.id IN (" . implode(",", $incident_ids) . ")"; $media_items_result = $this->db->query($this->query); // To store the fetched media items $media_items = array(); // Reset the temporary counter $i = 1; // Fetch items into array foreach ($media_items_result as $media_item) { $media_items[$media_item->incident_id][$i]['mediaid'] = $media_item->mediaid; $media_items[$media_item->incident_id][$i]['mediatitle'] = $media_item->mediatitle; $media_items[$media_item->incident_id][$i]['mediatype'] = $media_item->mediatype; $media_items[$media_item->incident_id][$i]['medialink'] = $media_item->medialink; $media_items[$media_item->incident_id][$i]['mediathumb'] = $media_item->mediathumb; $i++; } // Free temporary variables unset($media_items_result, $i); // // STEP 4. // Fetch the comments associated with the incidents // if ($this->comments) { $this->query = "SELECT id, incident_id, comment_author, comment_email, " . "comment_description, comment_rating, comment_date " . "FROM " . $this->table_prefix . "comment AS c " . "WHERE c.incident_id IN (" . implode(',', $incident_ids) . ")"; // Execute the query $incident_comments = $this->db->query($this->query); // To hold the incident category items $comment_items = array(); // Temporary counter $i = 1; // Fetch items into array foreach ($incident_comments as $incident_comment) { $comment_items[$incident_comment->incident_id][$i]['id'] = $incident_comment->id; $comment_items[$incident_comment->incident_id][$i]['incident_id'] = $incident_comment->incident_id; $comment_items[$incident_comment->incident_id][$i]['comment_author'] = $incident_comment->comment_author; $comment_items[$incident_comment->incident_id][$i]['comment_email'] = $incident_comment->comment_email; $comment_items[$incident_comment->incident_id][$i]['comment_description'] = $incident_comment->comment_description; $comment_items[$incident_comment->incident_id][$i]['comment_rating'] = $incident_comment->comment_rating; $comment_items[$incident_comment->incident_id][$i]['comment_date'] = $incident_comment->comment_date; $i++; } // Free temporary variables from memory unset($incident_comments); } // // STEP 5. // Return XML // foreach ($items as $item) { // Build xml file $xml->startElement('incident'); $xml->writeElement('id', $item->incident_id); $xml->writeElement('title', $item->incident_title); $xml->writeElement('description', $item->incident_description); $xml->writeElement('date', $item->incident_date); $xml->writeElement('mode', $item->incident_mode); $xml->writeElement('active', $item->incident_active); $xml->writeElement('verified', $item->incident_verified); $xml->startElement('location'); $xml->writeElement('id', $item->location_id); $xml->writeElement('name', $item->location_name); $xml->writeElement('latitude', $item->latitude); $xml->writeElement('longitude', $item->longitude); $xml->endElement(); $xml->startElement('categories'); $json_report_categories[$item->incident_id] = array(); // Check if the incident id exists if (isset($category_items[$item->incident_id])) { foreach ($category_items[$item->incident_id] as $category_item) { if ($this->response_type == 'json') { $category = array("id" => $category_item['cid'], "title" => $category_item['categorytitle']); $category["icon"] = url::base() . Kohana::config('upload.relative_directory') . '/' . $category_item['categorythumb']; if ($category_item['decayimagethumb']) { if ($category_item['decayimagethumb'] == $this->default_decayimage_thumb) { $category['decayimage'] = url::site() . '/plugins/decayimage/images/' . $category_item['decayimagethumb']; } else { $category['decayimage'] = url::base() . Kohana::config('upload.relative_directory') . '/' . $category_item['decayimagethumb']; } } $json_report_categories[$item->incident_id][] = array("category" => $category); } else { $xml->startElement('category'); $xml->writeElement('id', $category_item['cid']); $xml->writeElement('title', $category_item['categorytitle']); $xml->endElement(); } } } // End categories $xml->endElement(); $xml->startElement('comments'); $json_report_comments[$item->incident_id] = array(); // Check if the incident id exists if (isset($comment_items[$item->incident_id])) { foreach ($comment_items[$item->incident_id] as $comment_item) { if ($this->response_type == 'json') { $json_report_comments[$item->incident_id][] = array("comment" => $comment_item); } else { $xml->startElement('comment'); $xml->writeElement('id', $comment_item['id']); $xml->writeElement('comment_author', $comment_item['comment_author']); $xml->writeElement('comment_email', $comment_item['comment_email']); $xml->writeElement('comment_description', $comment_item['comment_description']); $xml->writeElement('comment_rating', $comment_item['comment_rating']); $xml->writeElement('comment_date', $comment_item['comment_date']); $xml->endElement(); } } } // End comments $xml->endElement(); $json_report_media[$item->incident_id] = array(); if (count($media_items) > 0) { if (isset($media_items[$item->incident_id]) and count($media_items[$item->incident_id]) > 0) { $xml->startElement('mediaItems'); foreach ($media_items[$item->incident_id] as $media_item) { $url_prefix = url::base() . Kohana::config('upload.relative_directory') . '/'; // If our media is not an image, we don't need to show an upload path if ($media_item['mediatype'] != 1) { $upload_path = ''; } elseif ($media_item['mediatype'] == 1 and valid::url($media_item['medialink']) == TRUE) { // If our media is an img and is a valid URL, don't show the upload path or prefix $upload_path = ''; $url_prefix = ''; } if ($this->response_type == 'json') { $json_report_media[$item->incident_id][] = array("id" => $media_item['mediaid'], "type" => $media_item['mediatype'], "link" => $upload_path . $media_item['medialink'], "thumb" => $upload_path . $media_item['mediathumb']); // If we are look at certain types of media, add some fields if ($media_item['mediatype'] == 1) { // Grab that last key up there $add_to_key = key($json_report_media[$item->incident_id]) + 1; // Give a full absolute URL to the image $json_report_media[$item->incident_id][$add_to_key]["thumb_url"] = $url_prefix . $upload_path . $media_item['mediathumb']; $json_report_media[$item->incident_id][$add_to_key]["link_url"] = $url_prefix . $upload_path . $media_item['medialink']; } } else { $xml->startElement('media'); if ($media_item['mediaid'] != "") { $xml->writeElement('id', $media_item['mediaid']); } if ($media_item['mediatitle'] != "") { $xml->writeElement('title', $media_item['mediatitle']); } if ($media_item['mediatype'] != "") { $xml->writeElement('type', $media_item['mediatype']); } if ($media_item['medialink'] != "") { $xml->writeElement('link', $upload_path . $media_item['medialink']); } if ($media_item['mediathumb'] != "") { $xml->writeElement('thumb', $upload_path . $media_item['mediathumb']); } if ($media_item['mediathumb'] != "" and $media_item['mediatype'] == 1) { $add_to_key = key($json_report_media[$item->incident_id]) + 1; $xml->writeElement('thumb_url', $url_prefix . $upload_path . $media_item['mediathumb']); $xml->writeElement('link_url', $url_prefix . $upload_path . $media_item['medialink']); } $xml->endElement(); } } $xml->endElement(); // Media } } $xml->endElement(); // End incident // Check for response type if ($this->response_type == 'json') { $json_reports[] = array("incident" => array("incidentid" => $item->incident_id, "incidenttitle" => $item->incident_title, "incidentdescription" => $item->incident_description, "incidentdate" => $item->incident_date, "incidentmode" => $item->incident_mode, "incidentactive" => $item->incident_active, "incidentverified" => $item->incident_verified, "locationid" => $item->location_id, "locationname" => $item->location_name, "locationlatitude" => $item->latitude, "locationlongitude" => $item->longitude, "incidenthasended" => $this->incidentHasEnded($item->incident_id)), "categories" => $json_report_categories[$item->incident_id], "media" => $json_report_media[$item->incident_id], "comments" => $json_report_comments[$item->incident_id]); } } // Get the default decayimage icon $decayimage_default_icon = ORM::factory('decayimage', 1); if ($decayimage_default_icon->decayimage_thumb == $this->default_decayimage_thumb) { $decayimage_default_icon = url::site() . '/plugins/decayimage/images/' . $decayimage_default_icon->decayimage_thumb; } else { $decayimage_default_icon = $prefix . '/' . $decayimage_default_icon->decayimage_thumb; } // Create the JSON array $data = array("payload" => array("domain" => $this->domain, "incidents" => $json_reports, "decayimage_default_icon" => $decayimage_default_icon), "error" => $this->api_service->get_error_msg(0)); if ($this->response_type == 'json') { return $this->array_as_json($data); } else { $xml->endElement(); //end incidents $xml->endElement(); // end payload $xml->startElement('error'); $xml->writeElement('code', 0); $xml->writeElement('message', 'No Error'); $xml->endElement(); //end error $xml->endElement(); // end response return $xml->outputMemory(true); } }
/** * Encodes simple multilevel array and hash values to valid xml string * * @param mixed $hash * @param string $childName * @return string */ public static function xmlEncode($hash, $childName = 'child') { $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('root'); self::xmlWrite($xml, $hash, $childName); $xml->endElement(); return $xml->outputMemory(true); }
/** * [xmlCreate] * @param string $startTag * @param array $array * @return string */ protected function xmlCreate($startTag, $array) { $xml = new \XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'utf-8'); $xml->startElement($startTag); $this->xmlWrite($xml, $array); $xml->endElement(); return $xml->outputMemory(true); }
/** * Generic function to get reports by given set of parameters */ private function _get_reports($where = '', $limit = '') { $ret_json_or_xml = ''; // Will hold the JSON/XML string $json_reports = array(); $json_report_media = array(); $json_incident_media = array(); $json_report_categories = array(); //XML elements $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('response'); $xml->startElement('payload'); $xml->writeElement('domain', $this->domain); $xml->startElement('incidents'); //find incidents $this->query = "SELECT i.id AS incidentid,\n i.incident_title AS incidenttitle," . "i.incident_description AS incidentdescription, " . "i.incident_date AS incidentdate, " . "i.incident_mode AS incidentmode, " . "i.incident_active AS incidentactive, " . "i.incident_verified AS incidentverified, " . "l.id AS locationid, " . "l.location_name AS locationname, " . "l.latitude AS locationlatitude, " . "l.longitude AS locationlongitude " . "FROM " . $this->table_prefix . "incident AS i " . "INNER JOIN " . $this->table_prefix . "location as l on l.id = i.location_id " . "{$where} {$limit}"; $items = $this->db->query($this->query); $i = 0; //No record found. if ($items->count() == 0) { return $this->response(4); } foreach ($items as $item) { if ($this->response_type == 'json') { $json_report_media = array(); $json_report_categories = array(); } //build xml file $xml->startElement('incident'); $xml->writeElement('id', $item->incidentid); $xml->writeElement('title', $item->incidenttitle); $xml->writeElement('description', $item->incidentdescription); $xml->writeElement('date', $item->incidentdate); $xml->writeElement('mode', $item->incidentmode); $xml->writeElement('active', $item->incidentactive); $xml->writeElement('verified', $item->incidentverified); $xml->startElement('location'); $xml->writeElement('id', $item->locationid); $xml->writeElement('name', $item->locationname); $xml->writeElement('latitude', $item->locationlatitude); $xml->writeElement('longitude', $item->locationlongitude); $xml->endElement(); $xml->startElement('categories'); //fetch categories $this->query = " SELECT c.category_title AS categorytitle, \n c.id AS cid " . "FROM " . $this->table_prefix . "category AS c INNER JOIN " . $this->table_prefix . "incident_category AS ic ON " . "ic.category_id = c.id WHERE ic.incident_id =" . $item->incidentid; $category_items = $this->db->query($this->query); foreach ($category_items as $category_item) { if ($this->response_type == 'json') { $json_report_categories[] = array("category" => array("id" => $category_item->cid, "title" => $category_item->categorytitle)); } else { $xml->startElement('category'); $xml->writeElement('id', $category_item->cid); $xml->writeElement('title', $category_item->categorytitle); $xml->endElement(); } } $xml->endElement(); //end categories //fetch media associated with an incident $this->query = "SELECT m.id as mediaid, m.media_title AS \n mediatitle, " . "m.media_type AS mediatype, m.media_link AS medialink, " . "m.media_thumb AS mediathumb FROM " . $this->table_prefix . "media AS m " . "INNER JOIN " . $this->table_prefix . "incident AS i ON i.id = m.incident_id " . "WHERE i.id =" . $item->incidentid; $media_items = $this->db->query($this->query); if (count($media_items) > 0) { $xml->startElement('mediaItems'); foreach ($media_items as $media_item) { if ($this->response_type == 'json') { $json_incident_media[] = array("id" => $media_item->mediaid, "type" => $media_item->mediatype, "link" => $media_item->medialink); } else { $xml->startElement('media'); if ($media_item->mediaid != "") { $xml->writeElement('id', $media_item->mediaid); } if ($media_item->mediatitle != "") { $xml->writeElement('title', $media_item->mediatitle); } if ($media_item->mediatype != "") { $xml->writeElement('type', $media_item->mediatype); } if ($media_item->medialink != "") { $xml->writeElement('link', $media_item->medialink); } if ($media_item->mediathumb != "") { $xml->writeElement('thumb', $media_item->mediathumb); } $xml->endElement(); } } $xml->endElement(); // media } $xml->endElement(); // end incident //needs different treatment depending on the output if ($this->response_type == 'json') { $json_reports[] = array("incident" => $item, "categories" => $json_report_categories, "media" => $json_report_media); } } // Create the json array $data = array("payload" => array("domain" => $this->domain, "incidents" => $json_reports), "error" => $this->api_service->get_error_msg(0)); if ($this->response_type == 'json') { $ret_json_or_xml = $this->array_as_json($data); return $ret_json_or_xml; } else { $xml->endElement(); //end incidents $xml->endElement(); // end payload $xml->startElement('error'); $xml->writeElement('code', 0); $xml->writeElement('message', 'No Error'); $xml->endElement(); //end error $xml->endElement(); // end response return $xml->outputMemory(true); } }
/** * do the search via the API * @param q - the search string. * * @param limit - the value to limit by. * * @return the search result. */ function _doSearch($q, $limit) { /** * This is mostly borrowed from the search functionality * see application/controller/search.php */ $search_query = ""; $keyword_string = ""; $where_string = ""; $plus = ""; $or = ""; $search_info = ""; $html = ""; $json_searches = array(); // Stop words that we won't search for // Add words as needed!! $stop_words = array('the', 'and', 'a', 'to', 'of', 'in', 'i', 'is', 'that', 'it', 'on', 'you', 'this', 'for', 'but', 'with', 'are', 'have', 'be', 'at', 'or', 'as', 'was', 'so', 'if', 'out', 'not'); $retJsonOrXml = ''; //will hold the json/xml string to return $replar = array(); //assists in proper xml generation // Doing this manaully. It was wasting my time trying to modularize it. // Will have to visit this again after a good rest. I mean a good rest. //XML elements $xml = new XmlWriter(); $xml->openMemory(); $xml->startDocument('1.0', 'UTF-8'); $xml->startElement('response'); $xml->startElement('payload'); $xml->startElement('searches'); $keywords = explode(' ', $q); if (is_array($keywords) && !empty($keywords)) { array_change_key_case($keywords, CASE_LOWER); $i = 0; foreach ($keywords as $value) { if (!in_array($value, $stop_words) && !empty($value)) { $chunk = mysql_real_escape_string($value); if ($i > 0) { $plus = ' + '; $or = ' OR '; } // Give relevancy weighting // Title weight = 2 // Description weight = 1 $keyword_string = $keyword_string . $plus . "(CASE WHEN incident_title LIKE '%{$chunk}%' THEN 2 ELSE 0 END) + (CASE WHEN incident_description LIKE '%{$chunk}%' THEN 1 ELSE 0 END)"; $where_string = $where_string . $or . "incident_title LIKE '%{$chunk}%' OR incident_description LIKE '%{$chunk}%'"; $i++; } } if (!empty($keyword_string) && !empty($where_string)) { $search_query = "SELECT *, (" . $keyword_string . ") AS relevance FROM incident WHERE (" . $where_string . ") ORDER BY relevance DESC "; } } if (!empty($search_query)) { if (!empty($limit) && is_numeric($limit)) { $l = "LIMIT 0 ," . $limit; } else { $l = " LIMIT ," . $this->list_limit; } $total_items = ORM::factory('incident')->where($where_string)->count_all(); $s = $search_query . $l; $query = $this->db->query($s); $this->total_records = $total_items; // Results Bar if ($total_items != 0) { if ($this->responseType == 'json') { $json_searches[] = array("total" => $total_items); } else { $xml->writeElement('total', $total_items); } } else { $xml->writeElement('total', $total_items); //create the json array $data = array("payload" => array("searches" => $json_searches), "error" => $this->_getErrorMsg(0)); if ($this->responseType == 'json') { $retJsonOrXml = $this->_arrayAsJSON($data); return $retJsonOrXml; } else { $xml->endElement(); //end searches $xml->endElement(); // end payload $xml->startElement('error'); $xml->writeElement('code', 0); $xml->writeElement('message', 'No Error'); $xml->endElement(); //end error $xml->endElement(); // end response return $xml->outputMemory(true); } } foreach ($query as $search) { $incident_id = $search->id; $incident_title = $search->incident_title; $incident_description = $search->incident_description; // Remove any markup, otherwise trimming below will mess things up $incident_description = strip_tags($incident_description); $incident_date = date('D M j Y g:i:s a', strtotime($search->incident_date)); //needs different treatment depending on the output if ($this->responseType == 'json') { $json_searches[] = array("search" => array("id" => $incident_id, "title" => $search->incident_title, "description" => $incident_description, "date" => $incident_date, "relevance" => $search->relevance)); } else { //build xml file $xml->startElement('search'); $xml->writeElement('id', $incident_id); $xml->writeElement('title', $incident_title); $xml->writeElement('description', $incident_description); $xml->writeElement('date', $incident_date); $xml->writeElement('relevance', $search->relevance); $xml->endElement(); // end searches } } } //create the json array $data = array("payload" => array("searches" => $json_searches), "error" => $this->_getErrorMsg(0)); if ($this->responseType == 'json') { $retJsonOrXml = $this->_arrayAsJSON($data); return $retJsonOrXml; } else { $xml->endElement(); //end searches $xml->endElement(); // end payload $xml->startElement('error'); $xml->writeElement('code', 0); $xml->writeElement('message', 'No Error'); $xml->endElement(); //end error $xml->endElement(); // end response return $xml->outputMemory(true); } }