Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F805099
image_tag_processor.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Feb 26, 10:57 PM
Size
9 KB
Mime Type
text/x-php
Expires
Fri, Feb 28, 10:57 PM (1 h, 47 m)
Engine
blob
Format
Raw Data
Handle
577352
Attached To
rINP In-Portal
image_tag_processor.php
View Options
<?php
class
ImageTagProcessor
extends
kDBTagProcessor
{
/**
* Prepares all image parameters as list block parameters (for easy usage)
*
* @param kDBList $object
* @param Array $block_params
* @author Alex
*/
function
PrepareListElementParams
(&
$object
,
&
$block_params
)
{
$image_url
=
$this
->
ImageSrc
(
$block_params
);
if
(!
$image_url
)
{
return
;
}
$block_params
[
'img_path'
]
=
$image_url
;
$image_dimensions
=
$this
->
ImageSize
(
$block_params
);
$block_params
[
'img_size'
]
=
$image_dimensions
?
$image_dimensions
:
' width="'
.
$block_params
[
'DefaultWidth'
].
'"'
;
$block_params
[
'alt'
]
=
htmlspecialchars
(
$object
->
GetField
(
'AltName'
));
// really used ?
}
/**
* [AGGREGATED TAGS] works as <inp2:CatalogItemPrefix_Image, ImageSize, ImageSrc ..../>
*
* @param Array $params
* @return string
*/
function
ItemImageTag
(
$params
)
{
$this
->
LoadItemImage
(
$params
);
return
$this
->
$params
[
'original_tag'
](
$params
);
}
function
LargeImageExists
(
$params
)
{
$object
=&
$this
->
getObject
(
$params
);
if
(
$object
->
GetDBField
(
'SameImages'
)
==
null
||
$object
->
GetDBField
(
'SameImages'
)
==
1
)
{
return
false
;
}
else
{
return
true
;
}
}
function
LoadItemImage
(
$params
)
{
$parent_item
=&
$this
->
Application
->
recallObject
(
$params
[
'PrefixSpecial'
]);
/* @var $parent_item kCatDBItem */
$object
=&
$this
->
Application
->
recallObject
(
$this
->
getPrefixSpecial
(),
null
,
Array
(
'skip_autoload'
=>
true
));
/* @var $object kDBItem */
$object
->
Clear
();
// if we need primary thumbnail which is preloaded with category item's list
$is_primary
=
$this
->
SelectParam
(
$params
,
'primary,Primary'
);
$image_id
=
$this
->
Application
->
GetVar
(
$this
->
Prefix
.
'_id'
);
if
(
// is primary, when primary mark set OR name & field not given
(
$is_primary
||
!(
isset
(
$params
[
'name'
])
||
isset
(
$params
[
'field'
])))
&&
// primary image is preloaded AND direct id not given
isset
(
$parent_item
->
Fields
[
'ThumbPath'
])
&&
!
$image_id
)
{
$object
->
SetDefaultValues
();
$object
->
SetDBField
(
'Url'
,
$parent_item
->
GetDBField
(
'FullUrl'
));
$object
->
SetDBFieldsFromHash
(
$parent_item
->
GetFieldValues
(),
Array
(
'SameImages'
,
'LocalThumb'
,
'ThumbPath'
,
'ThumbUrl'
,
'LocalImage'
,
'LocalPath'
));
$object
->
Loaded
=
true
;
}
else
{
// if requested image is not primary thumbnail - load it directly
$id_field
=
$this
->
Application
->
getUnitOption
(
$this
->
Prefix
,
'ForeignKey'
);
$parent_table_key
=
$this
->
Application
->
getUnitOption
(
$this
->
Prefix
,
'ParentTableKey'
);
$keys
[
$id_field
]
=
$parent_item
->
GetDBField
(
$parent_table_key
);
// which image to load?
if
(
$is_primary
)
{
// by PrimaryImage mark
$keys
[
'DefaultImg'
]
=
1
;
}
elseif
(
getArrayValue
(
$params
,
'name'
))
{
// by ImageName
$keys
[
'Name'
]
=
$params
[
'name'
];
}
elseif
(
getArrayValue
(
$params
,
'field'
))
{
// by virtual field name in main object
$field_options
=
$parent_item
->
GetFieldOptions
(
$params
[
'field'
]);
$keys
[
'Name'
]
=
isset
(
$field_options
[
'original_field'
])
?
$field_options
[
'original_field'
]
:
$params
[
'field'
];
}
elseif
(
$image_id
)
{
// by ID
$keys
[
'ImageId'
]
=
$image_id
;
}
else
{
// by PrimaryImage if no other criteria given
$keys
[
'DefaultImg'
]
=
1
;
}
$object
->
Load
(
$keys
);
if
(
isset
(
$params
[
'field'
]))
{
$image_src
=
$parent_item
->
GetDBField
(
$params
[
'field'
]);
// when image is uploaded to virtual field in main item, but not saved to db
$object
->
SetDBField
(
'ThumbPath'
,
$image_src
);
if
(!
$object
->
isLoaded
())
{
// set fields for displaing new image during main item suggestion with errors
$fields_hash
=
Array
(
'Url'
=>
''
,
'ThumbUrl'
=>
''
,
'LocalPath'
=>
''
,
'SameImages'
=>
1
,
'LocalThumb'
=>
1
,
'LocalImage'
=>
1
,
);
$object
->
SetDBFieldsFromHash
(
$fields_hash
);
$object
->
Loaded
=
true
;
}
}
}
}
function
getImageDimension
(
$type
,
$params
)
{
$ret
=
isset
(
$params
[
'Max'
.
$type
])
?
$params
[
'Max'
.
$type
]
:
false
;
if
(!
$ret
)
{
return
$ret
;
}
$parent_prefix
=
$this
->
Application
->
getUnitOption
(
$this
->
Prefix
,
'ParentPrefix'
);
if
(
$ret
==
'thumbnail'
)
{
$ret
=
$this
->
Application
->
ConfigValue
(
$parent_prefix
.
'_ThumbnailImage'
.
$type
);
}
if
(
$ret
==
'fullsize'
)
{
$ret
=
$this
->
Application
->
ConfigValue
(
$parent_prefix
.
'_FullImage'
.
$type
);
}
return
$ret
;
}
/**
* Appends "/" to beginning of image path (in case when missing)
*
* @param kDBItem $object
* @todo old in-portal doesn't append first slash, but we do => append first slash for him :)
*/
function
makeRelativePaths
(&
$object
)
{
$thumb_path
=
$object
->
GetDBField
(
'ThumbPath'
);
if
(
$thumb_path
&&
substr
(
$thumb_path
,
0
,
1
)
!=
'/'
)
{
$object
->
SetDBField
(
'ThumbPath'
,
'/'
.
$thumb_path
);
}
$local_path
=
$object
->
GetDBField
(
'LocalPath'
);
if
(
$local_path
&&
substr
(
$local_path
,
0
,
1
)
!=
'/'
)
{
$object
->
SetDBField
(
'LocalPath'
,
'/'
.
$local_path
);
}
}
function
ImageSrc
(
$params
)
{
$object
=&
$this
->
getObject
(
$params
);
$this
->
makeRelativePaths
(
$object
);
$base_url
=
rtrim
(
$this
->
Application
->
BaseURL
(),
'/'
);
if
(
$object
->
GetDBField
(
'SameImages'
)
&&
$object
->
GetDBField
(
'ThumbPath'
))
{
// we can auto-resize image, when source image available & we use same image for thumbnail & full image in admin
$max_width
=
$this
->
getImageDimension
(
'Width'
,
$params
);
$max_height
=
$this
->
getImageDimension
(
'Height'
,
$params
);
if
(
$max_width
>
0
||
$max_height
>
0
)
{
$image_helper
=&
$this
->
Application
->
recallObject
(
'ImageHelper'
);
/* @var $image_helper ImageHelper */
$src_image
=
FULL_PATH
.
$object
->
GetDBField
(
'ThumbPath'
);
return
$image_helper
->
ResizeImage
(
$src_image
,
$max_width
,
$max_height
);
}
}
$ret
=
''
;
// if we need thumbnail, or full image is same as thumbnail
$show_thumbnail
=
$this
->
SelectParam
(
$params
,
'thumbnail,Thumbnail'
)
||
// old style
(
isset
(
$params
[
'MaxWidth'
])
&&
$params
[
'MaxWidth'
]
==
'thumbnail'
)
||
// new style
(
isset
(
$params
[
'MaxHeight'
])
&&
$params
[
'MaxHeight'
]
==
'thumbnail'
);
if
(
$show_thumbnail
||
$object
->
GetDBField
(
'SameImages'
))
{
// return local image or url
$ret
=
$object
->
GetDBField
(
'LocalThumb'
)
?
$base_url
.
$object
->
GetDBField
(
'ThumbPath'
)
:
$object
->
GetDBField
(
'ThumbUrl'
);
if
(
$object
->
GetDBField
(
'LocalThumb'
)
&&
!
file_exists
(
FULL_PATH
.
$object
->
GetDBField
(
'ThumbPath'
))
&&
!
constOn
(
'DBG_IMAGE_RECOVERY'
))
{
// local thumbnail file is missing
$ret
=
''
;
}
}
else
{
// if we need full which is not the same as thumb
$ret
=
$object
->
GetDBField
(
'LocalImage'
)
?
$base_url
.
$object
->
GetDBField
(
'LocalPath'
)
:
$object
->
GetDBField
(
'Url'
);
if
(
$object
->
GetDBField
(
'LocalImage'
)
&&
!
file_exists
(
FULL_PATH
.
$object
->
GetDBField
(
'LocalPath'
)))
{
// local full image file is missing
$ret
=
''
;
}
}
$default_image
=
$this
->
SelectParam
(
$params
,
'default_image,DefaultImage'
);
return
(
$ret
&&
$ret
!=
$base_url
)
?
$ret
:
(
$default_image
?
$base_url
.
THEMES_PATH
.
'/'
.
$default_image
:
false
);
}
function
getFullPath
(
$path
)
{
if
(!
$path
)
{
return
$path
;
}
// absolute url
if
(
preg_match
(
'/^(.*):
\/\/
(.*)$/U'
,
$path
))
{
return
preg_replace
(
'/^'
.
preg_quote
(
$this
->
Application
->
BaseURL
(),
'/'
).
'(.*)/'
,
FULL_PATH
.
'/
\\
1'
,
$path
);
}
// relative url
return
FULL_PATH
.
'/'
.
substr
(
THEMES_PATH
,
1
).
'/'
.
$path
;
}
/**
* Makes size clause for img tag, such as
* ' width="80" height="100"' according to max_width
* and max_heght limits.
*
* @param array $params
* @return string
*/
function
ImageSize
(
$params
)
{
$img_path
=
$this
->
getFullPath
(
$params
[
'img_path'
]);
$image_helper
=&
$this
->
Application
->
recallObject
(
'ImageHelper'
);
/* @var $image_helper ImageHelper */
$max_width
=
$this
->
getImageDimension
(
'Width'
,
$params
);
$max_height
=
$this
->
getImageDimension
(
'Height'
,
$params
);
$image_dimensions
=
$image_helper
->
GetImageDimensions
(
$img_path
,
$max_width
,
$max_height
);
if
(!
$image_dimensions
)
{
return
false
;
}
return
' width="'
.
$image_dimensions
[
0
].
'" height="'
.
$image_dimensions
[
1
].
'"'
;
}
/**
* Prepares image parameters & parses block with them (for admin)
*
* @param Array $params
* @return string
*/
function
Image
(
$params
)
{
$image_url
=
$this
->
ImageSrc
(
$params
);
if
(!
$image_url
)
{
return
;
}
$object
=&
$this
->
getObject
(
$params
);
$params
[
'img_path'
]
=
$image_url
;
$image_dimensions
=
$this
->
ImageSize
(
$params
);
$params
[
'img_size'
]
=
$image_dimensions
?
$image_dimensions
:
' width="'
.
$params
[
'DefaultWidth'
].
'"'
;
$params
[
'alt'
]
=
htmlspecialchars
(
$object
->
GetField
(
'AltName'
));
// really used ?
$params
[
'name'
]
=
$this
->
SelectParam
(
$params
,
'block,render_as'
);
$params
[
'align'
]
=
$params
[
'align'
]?
$params
[
'align'
]
:
'left'
;
if
(!
$object
->
isLoaded
()
&&
!
$this
->
SelectParam
(
$params
,
'default_image,DefaultImage'
))
{
return
false
;
}
return
$this
->
Application
->
ParseBlock
(
$params
);
}
/**
* Returns url for image in case when image source is url (for admin)
*
* @param Array $params
* @return string
*/
function
ImageUrl
(
$params
)
{
$object
=&
$this
->
getObject
(
$params
);
if
(
$object
->
GetDBField
(
'SameImages'
)
?
$object
->
GetDBField
(
'LocalThumb'
)
:
$object
->
GetDBField
(
'LocalImage'
)
)
{
$ret
=
$this
->
Application
->
Phrase
(
getArrayValue
(
$params
,
'local_phrase'
));
}
else
{
$ret
=
$object
->
GetDBField
(
'SameImages'
)
?
$object
->
GetDBField
(
'ThumbUrl'
)
:
$object
->
GetDBField
(
'Url'
);
}
return
$ret
;
}
}
?>
Event Timeline
Log In to Comment