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 #16

Open
wants to merge 12 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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Input field that provides built-in number validation and numeric sorting.

The number field provides two additional datasource filtering methods:

### Range filtering
### 1) Range filtering

You can easily filter by a numeric range on the number field on your datasource. Simply enter something like this:

Expand All @@ -24,13 +24,18 @@ Just like any other datasource filter, you can make these values dynamic:

{$url-lower-limit} to {$url-upper-limit}

This would let you pass through the upper and lower limit as url parameters. E.g. `/products/?lower-limit-10&upper-limit=20`
This would let you pass through the upper and lower limit as url parameters. E.g. `/products/?lower-limit=10&upper-limit=20`

### Less than or greater than
### 2) Less than or greater than

You can also use standard greater than or less than symbols in the filter value or you can use words. e.g.

> 20
greater than 20

This will return all entries that have a value greater than 20.
This will return all entries that have a value greater than 20.

<= 20
equal to or less than 20

This will return all entries that have a value of 20 or less.
30 changes: 21 additions & 9 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
Class extension_numberfield extends Extension {

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

public function install() {
return Symphony::Database()->query("
CREATE TABLE `tbl_fields_number` (
`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_number')
->ifNotExists()
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'field_id' => 'int(11)',
])
->keys([
'id' => 'primary',
'field_id' => 'unique',
])
->execute()
->success();
}

}
9 changes: 8 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="numberfield" status="released" xmlns="http://getsymphony.com/schemas/extension/1.0">
<name>Number Field</name>
<name>Field: Number</name>
<description>Dedicated number storage</description>
<repo type="github">https://github.com/symphonycms/numberfield</repo>
<url type="discuss">http://getsymphony.com/discuss/thread/144/</url>
Expand All @@ -14,6 +14,13 @@
</author>
</authors>
<releases>
<release version="2.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="1.7.2" date="2017-08-23" min="2.5.x" max="2.x.x">
- PHP7 Compatibility for Symphony 2.x.x
</release>
<release version="1.7.1" date="2016-01-05" min="2.5" max="2.6.x">
- Set 'entry_id' as unique
</release>
Expand Down
57 changes: 40 additions & 17 deletions fields/field.number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

require_once FACE . '/interface.exportablefield.php';
require_once FACE . '/interface.importablefield.php';
require_once EXTENSIONS . '/numberfield/lib/class.entryquerynumberadapter.php';

class FieldNumber extends Field implements ExportableField, ImportableField {
public function __construct() {
parent::__construct();
$this->entryQueryFieldAdapter = new EntryQueryNumberAdapter($this);

$this->_name = __('Number');
$this->_required = true;

$this->set('required', 'no');
}

Expand Down Expand Up @@ -36,16 +40,27 @@ public function canPrePopulate() {
}

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,
`value` double default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `entry_id` (`entry_id`),
KEY `value` (`value`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
);
return Symphony::Database()
->create('tbl_entries_data_' . $this->get('id'))
->ifNotExists()
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'entry_id' => 'int(11)',
'value' => [
'type' => 'double',
'null' => true,
],
])
->keys([
'id' => 'primary',
'entry_id' => 'unique',
'value' => 'key',
])
->execute()
->success();
}

/*-------------------------------------------------------------------------
Expand All @@ -55,7 +70,7 @@ public function createTable() {
public 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);
Expand Down Expand Up @@ -85,11 +100,11 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWit
$label->appendChild(
Widget::Input(
'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix,
(strlen($value) != 0 ? $value : NULL)
(strlen($value) != 0 ? $value : null)
)
);

if($flagWithError != NULL) {
if($flagWithError != null) {
$wrapper->appendChild(Widget::Error($label, $flagWithError));
}
else {
Expand All @@ -98,7 +113,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWit
}

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

if($this->get('required') == 'yes' && strlen($data) == 0){
$message = __('‘%s’ is a required field.', array($this->get('label')));
Expand All @@ -113,7 +128,7 @@ public function checkPostFieldData($data, &$message, $entry_id = null) {
return self::__OK__;
}

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

if (strlen(trim($data)) == 0) return array();
Expand Down Expand Up @@ -229,8 +244,16 @@ public function fetchFilterableOperators()
),
array(
'title' => 'between',
'filter' => 'x to y',
'help' => __('Find values between two values with, %s to %s', array(
'filter' => 'between: ',
'help' => __('Find values between two values with %s and %s', array(
'<code>$x</code>',
'<code>$y</code>'
))
),
array(
'title' => 'to',
'filter' => ' to ',
'help' => __('Find values between two values with %s to %s', array(
'<code>$x</code>',
'<code>$y</code>'
))
Expand Down
139 changes: 139 additions & 0 deletions lib/class.entryquerynumberadapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* @package toolkit
*/
/**
* Specialized EntryQueryFieldAdapter that facilitate creation of queries filtering/sorting data from
* an textarea Field.
* @see FieldNumber
* @since Symphony 3.0.0
*/
class EntryQueryNumberAdapter extends EntryQueryFieldAdapter
{
public function isFilterBetween($filter)
{
return preg_match('/^between:? ?(-?(?:\d+(?:\.\d+)?|\.\d+)) and (-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter);
}

public function createFilterBetween($filter, array $columns)
{
$field_id = General::intval($this->field->get('id'));
$filter = $this->field->cleanValue($filter);
$matches = [];
preg_match('/^between:? ?(-?(?:\d+(?:\.\d+)?|\.\d+)) and (-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter, $matches);

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

public function isFilterTo($filter)
{
return preg_match('/^(-?(?:\d+(?:\.\d+)?|\.\d+)) to (-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter);
}

public function createFilterTo($filter, array $columns)
{
$field_id = General::intval($this->field->get('id'));
$filter = $this->field->cleanValue($filter);
$matches = [];
preg_match('/^(-?(?:\d+(?:\.\d+)?|\.\d+)) to (-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter, $matches);

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

public function isFilterEqualLesserGreater($filter)
{
return preg_match('/^(equal to or )?(less|greater) than\s*(-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter);
}

public function createFilterEqualLesserGreater($filter, array $columns)
{
$field_id = General::intval($this->field->get('id'));
$filter = $this->field->cleanValue($filter);
$matches = [];
preg_match('/^(equal to or )?(less|greater) than\s*(-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter, $matches);

switch($matches[2]) {
case 'less':
$expression .= '<';
break;

case 'greater':
$expression .= '>';
break;
}

if($matches[1]){
$expression .= '=';
}

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

public function isFilterSymbol($filter)
{
return preg_match('/^(=?[<>]=?)\s*(-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter);
}

public function createFilterSymbol($filter, array $columns)
{
$field_id = General::intval($this->field->get('id'));
$filter = $this->field->cleanValue($filter);
$matches = [];
preg_match('/^(=?[<>]=?)\s*(-?(?:\d+(?:\.\d+)?|\.\d+))$/i', $filter, $matches);

$conditions = [];
foreach ($columns as $key => $col) {
$conditions[] = [$this->formatColumn($col, $field_id) => [$matches[1] => (int)$matches[2]]];
}
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());
} elseif ($this->isFilterTo($filter)) {
return $this->createFilterTo($filter, $this->getFilterColumns());
} elseif ($this->isFilterEqualLesserGreater($filter)) {
return $this->createFilterEqualLesserGreater($filter, $this->getFilterColumns());
} elseif ($this->isFilterSymbol($filter)) {
return $this->createFilterSymbol($filter, $this->getFilterColumns());
}
return $this->createFilterEquality($filter, $this->getFilterColumns());
}
}