-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ PHP7 compatibility + Symphony 2.7 compatibility + Code cleanup + Added clean file name to data source output
- Loading branch information
Showing
5 changed files
with
390 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)", | ||
|
@@ -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` | ||
"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.