Skip to content

Commit

Permalink
Release 1.3.0
Browse files Browse the repository at this point in the history
+ PHP7 compatibility
+ Symphony 2.7 compatibility
+ Code cleanup
+ Added clean file name to data source output
  • Loading branch information
twiro committed Jul 10, 2018
1 parent 8b46c5a commit c47fc67
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 145 deletions.
4 changes: 2 additions & 2 deletions LICENCE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ licence as follows:

----- begin license block -----

Copyright 2008-2016 Rowan Lewis, Michael Eichelsdoerfer, Simon de Turck, Roman Klein
Copyright 2008-2018 Rowan Lewis, Michael Eichelsdoerfer, Simon de Turck, Roman Klein

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,4 +24,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

----- end license block -----
----- end license block -----
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Symphony CMS : Reflected Upload Field #
# Reflected Upload Field

An upload field that dynamically renames files (based on values from other fields in the same entry).
#### An upload field for Symphony CMS that dynamically renames files (based on values from other fields in the same entry).

## 1. Installation

Expand All @@ -10,11 +10,19 @@ An upload field that dynamically renames files (based on values from other field
3. You can now add the '**Reflected File Upload**' field to your sections.


## 2. Configuration & Usage ##
## 2. Field Settings

This field enables you to specify the naming expression using XPath (like in the reflection field). When uniqueness is important you can enable the "Always create unique name" option. This will add "Unique Upload Field" behavior by appending a unique token to the filename.
Compared to Symphony's default upload field **Reflected Upload Field** comes with the following two additional settings:

1. **Expression** represents the "formula" that's used to generate the reflected filename. You can either use static text or access other fields of the current entry via XPath: <code>{//entry/field-one} static text {//entry/field-two}</code>.
2. **Create unique filenames** gives you the option to add a unique token to the end of the generated filename. This random token will change whenever you save or resave an entry – so this option guarantees that the generated filenames won't get you into caching-troubles whenever you swap files in an entry.


## 3. Acknowledgements ##

This extension is based on [Unique Upload Field](https://github.com/michael-e/uniqueuploadfield) and [Reflection Field](https://github.com/symphonists/reflectionfield).
This extension was initially developed by [Simon de Turck][1] and is based on the extensions [Unique Upload Field][2] and [Reflection Field][3].


[1]: https://github.com/zimmen
[2]: https://github.com/michael-e/uniqueuploadfield
[3]: https://github.com/symphonists/reflectionfield
239 changes: 135 additions & 104 deletions extension.driver.php
Original file line number Diff line number Diff line change
@@ -1,92 +1,152 @@
<?php
class extension_reflecteduploadfield extends Extension {

#########################
##### CLASS METHODS #####
#########################
class extension_reflecteduploadfield extends Extension
{
/*------------------------------------------------------------------------*/
/* DEFINITION & SETTINGS
/*------------------------------------------------------------------------*/

/**
* Name of the extension field table
* @var string
*
* @since version 2.1.0
*/

const FIELD_TBL_NAME = 'tbl_fields_reflectedupload';

/**
* Holds the fields that need post-save treatment
* @static
* @var array
* Holds the fields that need post-save treatment
*
* @since version 1.0.0
*/

protected static $fields = array();

/**
* Add field to the $fields array for post save treatment
* @static
* @param $field
* @return void
* Add field to the $fields array for post save treatment
*
* @since version 1.0.0
*/
public static function registerField($field) {

public static function registerField($field)
{
self::$fields[] = $field;
}

/**
* @static
* @param $entry
* @return DOMXPath
* Gets XPATH Dom for entry.
* Function by Rowan Lewis <[email protected]>
* GET SUBSCRIBES DELEGATES
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#getSubscribedDelegates
*
* @since version 1.0.0
*/

public function getSubscribedDelegates()
{
return array(
array(
'page' => '/publish/new/' ,
'delegate' => 'EntryPostCreate' ,
'callback' => 'compileFields'
),
array(
'page' => '/publish/edit/' ,
'delegate' => 'EntryPostEdit' ,
'callback' => 'compileFields'
),
array(
'page' => '/frontend/' ,
'delegate' => 'EventPostSaveFilter' ,
'callback' => 'compileFields'
)
);
}

/**
* COMPILE FIELDS (delegate callback)
*
* @param $context
* @return void
* @since version 1.0.0
*/
public static function getXPath($entry) {
$entry_xml = new XMLElement('entry');
$section_id = $entry->get('section_id');
$data = $entry->getData();
$fields = array();
$entry_xml->setAttribute('id' , $entry->get('id'));

$associated = $entry->fetchAllAssociatedEntryCounts();

if (is_array($associated) and !empty($associated)) {
foreach ($associated as $section => $count) {
$handle = Symphony::Database()->fetchVar('handle' , 0 , "
SELECT s.handle FROM `tbl_sections` AS s
WHERE s.id = '{$section}'
LIMIT 1
");
$entry_xml->setAttribute($handle , (string)$count);

public function compileFields($context)
{
foreach (self::$fields as $field) {
if (!$field->compile($context['entry'])) {
// TODO:ERROR
}
}
}

// Add fields:
foreach ($data as $field_id => $values) {
if (empty($field_id)) continue;
$fm = new FieldManager($entry);
$field =& $fm->fetch($field_id);
$field->appendFormattedElement($entry_xml , $values , false , null);
}
/*------------------------------------------------------------------------*/
/* INSTALL / UPDATE / UNINSTALL
/*------------------------------------------------------------------------*/

$xml = new XMLElement('data');
$xml->appendChild($entry_xml);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
$dom->loadXML($xml->generate(true));
/**
* INSTALL
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#install
*
* @since version 1.0.0
*/

$xpath = new DOMXPath($dom);
public function install()
{
return self::createFieldTable();
}

if (version_compare(phpversion() , '5.3' , '>=')) {
$xpath->registerPhpFunctions();
}
/**
* CREATE FIELD TABLE
*
* @since version 1.3.0
*/

return $xpath;
public static function createFieldTable()
{
$tbl = self::FIELD_TBL_NAME;

return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `$tbl` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`destination` varchar(255) NOT NULL,
`validator` varchar(50),
`expression` VARCHAR(255) DEFAULT NULL,
`unique` tinyint(1) default '0',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}

############################
##### INSTANCE METHODS #####
############################
/**
* UPDATE
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#update
*
* @since version 1.0.0
*/

public function update($previousVersion) {

if(version_compare($previousVersion, '1.0','<=')){
public function update($previousVersion = false)
{
// updating from versions prior to 1.0
if(version_compare($previousVersion, '1.0','<=')) {
Symphony::Database()->query("ALTER TABLE `tbl_fields_reflectedupload` ADD `unique` tinyint(1) default '0'");
}
// Before 1.2

// updating from versions prior to 1.2
if (version_compare($previous_version, '1.2', '<')) {

// Remove directory from the upload fields, fixes Symphony Issue #1719
$upload_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_reflectedupload`");

if(is_array($upload_tables) && !empty($upload_tables)) foreach($upload_tables as $field) {
Symphony::Database()->query(sprintf(
"UPDATE tbl_entries_data_%d SET file = substring_index(file, '/', -1)",
Expand All @@ -96,61 +156,32 @@ public function update($previousVersion) {
}
}

public function uninstall() {

Symphony::Database()->query("DROP TABLE `tbl_fields_reflectedupload`");
}

public function install() {
return Symphony::Database()->query(
"CREATE TABLE `tbl_fields_reflectedupload` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`destination` varchar(255) NOT NULL,
`validator` varchar(50),
`expression` VARCHAR(255) DEFAULT NULL,
`unique` tinyint(1) default '0',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
}

/**
* Delegation
* @return array
* UNINSTALL
*
* http://www.getsymphony.com/learn/api/2.4/toolkit/extension/#uninstall
*
* @since version 1.0.0
*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/publish/new/' ,
'delegate' => 'EntryPostCreate' ,
'callback' => 'compileFields'
) ,

array(
'page' => '/publish/edit/' ,
'delegate' => 'EntryPostEdit' ,
'callback' => 'compileFields'
) ,
array(
'page' => '/frontend/' ,
'delegate' => 'EventPostSaveFilter' ,
'callback' => 'compileFields'
)
);
public function uninstall()
{
return self::deleteFieldTable();
}

/**
* @param $context
* @return void
* Delegate callback
* DELETE FIELD TABLE
*
* @since version 1.3.0
*/
public function compileFields($context) {
foreach (self::$fields as $field) {
if (!$field->compile($context['entry'])) {
//TODO:Error
}
}

public static function deleteFieldTable()
{
$tbl = self::FIELD_TBL_NAME;

return Symphony::Database()->query("
DROP TABLE IF EXISTS `$tbl`
");
}

}
18 changes: 11 additions & 7 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@
<extension id="reflecteduploadfield" status="released" xmlns="http://symphony-cms.com/schemas/extension/1.0">
<name>Reflected Upload Field</name>
<description>An upload field that dynamically renames files (based on values from other fields in the same entry).</description>
<repo type="github">https://github.com/twiro/reflecteduploadfield</repo>
<repo type="github">https://github.com/twiro/reflecteduploadfield/</repo>
<url type="issues">https://github.com/twiro/reflecteduploadfield/issues</url>
<url type="discuss">http://www.getsymphony.com/discuss/thread/80974/</url>
<types>
<type>Field</type>
<type>Reflection</type>
</types>
<authors>
<author>
<name github="twiro" symphony="roman">Roman Klein</name>
<website>http://romanklein.com</website>
</author>
<author>
<name github="zimmen" symphony="Zimmen">Simon de Turck</name>
<website>http://www.zimmen.com</website>
</author>
</authors>
<releases>
<release version="1.2" date="2016-03-10" min="2.3.3" max="2.5.x">
<release version="1.3.0" date="2018-07-10" min="2.4.x" max="2.7.x">
* PHP7 compatibility
* Symphony 2.7 compatibility
* Code cleanup
* Added clean file name to data source output
</release>
<release version="1.2.0" date="2016-03-10" min="2.3.3" max="2.5.x">
* Added compatibility with Symphony 2.3.3
* File Path no longer saved as part of the file name
* Updated readme and licence
</release>
<release version="1.1" date="2012-07-25" min="2.3.0" max="2.3.2">
<release version="1.1.0" date="2012-07-25" min="2.3.0" max="2.3.2">
* Added compatibility with Symphony 2.3.0
* Fix when removing a file and not picking a new one
</release>
Expand Down
Loading

0 comments on commit c47fc67

Please sign in to comment.