Example #1
0
function sys_csv_2_xml($file)
{
    $rows = sys_parse_csv($file);
    $out = "<assets>";
    if (count($rows) > 1) {
        $keys = array_shift($rows);
        foreach ($rows as $row) {
            $out .= "<asset>\n";
            foreach ($row as $key => $val) {
                if (!empty($keys[$key])) {
                    $out .= "<" . $keys[$key] . ">" . q($val) . "</" . $keys[$key] . ">\n";
                }
            }
            $out .= "</asset>\n";
        }
    }
    return $out . "</assets>";
}
 static function select($path, $fields, $where, $order, $limit, $vars, $mfolder)
 {
     $path = rtrim($path, "/");
     $datas = sys_parse_csv($path);
     $rows = array();
     $index = array_shift($datas);
     $i = 0;
     $map = array("subject" => "Subject", "description" => "Description", "location" => "Location", "category" => "Categories", "organizer" => "Meeting Organizer");
     foreach ($datas as $data) {
         $i++;
         $row = array();
         foreach ($fields as $field) {
             switch ($field) {
                 case "id":
                     $row[$field] = $path . "/?" . $i;
                     break;
                 case "folder":
                     $row[$field] = $path;
                     break;
                 case "created":
                     $row[$field] = 0;
                     break;
                 case "createdby":
                     $row[$field] = "";
                     break;
                 case "lastmodified":
                     $row[$field] = 0;
                     break;
                 case "lastmodifiedby":
                     $row[$field] = "";
                     break;
                 case "searchcontent":
                     $row[$field] = implode(" ", $data);
                     break;
                 case "begin":
                     $key1 = array_search("Start Date", $index);
                     $key2 = array_search("Start Time", $index);
                     $row[$field] = modify::datetime_to_int($data[$key1] . " " . $data[$key2]);
                     break;
                 case "ending":
                     $key1 = array_search("End Date", $index);
                     $key2 = array_search("End Time", $index);
                     $row[$field] = modify::datetime_to_int($data[$key1] . " " . $data[$key2]);
                     $row["until"] = $row[$field];
                     break;
                 case "duration":
                     $row[$field] = $row["ending"] - $row["begin"];
                     break;
                 case "priority":
                     $key = array_search("Priority", $index);
                     $row[$field] = strtolower($data[$key]);
                     break;
                 case "allday":
                     $key = array_search("All day event", $index);
                     $row[$field] = $data[$key] == "False" ? 0 : 1;
                     break;
                 case "participants_ext":
                     $key = array_search("Required Attendees", $index);
                     $row[$field] = $data[$key];
                     $key = array_search("Optional Attendees", $index);
                     if ($row[$field] != "" and $data[$key] != "") {
                         $row[$field] .= ", ";
                     }
                     $row[$field] .= $data[$key];
                     break;
                 default:
                     $row[$field] = "";
                     if (!isset($row[$field]) and in_array($field, $index)) {
                         $key = array_search($field, $index);
                         $row[$field] = $data[$key];
                     } else {
                         if (isset($map[$field])) {
                             $key = array_search($map[$field], $index);
                             $row[$field] = $data[$key];
                         }
                     }
                     break;
             }
         }
         if (sys_select_where($row, $where, $vars)) {
             $rows[] = $row;
         }
     }
     $rows = sys_select($rows, $order, $limit, $fields);
     return $rows;
 }
 static function select($path, $fields, $where, $order, $limit, $vars, $mfolder)
 {
     $path = rtrim($path, "/");
     $csv_data = sys_parse_csv($path);
     $rows = array();
     $index = array_shift($csv_data);
     $i = 0;
     $ids = array();
     $datas = array();
     foreach ($csv_data as $row) {
         $n_row = array();
         foreach ($row as $key => $val) {
             if (!empty($index[$key])) {
                 $key = strtolower($index[$key]);
             }
             $n_row[$key] = $val;
         }
         $datas[] = $n_row;
     }
     /*  
     "Middle Name","Suffix","Business Street 2","Business Street 3",
     "Home Street","Home Street 2","Home Street 3","Home City","Home State","Home Postal Code",
     "Home Country/Region","Other Street","Other Street 2","Other Street 3","Other City",
     "Other State","Other Postal Code","Other Country/Region",
     "Assistant's Phone","Business Phone 2","Callback","Car Phone",
     "Company Main Phone","Home Fax","Home Phone 2","ISDN",
     "Other Fax","Other Phone","Primary Phone","Radio Phone","TTY/TDD Phone","Telex",
     "Account","Anniversary","Assistant's Name","Billing Information",
     "Business Address PO Box","Categories","Children","Directory Server"
     "E-mail Type","E-mail Display Name","E-mail 2 Address","E-mail 2 Type",
     "E-mail 2 Display Name","E-mail 3 Address","E-mail 3 Type","E-mail 3 Display Name",
     "Gender","Government ID Number","Hobby","Home Address PO Box","Initials",
     "Internet Free Busy","Keywords","Language","Location","Manager's Name","Mileage",
     "Office Location","Organizational ID Number","Other Address PO Box","Priority",
     "Private","Profession","Referred By","Sensitivity","Spouse","Web Page"  
     */
     $mapping = array("e-mail address" => "email", "company" => "company", "first name" => "firstname", "last name" => "lastname", "title" => "title", "mobile phone" => "mobile", "pager" => "pager", "business phone" => "phone", "business fax" => "fax", "notes" => "description", "job title" => "position", "e-mail 2" => "emailprivate", "home phone" => "phoneprivate", "business address" => "street", "business street" => "street", "business city" => "city", "business postal code" => "zipcode", "business state" => "state", "business country/region" => "country", "department" => "department", "birthday" => "birthday");
     foreach ($datas as $data) {
         $i++;
         $row = array();
         foreach ($fields as $field) {
             $row[$field] = "";
             switch ($field) {
                 case "id":
                     $row[$field] = $path . "/?" . $i;
                     break;
                 case "folder":
                     $row[$field] = $path;
                     break;
                 case "created":
                     $row[$field] = 0;
                     break;
                 case "lastmodified":
                     $row[$field] = 0;
                     break;
                 case "lastmodifiedby":
                     $row[$field] = "";
                     break;
                 case "searchcontent":
                     $row[$field] = implode(" ", $data);
                     break;
                 case "contactid":
                     if (empty($data["name"])) {
                         if (!empty($data["last name"])) {
                             $row[$field] = $data["last name"];
                         }
                         if (!empty($data["first name"])) {
                             $row[$field] .= " " . $data["first name"];
                         }
                         if ($row[$field] == "" and !empty($data["e-mail address"])) {
                             $row[$field] = $data["e-mail address"];
                         }
                     } else {
                         $row[$field] = $data["name"];
                     }
                     $row[$field] = str_replace(array(" ", ".", ",", "@", "\"", "'"), array("_", "_", "", "_", "", ""), $row[$field]);
                     $row[$field] = substr(trim($row[$field], " _-."), 0, 15);
                     while (isset($ids[$row[$field]])) {
                         $row[$field] .= "_2";
                     }
                     $ids[$row[$field]] = "";
                     break;
                 case "lastname":
                     if (!empty($data["last name"])) {
                         $row[$field] = $data["last name"];
                     }
                     if ($row[$field] == "" and !empty($data["name"])) {
                         if ($pos = strpos($data["name"], " ")) {
                             $row[$field] = substr($data["name"], $pos + 1);
                         } else {
                             $row[$field] = $data["name"];
                         }
                     }
                     if ($row[$field] == "" and !empty($data["e-mail address"])) {
                         preg_match("/[-._]?([^-._@]+)@/i", $data["e-mail address"], $match);
                         if (!empty($match[1])) {
                             $row[$field] = ucfirst(strtolower($match[1]));
                         }
                     }
                     $row[$field] = trim($row[$field], " ,");
                     break;
                 case "firstname":
                     if (!empty($data["first name"])) {
                         $row[$field] = $data["first name"];
                     }
                     if ($row[$field] == "" and !empty($data["name"]) and $pos = strpos($data["name"], " ")) {
                         $row[$field] = substr($data["name"], 0, $pos);
                     }
                     if ($row[$field] == "" and !empty($data["e-mail address"])) {
                         preg_match("/([^-._@]+)[-._][^-._@]*@/i", $data["e-mail address"], $match);
                         if (!empty($match[1])) {
                             $row[$field] = ucfirst(strtolower($match[1]));
                         }
                     }
                     $row[$field] = trim($row[$field], " ,");
                     break;
                 default:
                     if ($field_key = array_search($field, $mapping) and !empty($data[$field_key])) {
                         $row[$field] = str_replace(array("\"", "'"), "", $data[$field_key]);
                     }
                     if ($field == "birthday" and $row[$field] != "0/0/00") {
                         $row[$field] = modify::datetime_to_int($row[$field]);
                     }
                     break;
             }
         }
         if (sys_select_where($row, $where, $vars)) {
             $rows[] = $row;
         }
     }
     $rows = sys_select($rows, $order, $limit, $fields);
     return $rows;
 }