Commit ce62b1a4747b5ad7e34aad8821d0223ca385074e

Authored by Edmar Moretti
1 parent f6487d78

Criado o diretório pacotes/classesphp para armazenar arquivos PHP de terceiros e…

… de uso geral. Incluída classe para manipulação de arquivos CSV.
Showing 1 changed file with 231 additions and 0 deletions   Show diff stats
pacotes/classesphp/class.CSVHandler.php 0 → 100644
... ... @@ -0,0 +1,231 @@
  1 +<?php
  2 +# This program is free software; you can redistribute it and/or modify
  3 +# it under the terms of the GNU Library General Public License as published by
  4 +# the Free Software Foundation either version 2 of the License, or
  5 +
  6 +//=====================================================================================================================
  7 +//classdef CSVHandler 0.91 :: CSV Handling Wrapper
  8 +//=====================================================================================================================
  9 +#
  10 +#
  11 +# Copyright (C) 2005 by Andreas Müller
  12 +
  13 +class CSVHandler {
  14 + var $Separator; //
  15 + var $DataFile;
  16 + var $DataKey;
  17 + var $HeaderData; //
  18 + var $ItemsList; //
  19 + var $Items_Count;
  20 + var $color;
  21 + var $RecordsList;
  22 +
  23 +// Standard User functions
  24 + function CSVHandler($Filename, $Separator, $KeyFieldName) { //Constructor
  25 + $this->DataFile=$Filename;
  26 + $this->DataKey=$KeyFieldName;
  27 + $this->Separator=$Separator;
  28 + $this->color="#FFFFFF";
  29 + }
  30 + function ReadCSV() { //read data into this->ItemsList and return it in an array
  31 + $this->Items_Count=0;
  32 + $this->ItemsList=array();
  33 + $Item=array();
  34 + $fp = fopen ($this->DataFile,"r");
  35 + $this->HeaderData = fgetcsv ($fp, 3000, $this->Separator);
  36 + while ($DataLine = fgetcsv ($fp, 3000, $this->Separator)) {
  37 + for($i=0;$i<count($this->HeaderData);$i++){
  38 + $Item[$this->HeaderData[$i]]=$DataLine[$i];
  39 + }
  40 + array_push($this->ItemsList,$Item);
  41 + $this->Items_Count++;
  42 + }
  43 + fclose($fp);
  44 + return ($this->ItemsList);
  45 + }
  46 + function Select($needle,$column="all") { //get items in a sort of SQL Select query and return them in an array
  47 + $this->ReadCSV();
  48 + if($needle=="*") {
  49 + $result=$this->ItemsList;
  50 + } else {
  51 + $result=array();
  52 + if($column=="all") {
  53 + while(list($key,$line)=each($this->ItemsList)) {
  54 + if (stristr(implode("",$line),$needle)) array_push($result,$line);
  55 + }
  56 + } else {
  57 + while(list($key,$line)=each($this->ItemsList)) {
  58 + if (stristr($line[$column],$needle)) array_push($result,$line);
  59 + }
  60 + }
  61 + }
  62 + return ($result);
  63 + }
  64 + function ListAll() { //prints a list of all Data
  65 + $Data=$this->ReadCSV();
  66 + reset ($this->ItemsList);
  67 + reset ($this->HeaderData);
  68 + $HHeaders="";
  69 + $HData="";
  70 + while(list($HKey,$HVal)=each($this->HeaderData)) { //Create Headers Line
  71 + $HHeaders.=$this->HTTD($HVal);
  72 + }
  73 + $HHeaders=$this->HTTR($HHeaders);
  74 + while(list($LineKey,$LineVal)=each($this->ItemsList)) { //Read Data Lines
  75 + $HDataLine="";
  76 + while(list($DataKey,$DataVal)=each($LineVal)) { //Dissect one Data Line
  77 + $HDataLine.=$this->HTTD($DataVal);
  78 + }
  79 + $HData.=$this->HTTR($HDataLine); //and add HTML to Data
  80 + }
  81 + print ($this->HTPage($this->HTTable($HHeaders.$HData)));
  82 + }
  83 + function GetValues($field) { //Fetch all values of a specified field and return values in array
  84 + $Data=$this->ReadCSV();
  85 + $values=array();
  86 + while(list($key,$val)=each($this->ItemsList)) {
  87 + if(!in_array($val[$field],$values)) array_push($values,$val[$field]);
  88 + }
  89 + sort($values);
  90 + return $values;
  91 + }
  92 + function Edit() { //All edit function in one Table
  93 + $Data=$this->ReadCSV();
  94 + if(isset($_POST['commit'])) {
  95 + $this->Update($_POST[$this->DataKey],$_POST);
  96 + $Data=$this->ReadCSV();
  97 + }
  98 + if(isset($_POST['add'])) {
  99 + $this->Add($_POST[$this->DataKey],$_POST);
  100 + $Data=$this->ReadCSV();
  101 + }
  102 + if(isset($_POST['delete'])) {
  103 + $this->Delete($_POST[$this->DataKey]);
  104 + $Data=$this->ReadCSV();
  105 + }
  106 + $PAGE=$this->EditList();
  107 + print $PAGE;
  108 + }
  109 +
  110 +// Administration Area
  111 + function Update($key,$data) { //Updating Item "key" with "data" named array
  112 + $this->ReadCSV();
  113 + for($i=0;$i<count($this->ItemsList);$i++) {
  114 + If($this->ItemsList[$i][$this->DataKey]==$key){
  115 + while(list($key,$val)=each($this->HeaderData)) {
  116 + if(isset($data[$val])) $this->ItemsList[$i][$val]=$data[$val];
  117 + }
  118 + }
  119 + }
  120 + $this->WriteData();
  121 + return($this->ItemsList);
  122 + }
  123 + function Add($key,$data) { //add an Item "key" with "data" named array
  124 + $this->ReadCSV();
  125 + $NewLine=array();
  126 + $NewItem=array($this->DataKey=>$key);
  127 + while(list($key,$val)=each($this->HeaderData)) {
  128 + if(isset($data[$val])) {
  129 + $NewItem[$val]=$data[$val];
  130 + } else {
  131 + $NewItem[$val]=$data[$val]="";
  132 + }
  133 + }
  134 + array_push($this->ItemsList,$NewItem);
  135 + $this->WriteData();
  136 + return($this->ItemsList);
  137 + }
  138 + function EditList() { //returns Editor's List of Items
  139 + reset ($this->ItemsList);
  140 + reset ($this->HeaderData);
  141 + $HHeaders=$this->HTTD(" ");
  142 + $HData="";
  143 + while(list($HKey,$HVal)=each($this->HeaderData)) { //Create Headers Line
  144 + $HHeaders.=$this->HTTD($HVal);
  145 + }
  146 + $HHeaders=$this->HTTR($HHeaders);
  147 + while(list($LineKey,$LineVal)=each($this->ItemsList)) { //Read Data Lines
  148 + $HDataLine="";
  149 + while(list($DataKey,$DataVal)=each($LineVal)) { //Dissect one Data Line
  150 + $HDataLine.=$this->HTInput($DataKey,$DataVal);
  151 + }
  152 + $HData.=$this->HTForm($LineVal[$this->DataKey],$this->HTTR($this->HTButton("commit").$HDataLine.$this->HTButton("delete"))); //and add HTML to Data
  153 + }
  154 + $HDataLine="";
  155 + reset($this->HeaderData);
  156 + while(list($DataKey,$DataVal)=each($this->HeaderData)) { // Add an extra Line for Adding a record
  157 + $HDataLine.=$this->HTInput($DataVal,"");
  158 + }
  159 + $HData.=$this->HTForm($LineVal[$this->DataKey],$this->HTTR($this->HTButton("add").$HDataLine)); //and add HTML to Data
  160 + return($this->HTPage($this->HTTable($HHeaders.$HData)));
  161 + }
  162 + function Delete($DeleteKey) { //Remove Item "Key" from the file
  163 + $inter=array();
  164 + while(list($key,$val)=each($this->ItemsList)) {
  165 + If($val[$this->DataKey]!=$DeleteKey) array_push($inter,$val);
  166 + }
  167 + $this->ItemsList=$inter;
  168 + $this->WriteData();
  169 + return($this->ItemsList);
  170 + }
  171 + function WriteData() { //writing contents of ItemList to Datafile
  172 + reset($this->ItemsList);
  173 + $Output=implode($this->Separator, $this->HeaderData)."\n";
  174 + while(list($key,$val)=each($this->ItemsList)) {
  175 + for($i=0;$i<count($this->HeaderData);$i++){
  176 + $writekey=$this->HeaderData[$i];
  177 + $writeitem[$writekey]=$val[$writekey];
  178 + }
  179 + $Output.=implode($this->Separator, $writeitem)."\n";
  180 + }
  181 + $fp = fopen ($this->DataFile,"w");
  182 + flock($fp,2);
  183 + fputs($fp,$Output);
  184 + flock($fp,3);
  185 + fclose($fp);
  186 + }
  187 +
  188 +// Accessory HTML output functions
  189 + function HTPage($value) { // Places $value into BODY of HTML Page
  190 + $result = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
  191 + $result.="<html><head><title>".$this->DataFile." Editor</title>\n";
  192 + $result.="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
  193 + $result.="<style type=\"text/css\">";
  194 + $result.="<!-- td { margin: 0px; padding: 0px; border: 1px solid #003399; } --></style></head>\n";
  195 + $result.="<body>\n".$value."</body>\n</html>";
  196 + return $result;
  197 + }
  198 + function HTForm($item,$data) { //places $data into form $item
  199 + return "<form name=\"".$item."\" method=\"post\">\n".$data."</form>\n";
  200 + }
  201 + function HTTable($value) { //places $value into TABLE
  202 + return "<table width=\"96%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n".$value."</table>\n";
  203 + }
  204 + function HTTR($value) { //places $value into TR
  205 + $this->SRotate();
  206 + return "<tr>\n".$value."</tr>\n";
  207 + }
  208 + function HTTD($value) { // places $value into TD
  209 + return "<td bgcolor=\"".$this->color."\">".$value."</td>\n";
  210 + }
  211 + function HTInput($field,$value) { //returns TD input field
  212 + $Olen=strlen($value);
  213 + if($Olen<3) {
  214 + $Ilen=12;
  215 + } else {
  216 + $Ilen=$Olen;
  217 + }
  218 + return "<td bgcolor=\"".$this->color."\"><input name=\"".$field."\" type=\"text\" id=\"".$field."\" value=\"".$value."\" size=\"".$Ilen."\"></td>\n";
  219 + }
  220 + function HTButton($value) { // returns "$value" button
  221 + return "<td><input name=\"".$value."\" type=\"submit\" id=\"".$value."\" value=\"".$value."\"></td>\n";
  222 + }
  223 + function SRotate() { //rotating colors for more readability of tables
  224 + if($this->color=="#FFFFFF") {
  225 + $this->color="#CCEEFF";
  226 + } else {
  227 + $this->color="#FFFFFF";
  228 + }
  229 + }
  230 +}
  231 +?>
... ...