From c32e5bf1e272cdc75154e1b66bd4a29b6cece74a Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 8 Feb 2021 08:44:44 -0800 Subject: [PATCH] e107::unserialize() will now return the array if the input is an array. Additional form-handler tests added for type: media, file and files. Admin-ui can now manage a change of field type from 'array' to 'json' without additional changes needed. --- e107_handlers/core_functions.php | 2 +- e107_handlers/form_handler.php | 58 +++++++++++++----------- e107_handlers/model_class.php | 2 +- e107_handlers/user_model.php | 2 +- e107_tests/tests/unit/e_formTest.php | 61 +++++++++++++++++++++----- e107_themes/bootstrap3/admin_style.css | 2 +- 6 files changed, 86 insertions(+), 41 deletions(-) diff --git a/e107_handlers/core_functions.php b/e107_handlers/core_functions.php index 0aad530b3..35e3b0411 100644 --- a/e107_handlers/core_functions.php +++ b/e107_handlers/core_functions.php @@ -541,7 +541,7 @@ class e_array { if(is_array($ArrayData)) { - return false; + return $ArrayData; } // Saftety mechanism for 0.7 -> 0.8 transition. diff --git a/e107_handlers/form_handler.php b/e107_handlers/form_handler.php index 13d560545..29476b117 100644 --- a/e107_handlers/form_handler.php +++ b/e107_handlers/form_handler.php @@ -1133,7 +1133,7 @@ class e_form $class = !empty($extras['class']) ? $extras['class']. ' ' : ''; $title = !empty($extras['title']) ? $extras['title'] : $title; - $ret = "".$label. ''; // using bootstrap. + $ret = "".$label. ''; // using bootstrap. if(!e107::getRegistry('core/form/mediaurl')) { @@ -1449,9 +1449,9 @@ class e_form $ret .= $preview; // image, video. audio tag etc. - $ret .= '
-
'.$editIcon.$previewIcon.'
-
'; + $ret .= "
+
".$editIcon.$previewIcon."
+
"; $ret .= "\n"; $ret .= ""; @@ -1589,7 +1589,7 @@ class e_form $ret = ''; - if($sc_parameters['data'] === 'array') + if(isset($sc_parameters['data']) && $sc_parameters['data'] === 'array') { // Do not use $this->hidden() method - as it will break 'id' value. $ret .= ""; @@ -5036,6 +5036,10 @@ var_dump($select_options);*/ public function renderValue($field, $value, $attributes, $id = 0) { + if(!empty($value) && !empty($attributes['data']) && ($attributes['data'] === 'array' || $attributes['data'] === 'json')) + { + $value = e107::unserialize($value); + } if(!empty($attributes['multilan']) && is_array($value)) { @@ -5638,22 +5642,26 @@ var_dump($select_options);*/ case 'media': - - if(!empty($value) && $attributes['data'] === 'json') - { - $tmp = e107::unserialize($value); - $value = !empty($tmp[0]['path']) ? $tmp[0]['path'] : null; // display first item. - } - - return e107::getMedia()->previewTag($value, $parms); + case 'images': + $firstItem = !empty($value[0]['path']) ? $value[0]['path'] : null; // display first item. + return e107::getMedia()->previewTag($firstItem, $parms); break; case 'files': + if(!empty($value) && !is_array($value)) + { + return "Type 'files' must have a data type of 'array' or 'json'"; + } $ret = '
    '; for ($i=0; $i < 5; $i++) { - //$k = $key.'['.$i.'][path]'; $ival = $value[$i]['path']; + + if(empty($ival)) + { + continue; + } + $ret .= '
  1. '.$ival.'
  2. '; } $ret .= '
'; @@ -5924,11 +5932,6 @@ var_dump($select_options);*/ $method = varset($attributes['field']); // prevents table alias in method names. ie. u.my_method. $_value = $value; - if(!empty($attributes['data']) && $attributes['data'] === 'array') // FIXME @SecretR - please move this to where it should be. - { - $value = e107::unserialize($value); // (saved as array, return it as an array) - } - $meth = (!empty($attributes['method'])) ? $attributes['method'] : $method; if(strpos($meth,'::')!==false) @@ -6105,6 +6108,12 @@ var_dump($select_options);*/ */ public function renderElement($key, $value, $attributes, $required_data = array(), $id = 0) { + + if(!empty($value) && !empty($attributes['data']) && ($attributes['data'] === 'array' || $attributes['data'] === 'json')) + { + $value = e107::unserialize($value); + } + $tp = e107::getParser(); $ret = ''; @@ -6356,7 +6365,7 @@ var_dump($select_options);*/ break; case 'images': - // return print_a($value, true); + $ret = ''; $label = varset($parms['label'], 'LAN_EDIT'); $max = varset($parms['max'],5); @@ -6379,11 +6388,6 @@ var_dump($select_options);*/ $max = varset($parms['max'],1); - if(!empty($value) && $attributes['data'] === 'json') - { - $value = e107::unserialize($value); - } - $ret = "
"; for ($i=0; $i < $max; $i++) { @@ -6399,8 +6403,10 @@ var_dump($select_options);*/ break; case 'files': + + $label = varset($parms['label'], 'LAN_EDIT'); - if($attributes['data'] === 'array') + if(!empty($attributes['data']) && ($attributes['data'] === 'array' || $attributes['data'] === 'json')) { $parms['data'] = 'array'; } diff --git a/e107_handlers/model_class.php b/e107_handlers/model_class.php index 4ddab77d7..038105356 100755 --- a/e107_handlers/model_class.php +++ b/e107_handlers/model_class.php @@ -2108,7 +2108,7 @@ class e_front_model extends e_model { $d = $this->getDataFields(); - if(!empty($d[$key]) && ($d[$key] == 'array')) + if(!empty($d[$key]) && ($d[$key] === 'array' || $d[$key] === 'json')) { return e107::unserialize($this->getData((string) $key, $default, $index)); } diff --git a/e107_handlers/user_model.php b/e107_handlers/user_model.php index 7a0975feb..1a9f86a22 100644 --- a/e107_handlers/user_model.php +++ b/e107_handlers/user_model.php @@ -1787,7 +1787,7 @@ class e_user extends e_user_model $userlogin = new userlogin(); - if(e_PAGE === 'admin.php') + if(defset('e_PAGE') === 'admin.php') { $userlogin->setSecureImageMode('admin'); // use the admin secure code pref. } diff --git a/e107_tests/tests/unit/e_formTest.php b/e107_tests/tests/unit/e_formTest.php index 255ec38a6..ef803ac12 100644 --- a/e107_tests/tests/unit/e_formTest.php +++ b/e107_tests/tests/unit/e_formTest.php @@ -79,8 +79,8 @@ class e_formTest extends \Codeception\Test\Unit 'bbarea_001' => array('title'=>'BBarea', 'type'=>'bbarea', 'inline'=>false), 'icon_001' => array('title'=>'Icon', 'type'=>'icon', 'inline'=>false), // 'media_001' => array('title'=>'Media', 'type'=>'media', 'inline'=>false), - // 'file_001' => array('title'=>'File', 'type'=>'file', 'inline'=>false), //FIXME - // 'files_001' => array('title'=>'File', 'type'=>'files', 'inline'=>false), //FIXME + 'file_001' => array('title'=>'File', 'type'=>'file', 'inline'=>false), + 'files_001' => array('title'=>'Files', 'type'=>'files', 'data'=> 'json', 'inline'=>false), 'datestamp_001' => array('title'=>'Datestamp', 'type'=>'datestamp', 'inline'=>false), 'date_001' => array('title'=>'Date', 'type'=>'date'), 'userclass_001' => array('title'=>'Userclass', 'type'=>'userclass'), @@ -92,6 +92,9 @@ class e_formTest extends \Codeception\Test\Unit // 'method_001' => array('title'=>'Method' , 'type'=>'method', 'inline'=>false), 'language_001' => array('title'=>'Language' , 'type'=>'language'), 'userclass_002' => array('title'=>'Userclass', 'type'=>'userclass', 'writeParms'=>array('default'=>255 /* e_UC_NOBODY*/)), + + 'media_001' => array('title'=>'Media', 'type'=>'media', 'data'=>'json', 'inline'=>false), + // 'lanlist_001' => array('title'=>'Lanlist' , 'type'=>'lanlist', 'inline'=>false), @@ -128,9 +131,35 @@ class e_formTest extends \Codeception\Test\Unit 'tags_001' => 'keyword1,keyword2,keyword3', 'bbarea_001' => '[html]bold[/html]', 'icon_001' => '{e_IMAGE}e107_icon_32.png', - // 'media_001' => '', // TODO - saves as json format. + 'file_001' => '{e_MEDIA_FILE}test.zip', - // 'files_001' => '{e_MEDIA_FILE}test.zip', + 'files_001' => '[ + { + "path": "{e_MEDIA_FILE}test.zip", + "name": "test.zip", + "id": "171" + }, + { + "path": "", + "name": "", + "id": "" + }, + { + "path": "", + "name": "", + "id": "" + }, + { + "path": "", + "name": "", + "id": "" + }, + { + "path": "", + "name": "", + "id": "" + } +]', 'datestamp_001' => 1454367600, 'date_001' => '2018-08-23', 'userclass_001' => 0, @@ -142,6 +171,14 @@ class e_formTest extends \Codeception\Test\Unit 'method_001' => 'custom-value', 'language_001' => 'fr', 'userclass_002' => '', + 'media_001' => '[ + { + "path": "{e_MEDIA_IMAGE}2020-12\/logo_alone_1050.jpg" + }, + { + "path": "{e_MEDIA_IMAGE}2020-12\/no1.png" + }, +]' // 'lanlist_001' => 'German', ); @@ -946,9 +983,9 @@ class e_formTest extends \Codeception\Test\Unit 'tags_001' => 'keyword1, keyword2, keyword3', 'bbarea_001' => 'bold', 'icon_001' => "e107_icon_32.png", - 'media_001' => '', - // 'file_001' => '{e_MEDIA_FILE}test.zip', - // 'files_001' => '{e_MEDIA_FILE}test.zip', + + 'file_001' => '{e_MEDIA_FILE}test.zip', + 'files_001' => '
  1. {e_MEDIA_FILE}test.zip
', 'datestamp_001' => '01 Feb 2016 : 15:00', 'date_001' => '2018-08-23', 'userclass_001' => 'Everyone (public)', @@ -960,6 +997,7 @@ class e_formTest extends \Codeception\Test\Unit // 'method_001' => 'custom-value', 'language_001' => 'French', 'userclass_002' => 'Everyone (public)', + 'media_001' => '', // 'lanlist_001' => 'German', // only works with multiple languages installed. @@ -1061,9 +1099,9 @@ class e_formTest extends \Codeception\Test\Unit // 'bbarea_001' => 'bold', // 'icon_001' => "e107_icon_32.png", - // 'media_001' => '', - // 'file_001' => '{e_MEDIA_FILE}test.zip', - // 'files_001' => '{e_MEDIA_FILE}test.zip', + + 'file_001' => "{e_MEDIA_FILE}test.zip", + 'files_001' => "
  1. {e_MEDIA_FILE}test.zip
  2. Choose a file
  3. Choose a file
  4. Choose a file
  5. Choose a file
", 'datestamp_001' => "", 'date_001' => "", 'userclass_001' => "", @@ -1075,6 +1113,7 @@ class e_formTest extends \Codeception\Test\Unit 'hidden_001' => "", // 'method_001' => 'custom-value', 'language_001' => "", + 'media_001' => "
", // 'lanlist_001' => 'German', // only works with multiple languages installed. ); @@ -1088,7 +1127,7 @@ class e_formTest extends \Codeception\Test\Unit $result = str_replace(array("\n", "\r"), "", $result); - if(empty($expected[$field])) + if(!isset($expected[$field])) { continue; // echo $result; diff --git a/e107_themes/bootstrap3/admin_style.css b/e107_themes/bootstrap3/admin_style.css index 16a8e2abe..c1d9aeb18 100644 --- a/e107_themes/bootstrap3/admin_style.css +++ b/e107_themes/bootstrap3/admin_style.css @@ -205,7 +205,7 @@ ul.checkboxes { display: inline-block; list-style: none; margin: 0; padding: 0;} .has-feedback-left input { padding-left:30px; } .form-control[disabled], .form-control[readonly] { - background-color: transparent; + /*background-color: transparent;*/ box-shadow: none; }