Skip to content

Commit

Permalink
Release 2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmacwhite committed Jun 2, 2022
1 parent f3f8a84 commit db00606
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 95 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.3 - 2022-06-02
### Fixed
- Added migration to remove settings key from the tablemaker plugin config key if present. From a Craft 2 site there was a settings key in the plugins table.
- Bumped schema version to 1.0.1 to ensure migrations are run.
- Cleanup TableMaker fieldtype and settings

## 2.0.2 - 2022-06-02
### Added
- Craft 2 fieldtype migration
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "supercool/tablemaker",
"description": "A user-definable table field type for Craft CMS",
"type": "craft-plugin",
"version": "2.0.2",
"version": "2.0.3",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -38,7 +38,7 @@
"extra": {
"name": "Table Maker",
"handle": "tablemaker",
"schemaVersion": "1.0.0",
"schemaVersion": "1.0.1",
"hasCpSettings": false,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/nottinghamcollege/tablemaker/master/CHANGELOG.md",
Expand Down
181 changes: 95 additions & 86 deletions src/fields/TableMakerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@
use craft\helpers\Json;
use craft\helpers\Template;
use supercool\tablemaker\assetbundles\field\FieldAsset;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\db\Schema;

/**
* @author Supercool Ltd
* @package TableMaker
* @since 1.0.0
*
* @property-read string $contentColumnType
* @property-read null|string $settingsHtml
*/

class TableMakerField extends Field
{
// Public Properties
// =========================================================================

public $columnsLabel;
public $columnsInstructions;
public $columnsAddRowLabel;
public $rowsLabel;
public $rowsInstructions;
public $rowsAddRowLabel;
public string $columnsLabel = '';
public string $columnsInstructions = '';
public string $columnsAddRowLabel = '';
public string $rowsLabel = '';
public string $rowsInstructions = '';
public string $rowsAddRowLabel = '';


// Static Methods
Expand Down Expand Up @@ -65,10 +73,9 @@ public static function displayName(): string
*
* @return array
*/
public function rules()
public function rules(): array
{
$rules = parent::rules();
return $rules;
return parent::rules();
}


Expand Down Expand Up @@ -105,13 +112,11 @@ public function getContentColumnType(): string
public function normalizeValue($value, ElementInterface $element = null)
{

if ( !is_array($value) )
{
if (!is_array($value)) {
$value = Json::decode($value);
}

if ( !isset($value['rows']) )
{
if (!isset($value['rows'])) {
$value['rows'] = [];
}

Expand All @@ -122,8 +127,7 @@ public function normalizeValue($value, ElementInterface $element = null)
<tr>
';

if ( !empty($value['columns']) )
{
if (!empty($value['columns'])) {
foreach ($value['columns'] as $col)
{
$html .= '<th align="' . $col['align'] . '" width="' . $col['width'] . '">' . $col['heading'] . '</th>';
Expand All @@ -138,12 +142,8 @@ public function normalizeValue($value, ElementInterface $element = null)
';

if ( !empty($value['rows']) )
{

foreach ($value['rows'] as $row)
{

if (!empty($value['rows'])) {
foreach ($value['rows'] as $row) {
$html .= '<tr>';

$i = 0;
Expand All @@ -154,9 +154,7 @@ public function normalizeValue($value, ElementInterface $element = null)
}

$html .= '</tr>';

}

}

$html .= '
Expand All @@ -170,7 +168,7 @@ public function normalizeValue($value, ElementInterface $element = null)

return $value;
}


/**
* Modifies an element query.
Expand All @@ -179,47 +177,43 @@ public function normalizeValue($value, ElementInterface $element = null)
*
* If the method returns `false`, the query will be stopped before it ever gets a chance to execute.
*
* @param ElementQueryInterface $query The element query
* @param mixed $value The value that was set on this field’s corresponding [[ElementCriteriaModel]] param,
* if any.
*
* @return null|false `false` in the event that the method is sure that no elements are going to be found.
* @param mixed $value The value that was set on this field’s corresponding [[ElementCriteriaModel]] param, if any.
* @param ElementInterface|null $element The element query
* @return null|false `false` in the event that the method is sure that no elements are going to be found.
*/
public function serializeValue($value, ElementInterface $element = null)
public function serializeValue($value, ElementInterface $element = null): ?bool
{

if ( !empty($value['rows']) && is_array($value['rows']) )
{
if (!empty($value['rows']) && is_array($value['rows'])) {
// drop keys from the rows array
$value['rows'] = array_values($value['rows']);

// loop each row
foreach ($value['rows'] as &$row)
{
if ( is_array($row) )
{
foreach ($value['rows'] as &$row) {
if (is_array($row)) {
// drop those array keys
$row = array_values($row);
}
}
}

// // drop keys from the columns array
if ( !empty($value['columns']) && is_array($value['columns']) )
{
if (!empty($value['columns']) && is_array($value['columns'])) {
$value['columns'] = array_values($value['columns']);
}

return parent::serializeValue($value, $element);
}


/**
* Returns the component’s settings HTML.
*
* @throws Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @return string|null
*/
public function getSettingsHtml()
public function getSettingsHtml(): ?string
{
// Render the settings template
return Craft::$app->getView()->renderTemplate(
Expand All @@ -230,15 +224,18 @@ public function getSettingsHtml()
);
}


/**
* Returns the field’s input HTML.
*
* @param mixed $value The field’s value. This will either be the [[normalizeValue() normalized value]],
* raw POST data (i.e. if there was a validation error), or null
* @param ElementInterface|null $element The element the field is associated with, if there is one
*
* @return string The input HTML.
* @param mixed $value The field’s value. This will either be the [[normalizeValue() normalized value]],
raw POST data (i.e. if there was a validation error), or null
* @param ElementInterface|null $element The element the field is associated with, if there is one
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws Exception
* @throws InvalidConfigException
* @return string The input HTML.
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
Expand All @@ -250,7 +247,7 @@ public function getInputHtml($value, ElementInterface $element = null): string
$name = $this->handle;

$columnsInput = $name.'[columns]';
$rowsInput = $name.'[rows]';
$rowsInput = $name.'[rows]';

$columnsInputId = $name.'-columns';
$rowsInputId = $name.'-rows';
Expand All @@ -259,33 +256,30 @@ public function getInputHtml($value, ElementInterface $element = null): string
$input = '<input class="table-maker-field" type="hidden" name="'.$name.'" value="">';

// get columns from db or fall back to default
if ( !empty($value['columns']) )
{
if (!empty($value['columns'])) {
foreach ($value['columns'] as $key => $val) {
$columns['col'.$key] = array(
$columns['col'.$key] = [
'heading' => $val['heading'],
'align' => $val['align'],
'width' => $val['width'],
'type' => 'multiline'
);
];
}
}
else
{
$columns = array(
'col0' => array(
else {
$columns = [
'col0' => [
'heading' => '',
'align' => '',
'width' => '',
'type' => 'multiline'
)
);
]
];
}


// get rows from db or fall back to default
if ( !empty($value['rows']) )
{
if (!empty($value['rows'])) {
// walk down the rows and cells appending 'row' to the rows' keys
// and 'col' to the cells' keys
foreach ($value['rows'] as $rowKey => $rowVal) {
Expand All @@ -294,34 +288,33 @@ public function getInputHtml($value, ElementInterface $element = null): string
}
}
}
else
{
$rows = array('row0' => array());
else {
$rows = ['row0' => []];
}

// prep col settings
$columnSettings = array(
'heading' => array(
$columnSettings = [
'heading' => [
'heading' => Craft::t('tablemaker', 'Heading'),
'type' => 'singleline'
),
'width' => array(
],
'width' => [
'heading' => Craft::t('tablemaker', 'Width'),
'class' => 'code',
'type' => 'singleline',
'width' => 50
),
'align' => array(
],
'align' => [
'heading' => Craft::t('tablemaker', 'Alignment'),
'class' => 'thin',
'type' => 'select',
'options' => array(
'options' => [
'left' => Craft::t('tablemaker', 'Left'),
'center' => Craft::t('tablemaker', 'Center'),
'right' => Craft::t('tablemaker', 'Right')
)
)
);
]
]
];

// init the js
$view->registerJs('new Craft.TableMaker(' .
Expand All @@ -339,29 +332,45 @@ public function getInputHtml($value, ElementInterface $element = null): string
$fieldSettings = $this->getSettings();

$columnsField = $view->renderTemplate('_includes/forms/editableTable', [
'label' => $fieldSettings['columnsLabel'] ? Craft::t('tablemaker', $fieldSettings['columnsLabel']) : Craft::t('tablemaker', 'Table Columns'),
'instructions' => $fieldSettings['columnsInstructions'] ? Craft::t('tablemaker', $fieldSettings['columnsInstructions']) : Craft::t('tablemaker', 'Define the columns your table should have.'),
'id' => $columnsInputId,
'name' => $columnsInput,
'cols' => $columnSettings,
'rows' => $columns,
'addRowLabel' => $fieldSettings['columnsAddRowLabel'] ? Craft::t('tablemaker', $fieldSettings['columnsAddRowLabel']) : Craft::t('tablemaker', 'Add a column'),
'initJs' => false
'label' => $fieldSettings['columnsLabel'] ?
Craft::t('tablemaker', $fieldSettings['columnsLabel']) :
Craft::t('tablemaker', 'Table Columns'),

'instructions' => $fieldSettings['columnsInstructions'] ?
Craft::t('tablemaker', $fieldSettings['columnsInstructions']) :
Craft::t('tablemaker', 'Define the columns your table should have.'),

'id' => $columnsInputId,
'name' => $columnsInput,
'cols' => $columnSettings,
'rows' => $columns,
'addRowLabel' => $fieldSettings['columnsAddRowLabel'] ?
Craft::t('tablemaker', $fieldSettings['columnsAddRowLabel']) :
Craft::t('tablemaker', 'Add a column'),

'initJs'=> false
]);

$rowsField = $view->renderTemplate('_includes/forms/editableTable', [
'label' => $fieldSettings['rowsLabel'] ? Craft::t('tablemaker', $fieldSettings['rowsLabel']) : Craft::t('tablemaker', 'Table Content'),
'instructions' => $fieldSettings['rowsInstructions'] ? Craft::t('tablemaker', $fieldSettings['rowsInstructions']) : Craft::t('tablemaker', 'Input the content of your table.'),
'label' => $fieldSettings['rowsLabel'] ?
Craft::t('tablemaker', $fieldSettings['rowsLabel']) :
Craft::t('tablemaker', 'Table Content'),

'instructions' => $fieldSettings['rowsInstructions'] ?
Craft::t('tablemaker', $fieldSettings['rowsInstructions']) :
Craft::t('tablemaker', 'Input the content of your table.'),

'id' => $rowsInputId,
'name' => $rowsInput,
'cols' => $columns,
'rows' => $rows,
'addRowLabel' => $fieldSettings['rowsAddRowLabel'] ? Craft::t('tablemaker', $fieldSettings['rowsAddRowLabel']) : Craft::t('tablemaker', 'Add a row'),
'addRowLabel' => $fieldSettings['rowsAddRowLabel'] ?
Craft::t('tablemaker', $fieldSettings['rowsAddRowLabel']) :
Craft::t('tablemaker', 'Add a row'),

'initJs' => false
]);

return $input . $columnsField . $rowsField;
}


}
2 changes: 1 addition & 1 deletion src/migrations/m220602_070853_remove_plugin_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function safeUp()
$projectConfig->muteEvents = true;

echo " > Removing plugin settings key...\n";
$projectConfig->remove($plugins['tablemaker']['settings']);
$projectConfig->remove('plugins.tablemaker.settings');

$projectConfig->muteEvents = false;
}
Expand Down
Loading

0 comments on commit db00606

Please sign in to comment.