Skip to content

Commit 81d3bfa

Browse files
Merge pull request #1658 from clpetersonucf/issue/1657-add-featured-flag-to-widget-table
Adds featured flag to widget table
2 parents 4ea9cce + 22f40e5 commit 81d3bfa

File tree

9 files changed

+59
-12
lines changed

9 files changed

+59
-12
lines changed

fuel/app/classes/materia/widget.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Widget
1414
public $id = 0;
1515
public $is_answer_encrypted = true;
1616
public $in_catalog = true;
17+
public $featured = false;
1718
public $is_editable = true;
1819
public $is_playable = true;
1920
public $is_qset_encrypted = true;
@@ -101,6 +102,7 @@ public function get($id_or_clean_name)
101102
'height' => $w['height'],
102103
'id' => $w['id'],
103104
'in_catalog' => $w['in_catalog'],
105+
'featured' => $w['featured'],
104106
'is_editable' => $w['is_editable'],
105107
'name' => $w['name'],
106108
'is_playable' => $w['is_playable'],

fuel/app/classes/materia/widget/installer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ protected static function save_params(array $params, ?int $widget_id = null): in
313313
else
314314
{
315315
// update
316-
// Do not over-write the db's in_catalog flag
316+
// Do not over-write the db's in_catalog and featured flag
317317
if (isset($params['in_catalog'])) unset($params['in_catalog']);
318+
if (isset($params['featured'])) unset($params['featured']);
318319

319320
$num = \DB::update('widget')
320321
->set($params)

fuel/app/classes/materia/widget/manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Widget_Manager
1212
*
1313
* @return array The information and metadata about the widget or widgets called for.
1414
*/
15-
static public function get_widgets($widget_ids=null, $type='featured')
15+
static public function get_widgets($widget_ids=null, $type='catalog')
1616
{
1717
$widgets = [];
1818
// =============== Get the requested widgets =================
@@ -40,6 +40,7 @@ static public function get_widgets($widget_ids=null, $type='featured')
4040
break;
4141

4242
case 'featured':
43+
$query->where('featured', '1');
4344
case 'catalog':
4445
default:
4546
$query->where('in_catalog', '1');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Fuel\Migrations;
4+
5+
class Add_featured_to_widget
6+
{
7+
public function up()
8+
{
9+
\DBUtil::add_fields(
10+
'widget', // find the table that contains the assets.
11+
[
12+
'featured' => ['type' => 'enum', 'constraint' => "'0','1'", 'default' => '0'],
13+
]
14+
);
15+
16+
// Update featured field to match in_catalog values
17+
\DB::update('widget')
18+
->value('featured', \DB::expr('in_catalog'))
19+
->execute();
20+
21+
}
22+
23+
public function down()
24+
{
25+
\DBUtil::drop_fields(
26+
'widget',
27+
['featured']
28+
);
29+
}
30+
}

fuel/app/tests/widgets/manager.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,33 @@ public function test_get_all_widgets_all()
142142

143143
public function test_get_all_widgets_featured()
144144
{
145+
$not_featured = $this->make_disposable_widget();
145146
$not_in_catalog = $this->make_disposable_widget();
146147
$not_playable = $this->make_disposable_widget();
147148
$visible[] = $this->make_disposable_widget();
148149
$visible[] = $this->make_disposable_widget();
149150

150-
// this shouldn't show up
151+
// this shouldn't show up despite featured being true
151152
$this->_as_super_user();
152153
$args = $this->sample_widget_update_args($not_in_catalog->id, $not_in_catalog->clean_name);
153154
$args->in_catalog = false;
155+
$args->featured = true;
154156
$msg = \Materia\Widget_Manager::update_widget($args);
155157

156-
// this shouldn't show up
158+
// this shouldn't show up despite featured being true
157159
$args = $this->sample_widget_update_args($not_playable->id, $not_playable->clean_name);
158160
$args->is_playable = false;
161+
$args->featured = true;
159162
$msg = \Materia\Widget_Manager::update_widget($args);
160163

164+
// have to set featured to true for visible widgets, as it is false by default
165+
foreach ($visible as $widget)
166+
{
167+
$args = $this->sample_widget_update_args($widget->id, $widget->clean_name);
168+
$args->featured = true;
169+
$msg = \Materia\Widget_Manager::update_widget($args);
170+
}
171+
161172
$res = \Materia\Widget_Manager::get_widgets(null, 'featured');
162173
self::assertCount(2, $res);
163174

@@ -184,7 +195,7 @@ public function test_get_all_widgets_by_id()
184195
$args->is_playable = false;
185196
$msg = \Materia\Widget_Manager::update_widget($args);
186197

187-
$res = \Materia\Widget_Manager::get_widgets(null, 'featured');
198+
$res = \Materia\Widget_Manager::get_widgets(null, 'catalog');
188199
self::assertCount(2, $res);
189200

190201
self::assertEquals($visible[0]->id, $res[0]->id);
@@ -225,6 +236,7 @@ protected function sample_widget_update_args($id=0, $clean_name='clean_name')
225236
$args->id = $id;
226237
$args->clean_name = $clean_name;
227238
$args->in_catalog = 1;
239+
$args->featured = 0;
228240
$args->is_editable = 1;
229241
$args->is_scorable = 1;
230242
$args->is_playable = 1;

src/components/catalog-card.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const CatalogCard = ({
99
id,
1010
clean_name = '',
1111
in_catalog = '0',
12+
featured = '0',
1213
name = '',
1314
dir = '',
1415
meta_data,
@@ -17,7 +18,7 @@ const CatalogCard = ({
1718
}) => {
1819
// 'Featured' label
1920
let featuredLabelRender = null
20-
if (in_catalog === '1') {
21+
if (featured === '1') {
2122
featuredLabelRender = <div className='featured-label'>
2223
<svg xmlns='http://www.w3.org/2000/svg'
2324
width='18'

src/components/catalog-page.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Catalog from './catalog'
77
const CatalogPage = () => {
88
const { data: widgets, isLoading} = useQuery({
99
queryKey: 'catalog-widgets',
10-
queryFn: apiGetWidgetsByType,
10+
queryFn: () => apiGetWidgetsByType('catalog'),
1111
staleTime: Infinity
1212
})
1313

src/components/catalog.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ const Catalog = ({widgets = [], isLoading = true}) => {
6868
results = results.filter(w => re.test(w.name))
6969
}
7070

71-
// if there are no filters set, take out the featured widgets (those with in_catalog = 1)
71+
// if there are no filters set, take out the featured widgets (those with featured = 1)
7272
// when no filter is present, a featured section appears already showing featured widgets
7373
if (!isFiltered) {
74-
results = results.filter(w => parseInt(w.in_catalog) !== 1)
74+
results = results.filter(w => parseInt(w.featured) !== 1)
7575
}
7676

7777
return [results, isFiltered]
@@ -189,7 +189,7 @@ const Catalog = ({widgets = [], isLoading = true}) => {
189189

190190
let featuredWidgetsRender = null
191191
if (!isFiltered && totalWidgets > 0 ) {
192-
const featuredWidgetListRender = widgets.filter(w => w.in_catalog==='1')
192+
const featuredWidgetListRender = widgets.filter(w => w.featured==='1')
193193
.map(w => <CatalogCard {...w} key={w.id} />)
194194
featuredWidgetsRender = (
195195
<div className='widget-group'>

src/util/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export const apiGetInstancesForUser = userId => {
7171
.then(handleErrors)
7272
}
7373

74-
export const apiGetWidgetsByType = () => {
75-
return fetchGet('/api/json/widgets_get_by_type/', { body: `data=${formatFetchBody(['all'])}` })
74+
export const apiGetWidgetsByType = (type = 'all') => {
75+
return fetchGet('/api/json/widgets_get_by_type/', { body: `data=${formatFetchBody([type])}` })
7676
}
7777

7878
// Gets widget info

0 commit comments

Comments
 (0)