-
Notifications
You must be signed in to change notification settings - Fork 243
/
ezimagefile.php
205 lines (184 loc) · 8.45 KB
/
ezimagefile.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
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
<?php
/**
* File containing the eZImageFile class.
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package kernel
*/
/*!
\class eZImageFile ezimagefile.php
\ingroup eZDatatype
\brief The class eZImageFile handles registered images
*/
class eZImageFile extends eZPersistentObject
{
function eZImageFile( $row )
{
$this->eZPersistentObject( $row );
}
static function definition()
{
static $definition = array( 'fields' => array( 'id' => array( 'name' => 'id',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'contentobject_attribute_id' => array( 'name' => 'ContentObjectAttributeID',
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZContentObjectAttribute',
'foreign_attribute' => 'id',
'multiplicity' => '1..*' ),
'filepath' => array( 'name' => 'Filepath',
'datatype' => 'string',
'default' => '',
'required' => true ) ),
'keys' => array( 'id' ),
'class_name' => 'eZImageFile',
'name' => 'ezimagefile' );
return $definition;
}
static function create( $contentObjectAttributeID, $filepath )
{
$row = array( "contentobject_attribute_id" => $contentObjectAttributeID,
"filepath" => $filepath );
return new eZImageFile( $row );
}
static function fetchForContentObjectAttribute( $contentObjectAttributeID, $asObject = false )
{
$rows = eZPersistentObject::fetchObjectList( eZImageFile::definition(),
null,
array( "contentobject_attribute_id" => $contentObjectAttributeID ),
null,
null,
$asObject );
if ( !$asObject )
{
$files = array();
foreach ( $rows as $row )
{
$files[] = $row['filepath'];
}
return array_unique( $files );
}
else
return $rows;
}
/**
* Looks up ezcontentobjectattribute entries matching an image filepath and
* a contentobjectattribute ID
*
* @param string $filePath file path to look up as URL in the XML string
* @param int $contentObjectAttributeID
*
* @return array An array of content object attribute ids and versions of
* image files where the url is referenced
*
* @todo Rewrite ! A where data_text LIKE '%xxx%' is a resource hog !
*/
static function fetchImageAttributesByFilepath( $filepath, $contentObjectAttributeID )
{
$db = eZDB::instance();
$contentObjectAttributeID = (int) $contentObjectAttributeID;
$cond = array( 'id' => $contentObjectAttributeID );
$fields = array( 'contentobject_id', 'contentclassattribute_id' );
$limit = array( 'offset' => 0, 'length' => 1 );
$rows = eZPersistentObject::fetchObjectList( eZContentObjectAttribute::definition(),
$fields,
$cond,
null,
$limit,
false );
if ( count( $rows ) != 1 )
return array();
$contentObjectID = (int)( $rows[0]['contentobject_id'] );
$contentClassAttributeID = (int)( $rows[0]['contentclassattribute_id'] );
// Transform ", &, < and > to entities since they are being transformed in entities by DOM
// See eZImageAliasHandler::initialize()
// Ref https://jira.ez.no/browse/EZP-20090
$filepath = $db->escapeString(
htmlspecialchars(
$filepath,
// Forcing default flags to be able to specify encoding. See http://php.net/htmlspecialchars
version_compare( PHP_VERSION, '5.4.0', '>=' ) ? ENT_COMPAT | ENT_HTML401 : ENT_COMPAT,
'UTF-8'
)
);
// Escape _ in like to avoid it to act as a wildcard !
$filepath = addcslashes( $filepath, "_" );
$query = "SELECT id, version
FROM ezcontentobject_attribute
WHERE contentobject_id = $contentObjectID AND
contentclassattribute_id = $contentClassAttributeID AND
data_text LIKE '%url=\"$filepath\"%'";
if ( $db->databaseName() == 'oracle' )
{
$query .= " ESCAPE '\'";
}
$rows = $db->arrayQuery( $query );
return $rows;
}
static function fetchByFilepath( $contentObjectAttributeID, $filepath, $asObject = true )
{
// Fetch by file path without $contentObjectAttributeID
if ( $contentObjectAttributeID === false )
return eZPersistentObject::fetchObject( eZImageFile::definition(),
null,
array( 'filepath' => $filepath ),
$asObject );
return eZPersistentObject::fetchObject( eZImageFile::definition(),
null,
array( 'contentobject_attribute_id' => $contentObjectAttributeID,
'filepath' => $filepath ),
$asObject );
}
static function moveFilepath( $contentObjectAttributeID, $oldFilepath, $newFilepath )
{
$db = eZDB::instance();
$db->begin();
eZImageFile::removeFilepath( $contentObjectAttributeID, $oldFilepath );
$result = eZImageFile::appendFilepath( $contentObjectAttributeID, $newFilepath );
$db->commit();
return $result;
}
static function appendFilepath( $contentObjectAttributeID, $filepath, $ignoreUnique = false )
{
if ( empty( $filepath ) )
return false;
if ( !$ignoreUnique )
{
// Fetch ezimagefile objects having the $filepath
$imageFiles = eZImageFile::fetchByFilepath( false, $filepath, false );
// Checking If the filePath already exists in ezimagefile table
if ( isset( $imageFiles[ 'contentobject_attribute_id' ] ) )
return false;
}
$fileObject = eZImageFile::fetchByFilepath( $contentObjectAttributeID, $filepath );
if ( $fileObject )
return false;
$fileObject = eZImageFile::create( $contentObjectAttributeID, $filepath );
$fileObject->store();
return true;
}
static function removeFilepath( $contentObjectAttributeID, $filepath )
{
if ( empty( $filepath ) )
return false;
$fileObject = eZImageFile::fetchByFilepath( $contentObjectAttributeID, $filepath );
if ( !$fileObject )
return false;
$fileObject->remove();
return true;
}
static function removeForContentObjectAttribute( $contentObjectAttributeID )
{
eZPersistentObject::removeObject( eZImageFile::definition(), array( 'contentobject_attribute_id' => $contentObjectAttributeID ) );
}
/// \privatesection
public $ID;
public $ContentObjectAttributeID;
public $Filepath;
}
?>