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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?php
/**
* IsisImporterEntities: provides ISIS import methods for importing data
* into model entities.
*/
class sfIsisImporterEntities {
/**
* Constructor.
*/
public function __construct($isis) {
$this->isis = $isis;
}
/**
* Get the entity name from a subfield.
*
* @param string $subfield Subfield name
* @return string Genre name
*/
static function entityName($subfield)
{
return ucfirst($subfield);
}
/**
* Import a single field into a model.
*
* @param object $model Model
* @param array $field Field data from ISIS database schema
* @param int $row Row number
*/
public function addMain(&$model, $field, $row = 0)
{
$value = $this->isis->filterBrackets($this->isis->getMainItem($field, $row));
if ($value != null)
{
$map = $this->isis->getMap($field);
$model->{$map}($value);
}
}
/**
* Import a single subfield into a model.
*
* @param object $model Model
* @param array $field Field data from ISIS database schema
* @param string $subfield Subfield name
* @param int $row Row number
*/
public function addSubfield(&$model, $field, $subfield, $row = 0)
{
$value = $this->isis->filterBrackets($this->isis->getSubfield($field, $subfield, $row));
if ($value != null)
{
$map = $this->isis->getMap($field, $subfield);
$model->{$map}($value);
}
}
/**
* Import all subfields from a field row into a model.
*
* @param object $model Model
* @param array $field Field data from ISIS database schema
* @param int $row Row number
*/
public function addSubfields(&$model, $field, $row = 0)
{
foreach ($this->isis->getSubfieldList($field) as $subfield)
{
$this->addSubfield($model, $field, $subfield, $row);
}
}
/**
* Import single values into the model.
*
* Currently undefined mappings for a field/subfield
* are not saved as we would need to make sure a corresponding
* field exists in the model. This prevents the map
* configurations like $map = array('type' => 'value'); to work.
*
* As we are importing single values, here we don't care with
* row numbers as we assume that just the first row should be
* imported.
*
* @param object $model Model object
* @param array $field Field data
*/
public function importValues(&$model, array $field)
{
if ($this->isis->fieldHasMap($field))
{
$this->addMain($model, $field);
}
foreach ($this->isis->getSubfieldList($field) as $subfield)
{
if ($this->isis->subfieldHasMap($field, $subfield))
{
$this->addSubfield($model, $field, $subfield);
}
}
}
/**
* Get an existing entity.
*
* @param string $entity Entity name
* @param string $value Value to search for
* @param string $by Field to search $value
* @return mixed Entity data or false
*/
public function getEntity($entity, $value, $by = 'name')
{
$findby = 'findOneBy'. ucfirst($by);
$data = Doctrine_Core::getTable($entity)->{$findby}($value);
if ($data)
{
return $data;
}
return false;
}
/**
* Add a new entity into the database if needed, returning
* the corresponding object.
*
* @param string $entity Entity name
* @param string $name Name value
* @param string $key Value key
* @return object Entity data
*/
public function addEntity($entity, $name, $key = 'name')
{
$name = $this->entityName($name);
$data = $this->getEntity($entity, $name, $key);
if (!$data)
{
$this->log("Adding new $entity $name.");
$data = $this->newEntity($entity, $name, $key);
}
return $data;
}
/**
* Add a new entity into the database.
*
* @param string $entity Entity name
* @param string $name Name value
* @param string $key Value key
* @return object Entity data
*/
public function newEntity($entity, $name, $key = 'name')
{
$data = new $entity();
$data->{$key} = $name;
$data->save();
return $data;
}
/**
* Add an element into the database if needed, returning
* the resulting object.
*
* @param string $entity Entity name
* @param string $value Entity value
* @return object Entity data
*/
public function newOrExisting($entity, $value, $by = 'Name')
{
// Check for a null value.
if ($value == null)
{
$this->log("Null element value for $entity.", 'debug');
return;
}
// Determine parse method.
if (method_exists($this, 'parse'. $entity .'Name'))
{
$method = 'parse'. $entity .'Name';
}
else
{
$method = 'parseName';
}
// Get existing element.
$name = ($by == 'Name') ? $this->{$method}($value) : $value;
$element = call_user_func(array($entity, 'getBy'. $by), $name);
// Create new element if needed.
if (!$element)
{
$this->log("Adding new $entity $value.");
$element = call_user_func(array($entity, 'addBy'. $by), $name);
}
return $element;
}
}
|