Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20130703 after a long time #2

Open
wants to merge 9 commits into
base: openduka-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Raw/*

# exclude everything except directory application
*.[oa]
*.zip
*~
*
!application
Expand All @@ -18,5 +19,6 @@ application/*
!application/models
!application/controllers

!assets/*
application/config/database.php
application/config/config.php
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OpenDuka

Open Duka is a project designed by the [Open Institute](http://www.openinstitute.com/) that will provide a freely accessible database of information on Kenyan entities.

This information will provide citizens, journalists, and civic activists with a practical and easy-to-use tool to understand the ownership structure of the world they live in, demonstrating the practical applications of open information for normal citizens. It will serve as a core dataset for citizens, journalists, and civic activists who want to build 3rd party public transparency and public accountability apps or services, by allowing them to cross reference the Open Duka company shareholder data against other datasets.

The project aims to scrape data from various sources that range from shareholder information, procurement information, legal cases and company information.

The current pilot is being launched in Kenya in partnership with the National Council of Law Reporting and funded by A.T.T.I.

This will be expanded to other sources of corporate data and the site will incorporate visualizations that will be able to show linkages of the data.


# Installation

* Install CodeIgnighter. Download it at https://github.com/EllisLab/CodeIgniter and follow installation documentation.

* Download OpenDuka files to the CodIgniter instance folder.

* Edit the config.php in the application/config folder to reflect the web instance you are running.

* Upload and reconstruct the database from the openduka_alpha.sql file.

* Edit the database.php in the application/config folder to reflect the database instance you are running.


# License

Open Duka by Open Institute is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
160 changes: 104 additions & 56 deletions application/controllers/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ function ListTable(){
}



function ListField(){
//$this->output->enable_profiler(TRUE);
$stabs = $this->input->post('STab');
Expand Down Expand Up @@ -339,7 +340,7 @@ function ListFieldEdit(){
$representation = explode(",",(str_replace(" ","",(str_replace("`","",$doc_info[0]['representation'])))));
//echo($stabs);
//var_dump($doc_info); exit;
$list = "<form id='DatasetEditForm' action='' method='post'>";
$list = "<form id='DatasetEntityEditForm' action='' method='post'>";
//$list .="<div class='spacer'>Document Name <input type='text' value='' name='DocName'/> {2007_PublicAwardedTenders}</div>";

$list .= "<div class='spacer'><div style='width: 300px;'>Select field to show Entity</div></div>";
Expand All @@ -349,16 +350,16 @@ function ListFieldEdit(){
$name = $kiwanja->name;
$list .= "<div class='spacer parent'>";
if (in_array($name, $representation)) {
$list .= "<input style='width: 20px;' type='checkbox' name='DataField[]' class='fild' checked value='".$kiwanja->name."'>";
$list .= "<input style='width: 20px;' type='checkbox' name='DataEntityField[]' class='fild' checked value='".$kiwanja->name."'>";
} else {
$list .= "<input style='width: 20px;' type='checkbox' name='DataField[]' class='fild' value='".$kiwanja->name."'>";
$list .= "<input style='width: 20px;' type='checkbox' name='DataEntityField[]' class='fild' value='".$kiwanja->name."'>";
}
$list .= $kiwanja->name ."</div>";
}
}

$list .= '<div class="spacer">
<input type="hidden" value="'. $stabs .'" name="tablename"/>
<input type="hidden" value="'. $stabs .'" name="Entitytablename"/>
<input type="button" class="DatasetEditBT" value="Submit" id="DatasetEditBT"/>
</div>';
$list .= '</form>';
Expand All @@ -369,29 +370,75 @@ function ListFieldEdit(){
}


function EntityExtract(){
//$this->output->enable_profiler(TRUE);

//$table_name = $this->input->post('tablename');
$DocumentType = $this->input->post('DocumentType');
//$Verb = $this->input->post('Verb');
$DocName = str_replace(' ', '_', $this->input->post('DocName'));
$viwanja = $this->input->post('Extract');
function ListFieldEntityEdit(){
//$this->output->enable_profiler(TRUE);
$stabs = $this->input->post('STab');
$doc_info = $this->admin_model->get_document_ref($stabs);
//$representation = explode(",",(str_replace(" ","",(str_replace("`","",$doc_info[0]['representation'])))));
//echo($stabs);
//var_dump($doc_info); exit;
$list = "<form id='DatasetEditForm' action='' method='post'>";
//$list .="<div class='spacer'>Document Name <input type='text' value='' name='DocName'/> {2007_PublicAwardedTenders}</div>";

$this->admin_model->table_name_change('NewTable', $DocName);
$list .= "<div class='spacer'><div style='width: 300px;'>Select at least 2 fields to extract Entity from. Please note that the content in the field will be taken as array having a comma as a delimeter.</div></div>";
$viwanja = $this->admin_model->get_fields($stabs);

$DocID = $this->admin_model->get_document_entry($DocName) ? : $this->admin_model->insert_document($DocName, $DocumentType);
foreach ($viwanja as $kiwanja) {$name[] = $kiwanja->name;}

//var_dump($name);
foreach ($viwanja as $kiwanja) {
if (substr($kiwanja->name, -3) != '_E_') {
$n = $kiwanja->name . '_E_';
$list .= "<div class='spacer parent'>";
if (in_array($n, $name)) {
$list .= "<input style='width: 20px;' type='checkbox' name='Extract[]' class='fild' checked value='".$kiwanja->name."'>";
} else {
$list .= "<input style='width: 20px;' type='checkbox' name='Extract[]' class='fild' value='".$kiwanja->name."'>";
}
$list .= $kiwanja->name ."</div>";
}
}

//var_dump($viwanja);
//var_dump($Verb);
$list .= '<div class="spacer">
<input type="hidden" value="'.$doc_info[0]['DocTypeID'].'" name="DocumentType"/>
<input type="hidden" value="'. $stabs .'" name="tablename"/>
<input type="button" class="DatasetEditBT" value="Submit" id="DatasetEditBT"/>
</div>';
$list .= '</form>';

$list = empty($list) ? "Sorry No Data" : $list;

echo $list;
}


function EntityExtract(){
//$this->output->enable_profiler(TRUE);

$table_name = $this->input->post('tablename');
$DocumentType = $this->input->post('DocumentType');
//$Verb = $this->input->post('Verb');
$DocName = (strlen(trim($this->input->post('DocName'))) >= 4) ? str_replace(' ', '_', $this->input->post('DocName')) : '';
$viwanja = $this->input->post('Extract');

if (strlen($DocName) >= 4) { $this->admin_model->table_name_change('NewTable', $DocName); }
else { $DocName = $table_name; }

$DocID = $this->admin_model->get_document_entry($DocName) ? : $this->admin_model->insert_document($DocName, $DocumentType);
$DocDetails = $this->admin_model->get_document_details($DocID);
$CountryID = $DocDetails[0]['CountryID'];

//var_dump($viwanja); exit;
//var_dump($Verb);
//echo $DocumentType; exit;

$list ="Records Submitted ";
$l=0;
for($i=0; $i<sizeof($viwanja); $i++){

//echo $viwanja[$i]; exit;
$this->admin_model->fieldcheck($viwanja[$i], $DocName);

$l += $this->admin_model->extract_entity($viwanja[$i], $DocName, $DocID, $this->session->userdata('user_id'), $DocumentType);
$l += $this->admin_model->extract_entity($viwanja[$i], $DocName, $DocID, $this->session->userdata('user_id'), $DocumentType, $CountryID);

}

Expand All @@ -404,12 +451,12 @@ function EntityExtract(){
function DatasetEdit(){
// $this->output->enable_profiler(TRUE);

$tbl = $this->input->post('tablename');
$DataField = $this->input->post('DataField');
// echo $tbl; exit;
$tbl = $this->input->post('Entitytablename');
$DataField = $this->input->post('DataEntityField');

$flds = (count($DataField) > 0) ? "'".implode(',', $DataField) ."'" : '*';
$list ="Records Submitted ";

//echo $flds; exit;
$this->admin_model->dataset_edit($tbl, $flds);

}
Expand Down Expand Up @@ -439,9 +486,9 @@ function DatasetAdd(){
*/
$fileElementName = 'fileToUpload';
//$msg .=$_FILES[$fileElementName]['error'];

if(!empty($_FILES[$fileElementName]['error']))
{
//echo $_FILES[$fileElementName]['error']; exit;
if(!empty($_FILES[$fileElementName]['error'])){
switch($_FILES[$fileElementName]['error'])
{

Expand Down Expand Up @@ -472,6 +519,7 @@ function DatasetAdd(){
$msg = '';//'No error code avaiable';
}
}

//$msg .= $_FILES[$fileElementName]['tmp_name'];
if(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
{
Expand Down Expand Up @@ -538,45 +586,45 @@ function DatasetAdd(){

//var_dump(explode($lineseparator,$csvcontent));

foreach(explode($lineseparator,$csvcontent) as $line) {
foreach(explode($lineseparator,$csvcontent) as $line) {

$lines++;
$skipped=0;
if($lines>1){
$line = trim($line," \t");
$lines++;
$skipped=0;
if($lines>1){
$line = trim($line," \t");

$line = str_replace("\r","",$line);
$line = str_replace("\r","",$line);

/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/

$linearray = explode($fieldseparator,$line);
$linearray = str_replace(",", "\,", $linearray);
$linearray = str_replace("'\," ,"'," ,$linearray);
//$linearray = preg_replace( "#[^a-zA-Z0-9,.]#", "", $linearray);
$linemysql = implode("','",$linearray);
if (strlen($linemysql)>=1){
if($addauto){
$query = "insert into $TblName values('','$linemysql');";
}
else
{
$query = "insert into $TblName values('$linemysql');";
$linearray = explode($fieldseparator,$line);
$linearray = str_replace(",", "\,", $linearray);
$linearray = str_replace("'\," ,"'," ,$linearray);
//$linearray = preg_replace( "#[^a-zA-Z0-9,.]#", "", $linearray);
$linemysql = implode("','",$linearray);
if (strlen($linemysql)>=1){
if($addauto){
$query = "insert into $TblName values('','$linemysql');";
}
else
{
$query = "insert into $TblName values('$linemysql');";
}
}
}

//$queries .= $query . "\n";
//$queries .= $query . "\n";

//echo $queries; exit;
$insert = $this->admin_model->populate_table($query);
//echo $queries; exit;
$insert = $this->admin_model->populate_table($query);

if(!$insert){ $skipped++; $msg .= " $skipped were not inserted";}
if(!$insert){ $skipped++; $msg .= " $skipped were not inserted";}

}
}
}
}

//$q = "insert into DocUploaded (title, doc_id, DocTypeID,data_table) values('$TblName', '".date('Ymd')."-$TblName',$DocumentType,'$TblName');";
//$this->admin_model->populate_table($q);
Expand All @@ -585,7 +633,7 @@ function DatasetAdd(){

// var_dump($_FILES['fileToUpload']['tmp_name']);
@unlink($_FILES[$fileElementName]);
}
}
}


Expand Down
9 changes: 7 additions & 2 deletions application/controllers/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function search(){
}
}
public function entity(){
//$this->output->enable_profiler(true);
$this->load->model('api_m');
if((!isset($_GET['key']))||(!isset($_GET['id']))){
$result = array("error"=>"missing key and or entity id");
Expand All @@ -87,8 +88,10 @@ public function entity(){
if($this->api_m->valid_key($_GET['key']))
{
$doc = array();
// echo $_GET['id']; exit;
$result = $this->api_m->get_entries('ID',$_GET['id']);
$docmap = explode(',',$result[0]['DocID']);
//var_dump($docmap); exit;
foreach($docmap as $k => $d){

if(!in_array($d, $doc)){
Expand All @@ -97,22 +100,24 @@ public function entity(){
}

$maps= '{"data":[';
//var_dump($root_node);
//var_dump($doc);
if(sizeof($doc)>0){
for($i=0; $i<sizeof($doc); $i++){
$doc_ref = $doc[$i];
if (isset($doc_ref)){
$dataset = $this->api_m->get_doc($doc_ref);
//var_dump($dataset);

$maps.= '{"dataset_type":[{"'. $dataset[0]['DocTypeName'] .'": [{"dataset":';
$dt=$dataset[0]['data_table'];
$dtID=$dataset[0]['DocTypeID'];
if($dt!=""){

$d=1;
$ds=$dataset[0]['representation'];
$q = ($ds=="")? '*' : $ds;

$dta = $this->api_m->get_dataset($dt,$q,$_GET['id']);
var_dump($dta);
$maps.= json_encode($dta);

}
Expand Down
Loading