Hours of Operation using Matrix - expressionengine

I am building a directory of businesses and want to not only publish a list of business hours, but also to publish if a business is currently open for business.
In a matrix, I have 7 rows with row_1 representing Sunday row_7 for Saturday. So I have two questions.
Is this as concise as the code COULD be or is there a better way?
Is there a flaw in the conditional that tells whether a business is currently open? It seems to work right now, but not very tested.
{!-- Hours of Operation --}
{exp:stash:set name="hours-of-operation"}
The Current time is: {current_time format="%g:%i%a"}<br/>
{hours_of_operation}
{if row_count=="1"}Sunday{/if}
{if row_count=="2"}Monday{/if}
{if row_count=="3"}Tuesday{/if}
{if row_count=="4"}Wednesday{/if}
{if row_count=="5"}Thursday{/if}
{if row_count=="6"}Friday{/if}
{if row_count=="7"}Saturday{/if}
{open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}<br/>
{/hours_of_operation}
{/exp:stash:set}
{!-- Hours of Operation --}
{!-- Are we open? --}
{exp:stash:set name="are-we-open"}
{exp:mx_calc expression='{current_time format="%w"}+1'}
{!-- matrix --}
{hours_of_operation}
{if row_count=="{calc_result}"}
Today is: {current_time format="%l"}<br/>
<strong>
{if '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && '{close_time format="%H%i"}' <= '{current_time format="%H%i"}'}
We are currently open!{if:else}We are currently closed.
{/if}
</strong><br/>
Today's Hours are:<br/> <strong>{open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}</strong><br/>
{/if}
{/hours_of_operation}
{!-- matrix --}
{/exp:mx_calc}
{/exp:stash:set}
{!-- Are we open? --}

This looks good to me, the only thing I would change is add another column on the left of the matrix and call it day of week with a drop down to allow the client to select the day. then in your code you can get rid of all those conditionals and replace it with just {day_of_week}

This logic shouldn't work:
{if '{open_time format="%H%i"}' <= '{current_time format="%H%i"}' && '{close_time format="%H%i"}' <= '{current_time format="%H%i"}'}
You're checking that both the closing and opening times are less than current_time, rather than checking that current_time is between the two values. If the business is open then close_time should be more than current_time, not less. The logic should be:
{if
'{open_time format="%H%i"}' <= '{current_time format="%H%i"}' &&
'{close_time format="%H%i"}' > '{current_time format="%H%i"}'
}
Also if we're being picky, what do people do if they have to input data for a business that's closed entirely for one or more days of the week? If it were me I'd throw a PT Switch field in as a 'Closed All Day' column, defaulting to no. It would only require a small tweak to your existing logic:
{if
'{open_time format="%H%i"}' <= '{current_time format="%H%i"}' &&
'{close_time format="%H%i"}' > '{current_time format="%H%i"}' &&
'{closed_all_day}' != 'y'
}
We're currently open!
{if:else}
Then in the {hours_of_operation} loop:
{if closed_all_day != 'y'}
{open_time format="%g:%i%a"} - {close_time format="%g:%i%a"}<br/>
{else}
Closed<br/>
{/if}

Related

ExpressionEngine: Embed and filter

I got "Data/.List" file with content:
<!-- SubList 1 -->
{embed="Element/.ListThumb" e_id="1" e_cat="cat1" e_name="A"}
{embed="Element/.ListThumb" e_id="2" e_cat="cat2" e_name="B"}
{embed="Element/.ListThumb" e_id="3" e_cat="cat3" e_name="C"}
{embed="Element/.ListThumb" e_id="4" e_cat="cat1|cat3" e_name="D"}
<!-- SubList 2 -->
{embed="Element/.ListThumb" e_id="11" e_cat="cat2" e_name="E"}
{embed="Element/.ListThumb" e_id="12" e_cat="cat3" e_name="F"}
{embed="Element/.ListThumb" e_id="13" e_cat="cat1|cat3" e_name="G"}
{embed="Element/.ListThumb" e_id="14" e_cat="cat1|cat2" e_name="H"}
and in index I want to embed and render filtered elements:
{exp:channel:entries channel="categories"}
<section>
{name}
{embed="Data/.List" e_cat="{id}"}
</section>
{/exp:channel:entries}
And I want to achieve:
Category1:
A, D, G, H
Category2:
B, E, H
Category3:
C, D, F, G
I cannot change Data/.List
So how can I achieve this result or with which query should i look for solution (tried: "ee conditional embed", "static embed", "filter embed")
I'm using channel to easily add new categories with "order" field
Channel Category is using id field (eg. "cat1")
Data/.List is like "row file" for me,
so I will write simple script to parse it to file like this
{if embed:cat == "cat1"}
{embed="Element/.ListThumb" e_id="1" e_cat="cat1" e_name="A"}
{embed="Element/.ListThumb" e_id="4" e_cat="cat1|cat3" e_name="D"}
{embed="Element/.ListThumb" e_id="13" e_cat="cat1|cat3" e_name="G"}
...
{/if}
{if embed:cat == "cat2"}
{embed="Element/.ListThumb" e_id="2" e_cat="cat2" e_name="B"}
...
Assuming {id} from index is e_id specified in Data/.List...
{exp:channel:entries channel="categories"}
<section>
{name}
{if id == 1}
{embed="Element/.ListThumb" e_id="{id}" e_cat="cat{id}" e_name="A"}
{if:elseif id == 2}
{embed="Element/.ListThumb" e_id="{id}" e_cat="cat{id}" e_name="B"}
{if:elseif id == 3}
{embed="Element/.ListThumb" e_id="{id}" e_cat="cat{id}" e_name="C"}
{if:elseif id == 4}
{embed="Element/.ListThumb" e_id="{id}" e_cat="cat1|cat3" e_name="D"}
{if:elseif id == 11}
{embed="Element/.ListThumb" e_id="{id}" e_cat="cat2" e_name="E"}
{if:elseif id == 12}
{embed="Core/.ListThumb" e_id="{id}" e_cat="cat3" e_name="F"}
{if:elseif id == 13}
{embed="Core/.ListThumb" e_id="{id}" e_cat="cat1|cat3" e_name="G"}
{if:elseif id == 14}
{embed="Core/.ListThumb" e_id="{id}" e_cat="cat1|cat2" e_name="H"}
{/if}
</section>
{/exp:channel:entries}
Thus bypassing the template you mentioned can't be changed because it's impossible to deliver what you want without avoid it or changing it.
If this answer is still way off your requirement, we need more information as to what is the end objective and data structure as I think the current approach is the problem, as there are too many fundamental questions with it...
Why not use relationships?
Why have you got a channel called "categories" instead of using native categories?
You have a channel called categories, but then you're hard coding the e_cat in the template - surely this value should be part of the categories data?
Why can't you change Data/.List? This is the template that's causing all the issues!
Is the intention to use different template groups with Element and Core, or is that a mistake?

How do you check if a file description is empty or not in Expressionengine 2?

I am using Expressionengine 2. I would like to check to see if the file description is not empty so that if there is any content it will be used instead of the entry title in the alt tag.
Currently I have the code below which brings in the entry title but I can't work out how I get the logic in place to check the existence of descriptive text for the file (image):
<img class="aClass" src="{work_detail_extra_image_01}"
alt="
{if work_detail_extra_image_01:description != ""}
{work_detail_extra_image_01}{description}{/work_detail_extra_image_01}
{if:else}
{title}
{/if}
"
border="0" />
Thanks for taking a look
Try removing != "" so that line becomes:
{if work_detail_extra_image_01:description}.
It'll see if it exists or not.
I'd try to display the description there first without conditionals to make sure they aren't your problem.
Try this:
<img class="aClass" src="{work_detail_extra_image_01}"
alt="{work_detail_extra_image_01}
{if work_detail_extra_image_01:description != ""}
{work_detail_extra_image_01:description}
{if:else}
{title}
{/if}
{/work_detail_extra_image_01}
"
border="0" />

How can I filter search results by category in ExpressionEngine 1.6.8

I have inherited search results template in an EE 1.6.8 install and would like to filter the results by category. This is possible to a point but the paged results are incomplete i.e display an inconsistent no. of results. I believe this is because the template is doing n+1 queries (bad enough in the first place) in a nested query via a custom localisation module (However, if I remove that then my content is not localised)
{exp:search:search_results disable="member_data|trackbacks" orderby"date" paginate="bottom" limit="10" show_expired="no" show_future_entries="no" }
{count}
{exp:localisation:local_query local_sql_url_title="content-from-url-title" page_url_title="{url_title}" }
{categories}
{if category_group ==1}
{if category_name == "Videos"}
<p>
...
</p>
{if:elseif category_name == "audio"}
<p>
...
{if:elseif category_name == "Images"}
...
{if:elseif weblog_short_name == "gpress-releases" }
...
{if:elseif weblog_short_name == "articles" }
...
{if:elseif weblog_short_name == "press-kits" }
...
{/if}
{/if}
{/categories}
{/exp:localisation:local_query }
{/exp:search:search_results}
Does anyone know how I can filter search results by category when an article might be assigned to more than one category (in another group)? I need to display slightly different content e.g. a different icon based on the category name.
Upgrading to EE 2.x is not an option at this time.
Why not just upload your category icon as the category image for each category? Your template will be much cleaner and simpler:
{exp:search:search_results disable="member_data|trackbacks" orderby"date" paginate="bottom" limit="10" show_expired="no" show_future_entries="no"}
{count}
{exp:localisation:local_query local_sql_url_title="content-from-url-title" page_url_title="{url_title}" }
{categories}<img src="{category_image}" alt="{category_name}" class="category-icon" />{/categories}
{/exp:localisation:local_query }
{/exp:search:search_results}`

ExpressionEngine - PHP in template, Print something if entry has certain field value?

I have a template that is used for entries. The entries have a field that will always have 1 of 2 values. Can I write some PHP to show something different depending on the field value?
Ive tried the following but it gives me a PHP error:
<?php if($my_field == 'value1') { ?>
<h3>Value 1</h3>
<?php } else { ?>
<h3>Value 2</h3>
<?php } ?>
Thanks
You don't have to use PHP.
{if my_field == 'foo'}
Value 1
{if:else}
Value 2
{/if}
If you're planning on doing any EE tag processing within those conditionals, you shouldn't use the if:else syntax, as with it, the content between the conditionals will always be parsed, but just not displayed, which needlessly uses server resources and increases load time.
So in that case, use simple conditionals instead:
{if my_field == 'foo'}
Value 1
{/if}
{if my_field == 'bar'}
Value 2
{/if}
See: http://expressionengine.com/user_guide/templates/globals/conditionals.html
You can use ExpressionEngine's Conditional Global Variables to display your content, without having to use PHP in your templates.
Rewriting your example using native ExpressionEngine's Simple Conditional tags would result in the following:
{exp:channel:entries channel="channel_name" dynamic="off"}
{if "my_field" == "value1"}
Value 1
{/if}
{if "my_field" == "value2"}
Value 2
{/if}
{/exp:channel:entries}
You can use simple or complex conditionals anywhere in your templates, with the former being less resource expensive, but ExpressionEngine's Parse Order (PDF, 32 KB) may affect how they're evaluated and replaced.
In most cases, you'll need to ensure your custom fields and conditionals are within the {exp:channel:entries} tag loop for the values to be properly output and tested when the page is being built.

"no_entries" conditional logic

Is this possible to do the following in ExpressionEngine:
(code taken from here)
IF THERE ARE RELATED ENTRIES SHOW THIS: (important to see the header)
HEADING : Related Entries:
Entry 1
Entry 2
Entry 3
ELSE (SHOW NOTHING)
...
DONE
Code:
{related_entries id="performers"}
{if no_related_entries}
<h2>No Entries</h2> {/if}
<h2>{title}</h2> {body}
{/related_entries}
How do I hide the header? Because the only way to check if there are related entries is to actually start the {related_entries} LOOP.
Any hints? I don't want to hack into PHP for this.
{related_entries id="performers"}
{if title != ""}
<h2>{title}</h2>
{/if}
{body}
{/related_entries}
This should do it, no need for no_related_entries, since you do not plan on doing anything if there is nothing.
Since you have header tags around your title, I imagine you want to avoid printing out header tags when there isn't any related entries.
so if title is not empty, display, if it is, then it won't, so you'll avoid <h2></h2>
don't worry about putting a conditional around the body tag, it will just not display anything if it is blank, but if you put an html tag around it like you did the title, then you would do the same as you do w/ the title conditional.
This ought to do the trick
{related_entries id="performers"}
{if no_related_entries}
<h2>No Entries</h2>
{if:else}
<h2>{title}</h2> {body}
{/if}
{/related_entries}
Sam "SammyTheSnake" Penny

Resources