Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP7 Compatibility release #2

Open
wants to merge 8 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
Class extension_timefield extends Extension{

public function uninstall(){
return Symphony::Database()->query("DROP TABLE `tbl_fields_time`");
return Symphony::Database()
->drop('tbl_fields_time')
->ifExists()
->execute()
->success();
}

public function install(){
return Symphony::Database()->query("
CREATE TABLE `tbl_fields_time` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
");
return Symphony::Database()
->create('tbl_fields_time')
->ifNotExists()
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'field_id' => 'int(11)'
])
->keys([
'id' => 'primary',
'field_id' => 'unique',
])
->execute()
->success();
}

}
}
6 changes: 5 additions & 1 deletion extension.meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension id="timefield" status="released" xmlns="http://getsymphony.com/schemas/extension/1.0">
<name>Time Field</name>
<name>Field: Time</name>
<description>A field for hours, minutes, and seconds; filterable by value or range.</description>
<repo type="github">https://github.com/symphonists/timefield</repo>
<url type="discuss">https://www.getsymphony.com/discuss/thread/207/</url>
Expand All @@ -14,6 +14,10 @@
</author>
</authors>
<releases>
<release version="3.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x">
- Update for Symphony 4.x
- Code refactoring for Database and EQFA
</release>
<release version="2.0.4" date="2017-08-10" min="2.6.0" max="2.x.x">
- Fix typo
</release>
Expand Down
72 changes: 44 additions & 28 deletions fields/field.time.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
require_once EXTENSIONS . '/timefield/lib/class.entryquerytimeadapter.php';

Class fieldTime extends Field{

function __construct(){
parent::__construct();
$this->entryQueryFieldAdapter = new EntryQueryTimeAdapter($this);

$this->_name = 'Time';
$this->_required = true;
$this->set('required', 'yes');
Expand Down Expand Up @@ -36,7 +39,7 @@ function commit(){
return FieldManager::saveSettings($id, array());
}

public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC'){
if(in_array(strtolower($order), array('random', 'rand'))) {
$sort = 'ORDER BY RAND()';
} else {
Expand Down Expand Up @@ -67,21 +70,21 @@ function groupRecords($records){
return $groups;
}

function displaySettingsPanel(XMLElement &$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL, $entry_id = NULL){
function displaySettingsPanel(XMLElement &$wrapper, $errors = null){
parent::displaySettingsPanel($wrapper, $errors);
$div = new XMLElement('div', NULL, array('class' => 'two columns'));
$div = new XMLElement('div', null, array('class' => 'two columns'));
$this->appendRequiredCheckbox($div);
$this->appendShowColumnCheckbox($div);
$wrapper->appendChild($div);
}

public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = NULL, $entry_id = NULL){
public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null){
$element = new XMLElement($this->get('element_name'), $data['value']);
$element->setAttribute('seconds', (int)$data['seconds']);
$wrapper->appendChild($element);
}

public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = NULL){
public function prepareTableValue($data, XMLElement $link = null, $entry_id = null){
$value = $data['value'];

if($link){
Expand All @@ -92,27 +95,27 @@ public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = NU
return $value;
}

function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL, $entry_id = NULL){
function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null){
$value = (int)$data['seconds'];

$label = Widget::Label($this->get('label'));
if($this->get('required') != 'yes') $label->appendChild(new XMLElement('i', 'Optional'));
$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($data['value']) != 0 ? self::timeIntToString($value) : NULL)));
$label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($data['value']) != 0 ? self::timeIntToString($value) : null)));



if($flagWithError != NULL) $wrapper->appendChild(Widget::Error($label, $flagWithError));
if($flagWithError != null) $wrapper->appendChild(Widget::Error($label, $flagWithError));
else $wrapper->appendChild($label);
}

public function displayDatasourceFilterPanel(XMLElement &$wrapper, $data = NULL, $errors = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL){
public function displayDatasourceFilterPanel(XMLElement &$wrapper, $data = null, $errors = null, $fieldnamePrefix = null, $fieldnamePostfix = null){
parent::displayDatasourceFilterPanel($wrapper, $data, $errors, $fieldnamePrefix, $fieldnamePostfix);
$text = new XMLElement('p', new XMLElement('p', __('To filter by ranges, use the format <code>HH:MM:SS to HH:MM:SS</code>'), array('class' => 'help')));
$wrapper->appendChild($text);
}

public function checkPostFieldData($data, &$message, $entry_id=NULL){
$message = NULL;
public function checkPostFieldData($data, &$message, $entry_id = null){
$message = null;

if($this->get('required') == 'yes' && strlen($data) == 0){
$message = __('This is a required field');
Expand All @@ -128,21 +131,34 @@ public function checkPostFieldData($data, &$message, $entry_id=NULL){
}

public function createTable(){

return Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` int(11) unsigned NOT NULL auto_increment,
`entry_id` int(11) unsigned NOT NULL,
`seconds` bigint(20) default NULL,
`value` varchar(20) default NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`),
KEY `seconds` (`seconds`)
) TYPE=MyISAM;"
);
}

public function processRawFieldData($data, &$status, &$message=null, $simulate = false, $entry_id = null){
return Symphony::Database()
->create('tbl_entries_data_' . $this->get('id'))
->ifNotExists()
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'entry_id' => 'int(11)',
'seconds' => [
'type' => 'bigint(20)',
'null' => true,
],
'value' => [
'type' => 'varchar(20)',
'null' => true,
],
])
->keys([
'id' => 'primary',
'entry_id' => 'key',
'seconds' => 'key',
])
->execute()
->success();
}

public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null){
$status = self::__OK__;

if(strlen($data) > 0){
Expand Down Expand Up @@ -172,10 +188,10 @@ public static function timeIntToString($int){
$minutes = max(0, (int)$minutes);
$seconds = max(0, (int)$seconds);

return "$hours:".($minutes < 10 ? '0' : NULL)."$minutes:" . ($seconds < 10 ? '0' : NULL) . $seconds;
return "$hours:".($minutes < 10 ? '0' : null)."$minutes:" . ($seconds < 10 ? '0' : null) . $seconds;
}

function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation=false){
function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false){
$field_id = $this->get('id');

## Check its not a regexp
Expand Down
65 changes: 65 additions & 0 deletions lib/class.entryquerytimeadapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @package toolkit
*/
/**
* Specialized EntryQueryFieldAdapter that facilitate creation of queries filtering/sorting data from
* a time Field.
* @see FieldTime
* @since Symphony 3.0.0
*/
class EntryQueryTimeAdapter extends EntryQueryFieldAdapter
{
public function isFilterBetween($filter)
{
return preg_match('/\b ?to ?\b/i', $filter);
}

public function createFilterBetween($filter, array $columns)
{
$field_id = General::intval($this->field->get('id'));
$filter = $this->field->cleanValue($filter);
$matches = [];
list($from, $to) = preg_split('/\b ?to ?\b/i', $filter);

$from = trim($from);
$to = trim($to);

$from_sec = $this->field::timeStringToInt($from);
$to_sec = $this->field::timeStringToInt($to);

$conditions = [];
foreach ($columns as $key => $col) {
$conditions[] = [$this->formatColumn($col, $field_id) => ['between' => [$from_sec, $to_sec]]];
}
if (count($conditions) < 2) {
return $conditions;
}
return ['or' => $conditions];
}

/**
* @see EntryQueryFieldAdapter::filterSingle()
*
* @param EntryQuery $query
* @param string $filter
* @return array
*/
protected function filterSingle(EntryQuery $query, $filter)
{
General::ensureType([
'filter' => ['var' => $filter, 'type' => 'string'],
]);

if ($this->isFilterBetween($filter)) {
return $this->createFilterBetween($filter, $this->getFilterColumns());
}
return $this->createFilterEquality($filter, ['seconds']);
}

public function getSortColumns()
{
return ['seconds'];
}
}