protected function action() { $node = $this->template->getElementById('siteContent'); try { if (!$this->request_exists('export') || !($export = $this->request('export')) || !array_key_exists('exports', $this->args) || !is_array($this->args['exports']) || !array_key_exists($export, $this->args['exports']) || !is_array($args = $this->args['exports'][$export])) { return $this->action_menu($node); } $this->show_status("Beginning Export"); if (!array_key_exists('curl_opts', $args) || !is_array($curl_opts = $args['curl_opts']) || !array_key_exists('url', $args['curl_opts']) || !array_key_exists('pass', $args['curl_opts']) || !array_key_exists('user', $args['curl_opts']) || !array_key_exists('report_opts', $args) || !is_array($report_opts = $args['report_opts']) || !array_key_exists('report_view', $report_opts) || !array_key_exists('transform', $report_opts) || !($xml_export = new I2CE_CustomReport_Display_Export($this, $report_opts['report_view']))) { return false; } } catch (Exception $e) { return false; //need better error message/handling } $xml_export->setTransform($report_opts['transform']); $xml_export->setStyle('xml'); $this->show_status("Validated Export Paramaters"); if (!($export = $xml_export->generateExport()) || !$this->show_status_expander("Generated Export", $export) || !($file = tempnam(sys_get_temp_dir(), 'DHIS_POST_') . '.xml') || !file_put_contents($file, $export) || !is_resource($fp = fopen($file, "r"))) { return false; } $this->show_status("Sending file sized:" . filesize($file)); if (!is_resource($ch = curl_init($curl_opts['url'])) || !$this->show_status("Opening Connection to DHIS Dashboard") || !curl_setopt($ch, CURLOPT_POST, 1) || !curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) || !curl_setopt($ch, CURLOPT_HEADER, 0) || !curl_setopt($ch, CURLOPT_USERPWD, $curl_opts['user'] . ':' . $curl_opts['pass']) || !curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml')) || !$this->show_status("Sending Data to DHIS Dashboard")) { return false; } curl_setopt($ch, CURLOPT_POSTFIELDS, $export); $curl_out = curl_exec($ch); if ($err = curl_errno($ch)) { $this->show_status('Data Error (' . $err . ') ' . curl_error($ch)); } else { $this->show_status("Data Sent Succesfully"); } curl_close($ch); unlink($file); if (($outNode = $this->template->createElement('pre')) instanceof DOMNode && $node->appendChild($outNode)) { $this->show_status_expander("DHIS2 Response", $curl_out); if (!$this->process_response($curl_out)) { $this->show_status("Invalid Response From DHIS"); } } return true; }
protected function action() { /* * Get org unit hierarchy from query and store these information in table * * */ // Fetch last time stored magic data if it is created $lastreporttime = '2000-1-1 00:00:00'; I2CE::getConfig()->setIfIsSet($lastreporttime, "/modules/DHIS-Dashboard/Time/SendTime"); $username = false; $password = false; if (!I2CE::getConfig()->setIfIsSet($username, "/modules/DHIS-Dashboard/credentials/username") || !$username) { I2CE::raiseError("No username set"); return false; } if (!I2CE::getConfig()->setIfIsSet($password, "/modules/DHIS-Dashboard/credentials/password") || !$username) { I2CE::raiseError("No password set"); return false; } $last_update = date('y-m-d h:m:s'); // echo $last_update; $orgQuery = "select asd.row,asd.id,asd.name,asd.parent,asd1.row,asd.last_modified as parentid from\n\n (select (@row:=@row+1) as row,id,name,parent,last_modified from hippo_country,\n (SELECT @row:=0) AS row_count\n union\n select (@row:=@row+1) as row,hr.id,hr.name,hr.country,hr.last_modified from hippo_region hr\n inner join hippo_country hc on hc.id=hr.country,\n (SELECT @row:=0) AS row_count\n union\n select (@row:=@row+1) as row,hd.id,hd.name,hd.region,hd.last_modified from hippo_district hd\n inner join hippo_region hr on hd.region=hr.id,\n (SELECT @row:=0) AS row_count\n union\n select (@row:=@row+1) as row,co.id,co.name,co.district,co.last_modified from hippo_county co\n inner join hippo_district hd on co.district=hd.id,\n (SELECT @row:=0) AS row_count\n union\n select (@row:=@row+1) as row,hf.id,hf.name,hf.location,hf.last_modified from hippo_facility hf,\n (SELECT @row:=0) AS row_count\n where location is not null\n )asd\n\n left join\n\n (select (@row1:=@row1+1) as row,id,name,parent from hippo_country,\n (SELECT @row1:=0) AS row_count\n union\n select (@row1:=@row1+1) as row,hr.id,hr.name,hr.country from hippo_region hr\n inner join hippo_country hc on hc.id=hr.country,\n (SELECT @row1:=0) AS row_count\n union\n select (@row1:=@row1+1) as row,hd.id,hd.name,hd.region from hippo_district hd\n inner join hippo_region hr on hd.region=hr.id,\n (SELECT @row1:=0) AS row_count\n union\n select (@row1:=@row1+1) as row,co.id,co.name,co.district from hippo_county co\n inner join hippo_district hd on co.district=hd.id,\n (SELECT @row1:=0) AS row_count\n union\n select (@row:=@row+1) as row,hf.id,hf.name,hf.location from hippo_facility hf,\n (SELECT @row:=0) AS row_count\n where location is not null\n )asd1\n on asd.parent=asd1.id\n where asd.last_modified > '{$lastreporttime}' "; //query to get data elements $deQuery = "select hj.id,hj.title as name from hippo_job hj\n where hj.last_modified > '{$lastreporttime}' "; $db = MDB2::singleton(); $orgRes = $db->query($orgQuery); $deRes = $db->query($deQuery); $totalOrgUnitSend = 0; $totalDESend = 0; $totalOrgUnitReached = 0; $totalDEReached = 0; while ($data = $orgRes->fetchRow()) { $orgId = 'id'; $orgName = 'name'; $orgParent = 'parent'; $orgIdArr[] = $data->{$orgId}; $orgNameArr[] = $data->{$orgName}; $orgIdParentArr[] = $data->{$orgParent}; $orgIdToSend = $data->{$orgId}; $orgNameToSend = $data->{$orgName}; $orgParentToSend = $data->{$orgParent}; $mdTypeToSend = 'OU'; $deCodeToSend = ''; $deNameToSend = ''; //curl to send org structure Sagar start //Information from page $url = false; if (!I2CE::getConfig()->setIfIsSet($url, "/modules/DHIS-Dashboard/urls/meta_sync") || !$url) { I2CE::raiseError("Bad url for meta sync"); return false; } $curl_parameters = array('orgUnitCode' => $orgIdToSend, 'orgUnitName' => $orgNameToSend, 'parentOrgUnitCode' => $orgParentToSend, 'metaDataType' => $mdTypeToSend, 'dataElementCode' => $deCodeToSend, 'dataElementName' => $deNameToSend); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}"); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curl_parameters)); $content = curl_exec($ch); $info = curl_getinfo($ch); $totalOrgUnitSend = $totalOrgUnitSend + 1; if ($content === false || $info['http_code'] != 200) { $output = "\nNo cURL data returned for {$url} [" . $info['http_code'] . "]"; if (curl_error($ch)) { $output .= "\n" . curl_error($ch); } } else { $totalOrgUnitReached = $totalOrgUnitReached + 1; } curl_close($ch); } while ($data = $deRes->fetchRow()) { $deCode = 'id'; $deName = 'name'; $deIdArr[] = $data->{$deCode}; $deNameArr[] = $data->{$deName}; $orgIdToSend = ''; $orgNameToSend = ''; $orgParentToSend = ''; $mdTypeToSend = 'DE'; $deCodeToSend = $data->{$deCode}; $deNameToSend = $data->{$deName}; $rest1 = substr($deCodeToSend, 0, 3); $rest = substr($deCodeToSend, 4, 8); $deCodeToSend = $rest1 . $rest; $url = false; if (!I2CE::getConfig()->setIfIsSet($url, "/modules/DHIS-Dashboard/urls/meta_sync") || !$url) { I2CE::raiseError("Bad url for meta sync"); return false; } $curl_parameters = array('orgUnitCode' => $orgIdToSend, 'orgUnitName' => $orgNameToSend, 'parentOrgUnitCode' => $orgParentToSend, 'metaDataType' => $mdTypeToSend, 'dataElementCode' => $deCodeToSend, 'dataElementName' => $deNameToSend); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}"); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curl_parameters)); $content = curl_exec($ch); $info = curl_getinfo($ch); // print_r($ch); $totalDESend = $totalDESend + 1; if ($content === false || $info['http_code'] != 200) { $output = "\nNo cURL data returned for {$url} [" . $info['http_code'] . "]"; // echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); if (curl_error($ch)) { $output .= "\n" . curl_error($ch); } // echo $output; } else { $totalDEReached = $totalDEReached + 1; } curl_close($ch); //curl to send org structure Sagar end } //store magic data in the I2CE::getConfig()->traverse("/modules/DHIS-Dashboard/Time/SendTime", true, false)->setValue(date('y-m-d h:m:s')); // Get period_type start $period_type = $this->request('Period'); // echo $period_type; // Get period_type end // Get report start $report_view = $this->request('ReportViews'); // Get report end switch ($period_type) { case 'Q': $transform = 'dhis_quarterly'; break; case 'Y': $transform = 'dhis_yearly'; break; case 'MA': $transform = 'dhis_all_month'; break; default: case 'M': $transform = 'dhis_monthly'; break; } $xml_export = new I2CE_CustomReport_Display_Export($this, $report_view); $xml_export->setStyle('xml'); $xml_export->setTransform($transform); if (!($exported_xml = $xml_export->generateExport())) { I2CE::raiseError("Could not generate export report"); return false; } $exported_xml = str_replace("b|", "b", $exported_xml); $file_path = tempnam(sys_get_temp_dir(), 'DHIS_DASH_') . 'xml'; $fp = fopen($file_path, "w"); fwrite($fp, $exported_xml); fclose($fp); $url = false; if (!I2CE::getConfig()->setIfIsSet($url, "/modules/DHIS-Dashboard/urls/value_sets") || !$url) { I2CE::raiseError("Bad url for value sets"); return false; } //Code to send data to DHIS $output = exec("curl -d @{$file_path} {$url} -H 'Content-Type:application/xml' -u {$username}:{$password} -v"); try { $xml = new SimpleXMLElement($output); $status = $xml->dataValueCount; $shortStatus = ' Data Status-- ' . ' :Imported-' . $status['imported'] . ' :Updated-' . $status['updated'] . ' :Ignored-' . $status['ignored'] . ' '; $this->template->setDisplayData("importedStatus", $shortStatus); } catch (Exception $ex) { $this->template->setDisplayData("importedStatus", $ex); } unlink($file_path); return true; }