ProductOpener::Display - list, create and save products
ProductOpener::Display
generates the HTML code for the web site and the JSON responses for the API.
Exported variables that are available for other modules.
When the module is loaded (at the start of Apache with mod_perl), we record the modification date of static files like CSS styles an JS code so that we can add a version parameter to the request in order to make sure the browser will not serve an old cached version.
$$request_ref->{scripts} .= <<HTML <script type="text/javascript" src="/js/dist/product-multilingual.js?v=$file_timestamps{"js/dist/product-multilingual.js"}"></script> HTML ;
The files that need to be checked need to be specified in the code of Display.pm.
e.g. %file_timestamps = ( "css/dist/app.css" => "CSS file generated by the 'npm run build' command", "css/dist/product-multilingual.css" => "CSS file generated by the 'npm run build' command", "js/dist/product-multilingual.js" => "JS file generated by the 'npm run build' command", );
Return the localized URL for a text. (e.g. "data" points to /data in English and /donnees in French) Note: This currently only has ecoscore
Add some functions and variables needed by many templates and process the template with template toolkit.
This function instructs mod_perl to print redirect HTTP header (Location) and to terminate the request immediately. The mod_perl process is not terminated and will continue to serve future requests.
The request object may contain a cookie.
e.g. 302 for a temporary redirect
CGI.pm param() function returns a list when called in a list context (e.g. when param() is an argument of a function, or the value of a field in a hash). This causes issues for function signatures that expect a scalar, and that may get passed an empty list if the parameter is not set.
So instead of calling CGI.pm param() directly, we call single_param() to prefix it with scalar.
A scalar value for the parameter, or undef if the parameter is not defined.
Return a request parameter. The parameter can be passed in the query string, as a POST multipart form data parameter, or in a POST JSON body
A scalar value for the parameter, or undef if the parameter is not defined.
init_request()
is called at the start of each new request (web page or API). It initializes a number of variables, in particular:
$cc : country code
$lc : language code
$knowledge_panels_options_ref: Reference to a hashmap that collect options to display knowledge panels for current request See also "knowledge_panels_options_ref" in ProductOpener::KnowledgePanels It also initializes a request object that is returned.
This function may be passed an existing request object reference (e.g. pre-containing some fields of the request, like a JSON body).
If not passed, a new request object will be created.
Reference to request object.
Set two attributes to `request_ref`:
- `user_agent`: the request User-Agent - `is_crawl_bot`: a flag (0 or 1) that indicates whether the request comes from a known web crawler (Google, Bing,...). We only use User-Agent value to set this flag. - `is_denied_crawl_bot`: a flag (0 or 1) that indicates whether the request comes from a web crawler we want to deny access to.
Display an error message using the site template.
The request is not terminated by this function, it will continue to run.
Display an error message using the site template, and terminate the request immediately.
Any code after the call to display_error_and_exit() will not be executed.
Return an empty HTML page with a '<meta name="robots" content="noindex">' directive in the HTML header.
This is useful to prevent web crawlers to overload our servers by querying webpages that require a lot of resources (especially aggregation queries).
Return a page with a 429 status code and a message explaining that the user is sending too many requests.
Return robots.txt page and exit.
robots.txt is dynamically generated based on lc, it's content depends on $request_ref: - if $request_ref->{deny_all_robots_txt} is 1: a robots.txt where we deny all traffic combinations. - otherwise: the standard robots.txt. We disallow indexing of most facet pages, the exceptions can be found in ProductOpener::Config::index_tag_types
This function goes through the tags filters from the request and canonicalizes them. If the requested tags are not canonical, we will redirect to the canonical URL.
Generate a title from the tags in the request.
Title string.
Generate a description for some tag types, like additives, if there is a template set in the Config.pm file.
This feature was coded before the introduction of knowledge panels. It is in maintenance mode, and should be reimplemented as facets knowledge panels (server side, or with client side facets knowledge panels)
This function is called to display either:
1. Products that have a specific tag: /category/cakes or that don't have a specific tag /category/-cakes or that have 2 specific tags /category/cake/brand/oreo 2. List of tags of a given type: /labels possibly for products that have a specific tag: /category/cakes/labels or more specific tags: /category/cakes/label/organic/additives
When displaying products for a tag, the function generates tag type specific HTML that is displayed at the top of the page: - tag parents and children - maps for tag types that have a location (e.g. packaging codes) - special properties for some tag types (e.g. additives)
The function then calls search_and_display_products() to display the paginated list of products.
When displaying a list of tags, the function calls display_list_of_tags().
Return a reference to a hash of all request parameters (CGI params and JSON body) name and values.
This function builds the HTML returned by the /search endpoint.
The results can be displayed in different ways:
1. a paginated list of products (default) The function calls search_and_display_products() to display the paginated list of products.
2. results filtered and ranked on the client-side 2.1. according to user preferences that are locally saved on the client: &user_preferences=1 2.2. according to preferences passed in the url: &preferences=..
3. on a graph (histogram or scatter plot): &graph=1 -- TODO: not supported yet
4. on a map &map=1 -- TODO: not supported yet
This function looks at the request object to set parameters to pass to the get_products_collection() function.
An optional reference to a hash of parameters that should be added to the parameters extracted from the request object.
A reference to a parameters object that can be passed to get_products_collection()
This function is used to parse search query parameters that are passed to the API (/api/v?/search endpoint) or to the web site search (/search endpoint) either as query string parameters (e.g. ?labels_tags=en:organic), POST parameters, or POST JSON body parameters.
The function then adds the corresponding query filters in the MongoDB query.
It also adds the country and owner filters to the query.
Reference to the request object.
Reference to the MongoDB query object.
This function is used to parse search query parameters that are passed to the API (/api/v?/search endpoint) or to the web site search (/search endpoint) either as query string parameters (e.g. ?labels_tags=en:organic), POST parameters, or POST JSON body parameters.
The function then adds the corresponding query filters in the MongoDB query.
Reference to a hash of parameters (name and value).
Reference to the MongoDB query object.
Search products and return an HTML snippet that should be included in the webpage.
Reference to the internal request object.
Reference to the MongoDB query object.
A string indicating how to sort results (created_t, popularity,...), or a sorting subroutine.
Limit of the number of products to return.
Requested page (first page starts at 1).
This function is used for page navigation and gets called when there is more than one page of products. The URL can be different, either page=<number> , or /<number> . page=<number> is used for search queries. /<number> is used for facets.
Called by search_and_graph_products() to display a scatter plot of products on 2 axis
Options for the graph, set by /cgi/search.pl
List of search results from search_and_graph_products()
Called by search_and_graph_products() to display an histogram of products on 1 axis
Options for the graph, set by /cgi/search.pl
List of search results from search_and_graph_products()
Transform a traceability code (emb code) into a latitude / longitude pair.
We try using packagers_codes taxonomy, or fsa_rating or geocode for uk, or city.
The traceability code
(latitude, longitude) if found, or (undef, undef) otherwise
Build the HTML to display a map of products
Must return a reference to a function that on each call return a product, or undef to end iteration
Specifications for the graph
Build the MongoDB query corresponding to a search to display a map
Base query that will be modified to be able to build the map
Trigger a search and build a map
Base query for this search
Specification of the graph
add a permalink to a search result page
Display an explanation of the possible improvement, using the improvement data stored in $product_ref->{improvements_data}
Warning: This function only works with Nutri-Score 2021.
Generates HTML code with information on how the Nutri-Score was computed for a particular product.
For each component of the Nutri-Score (energy, sugars etc.) it shows the input value, the rounded value according to the Nutri-Score rules, and the corresponding points.
Generates a data structure to display the nutrient levels (food traffic lights).
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
Reference to a data structure with needed data to display.
Generates a data structure to display the Nutri-Score.
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
Reference to a data structure with needed data to display.
Compares a product nutrition facts to average nutrition facts of each of its categories.
If defined, we will limit the number of categories returned, and keep the most specific categories.
Reference to a comparisons data structure that can be passed to the data_to_display_nutrition_table() function.
Generates a data structure to display a nutrition table.
The nutrition table can be the nutrition table of a product, or of a category (stats for the categories).
In the case of a product, extra columns can be added to compare the product nutrition facts to the average for its categories.
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
Reference to an array with nutrition facts for 1 or more categories.
Reference to a data structure with needed data to display.
Generates HTML to display a nutrition table.
Use data produced by data_to_display_nutrition_table
Reference to an array with nutrition facts for 1 or more categories.
HTML for the nutrition table.
Return a JSON structure with all available preference values for attributes.
This is used by clients that ask for user preferences to personalize filtering and ranking based on product attributes.
Sets the desired language for the user facing strings.
Return a JSON structure with all available attribute groups and attributes, with strings (names, descriptions etc.) in a specific language, and return them in an array of attribute groups.
This is used in particular for clients of the API to know which preferences they can ask users for, and then use for personalized filtering and ranking.
Returned attributes contain both data and strings intended to be displayed to users. This parameter sets the desired language for the user facing strings.
Generate an extract of a taxonomy for specific tags, fields and languages, and return it as a JSON object.
Accessed through the /api/v2/taxonomy API
e.g. https://world.openfoodfacts.org/api/v2/taxonomy?type=labels&tags=en:organic,en:fair-trade&fields=name,description,children&include_children=1&lc=en,fr
Return product data in JSON format.
This function is used only for api v0, v1 and v2. API v3 + uses APIProductRead.pm
Displays icons (e.g., the camera icon "Picture with barcode", the graph and maps button, etc)
Recursive function to display how the ingredients were analyzed. This function calls itself to display sub-ingredients of ingredients.
Reference to the product's ingredients array or the ingredients array of an ingredient.
Reference to a list of ingredients in text format that we will reconstruct from the ingredients array.
Reference to an HTML list of ingredients in ordered nested list format that corresponds to the ingredients array.
Generate HTML to display how the specific ingredients (e.g. mentions like "Total milk content: 90%") were analyzed.
Empty string if no specific ingredients were detected, or HTML describing the specific ingredients.
Generates a data structure to display the details of ingredients analysis.
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
Reference to a data structure with needed data to display.
Generates HTML code with information on how the ingredient list was parsed and mapped to the ingredients taxonomy.
Generates a data structure to display the results of ingredients analysis.
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
Reference to a data structure with needed data to display.
Generates HTML code with icons that show if the product is vegetarian, vegan and without palm oil.
Generates HTML code with information on how the Eco-score was computed for a particular product.
Generates simple HTML code (to display in a mobile app) with information on how the Eco-score was computed for a particular product.
Analyze the distribution of selected parent ingredients in the searched products
Load the Folksonomy Engine properties script
Generates a data structure to display a product image.
The resulting data structure can be passed to a template to generate HTML or the JSON data for a knowledge panel.
- Reference to a data structure with needed data to display. - undef if no image is available for the requested image type
Returns the pretty path for the organization page or an empty string if not on the producers platform.
/org/[orgid]