forked from daniel-sc/podio-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HumanFormat.php
170 lines (156 loc) · 5.85 KB
/
HumanFormat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of HumanFormat
*
* @author SCHRED
*/
class HumanFormat {
/**
* Creates a human readable string
* @param PodioItem $item
* @return string
*/
public static function toHumanReadableString($item) {
$itemFile = '--- ' . $item->title . ' ---' . "\n";
$itemFile .= 'Item ID: ' . $item->item_id . "\n";
if ($item instanceof PodioItem) {
foreach ($item->fields as $field) {
if ($field instanceof PodioItemField) {
$itemFile .= $field->label . ': ' . HumanFormat::getFieldValue($field) . "\n";
} else {
echo "WARN non PodioItemField:";
var_dump($item);
}
}
} else {
echo "WARN non PodioItem:";
var_dump($item);
foreach ($item->fields as $field) {
$itemFile .= $field->label . ': ' . HumanFormat::getFieldValue($field) . "\n";
}
}
$itemFile .= "\n";
return $itemFile;
}
/**
* given a podio field object -> returns a human readable format of the field value (or app reference as array (app_id, item_id)
* @param type $field should be of type PodioItemField
* @return string
*/
public static function getFieldValue($field) {
$value = "";
if ($field->type == "text" || $field->type == "number" || $field->type == "progress" || $field->type == "duration" || $field->type == "state") {
if (is_string($field->values)) {
// TODO refactor
$value = $field->values;
$value = str_ireplace('</p>', '</p><br>', $value);
$value = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $value);
$value = strip_tags(br2nl($value));
$value = str_replace(' ', ' ', $value);
$value = str_replace('&', '&', $value);
} else if (is_numeric($field->values)) {
$value = $field->values;
} else {
echo "WARN expected string or number, but found: ";
var_dump($field->values);
}
return $value;
}
if ($field->type == "category") {
$selected_categories = array();
foreach ($field->values as $category) {
array_push($selected_categories, $category['text']);
}
return HumanFormat::implodeStrings(", ", $selected_categories);
}
if ($field->type == "date") {
return HumanFormat::parseDate($field->values['start']) . " - " . HumanFormat::parseDate($field->values['end']);
}
if ($field->type == "money") {
return "" . $field->values['value'] . " " . $field->values['currency'];
}
if ($field->type == "contact") {
if (is_array($field->values) || $field->values instanceof Traversable) {
foreach ($field->values as $contact) {
$value .= "\nUserid: $contact->user_id, name: $contact->name, email: " . HumanFormat::implodeStrings(', ', $contact->mail) . ", phone: " . HumanFormat::implodeStrings(", ", $contact->phone);
}
} else {
echo "WARN unexpected contact type:";
var_dump($field);
}
return $value;
}
if ($field->type == "embed") {
if (is_array($field->values) || $field->values instanceof Traversable) {
foreach ($field->values as $embed) {
//TODO implode (general function..)
$value .= "\nurl: " . $embed->original_url;
}
} else {
echo "WARN unexpected embed type:";
var_dump($field);
}
return $value;
}
if ($field->type == "app") {
if (is_array($field->values) || $field->values instanceof Traversable) {
foreach ($field->values as $app) {
//TODO implode (general function..)
$value .= "\napp: " . $app->app->name . ", item_name: " . $app->item_name . ", title: " . $app->title; //TODO more info?!
}
} else {
echo "WARN unexpected app type:";
var_dump($field);
}
return $value;
}
if ($field->type == "image") {
$images = array();
foreach ($field->values as $image) {
array_push($images, "fileid: " . $image->file_id . ", name: " . $image->name);
}
return HumanFormat::implodeStrings(" | ", $images);
}
if ($field->type == "location") {
$locations = array();
foreach ($field->values as $location) {
array_push($locations, $location);
}
return HumanFormat::implodeStrings(" | ", $locations);
}
echo "WARN unexpected type: ";
var_dump($field);
return $field->values;
}
/**
* takes care of NULL etc..
* @param type $date
*/
public static function parseDate($date) {
if (is_null($date))
return '';
if (is_string($date))
return $date;
if ($date instanceof DateTime)
return date_format($date, 'Y-m-d H:i:s');
echo "WARNING unexpected date: ";
var_dump($date);
return $date;
}
/**
*
* @param type $glue
* @param type $pieces can be NULL
* @return string
*/
public static function implodeStrings($glue, $pieces) {
if (is_null($pieces))
return '';
return implode($glue, $pieces);
}
}