cms - adamjimenez/shiftlib GitHub Wiki

CMS Class

Access data from the GenieAdmin

get

array $cms->get( string $section, mixed $conditions=NULL, int $num_results=NULL, string $order=NULL, bool $asc=true, string $prefix=null )

This is used for pulling content from the CMS example:

Example #1 Retrieve all items

This will retrieve all items from the cms section called "news" order by date (newest first).

$news_items = $cms->get('news', null, null, 'date', false);

Example #2 Retrieve specific item

This will retrieve the item where ID = 1

$news_item = $cms->get('news', 1);

Which is equivalent to:

$news_item = $cms->get('news', ['id'=> 1]);

Example #3 Retrieve by page name

This will retrieve the item where page name = "latest-news"

$news_item = $cms->get('news', 'latest-news');

Example #4 Search

This will retrieve all items where "display = true"

$news_items = $cms->get('news', [display => true]);

Example #5 Retrieve by date

This will retrieve all items from the last 30 days

$news_items = $cms->get('news', [
    'date' => '-30 days',
    'func' => ['date' => '>']
]);

set_section

$cms->set_section(string $section, int $id=null, array $editable_fields );

This sets the current section - this is required before displaying fields or saving data.

Example #1 form submission

This will create a form, validate and submit.

Fields are validated if their required checkbox is ticked in the configure section.

Contact form

<?php
//set cms section for fields and validation
$cms->set_section('enquiries');

//handle form submission
if(isset($_POST['save'])) {
    $cms->submit(true); //true to send an email notification to admin
}
?>

<h1>Contact form</h1>
<?php if($cms->saved) { ?>
<p>Thanks, we will be in touch..</p>
<?php } else { ?>
<form method="post" class="validate">
    <input type="hidden" name="save" value="1">
    <p><?=$cms->get_field('name', 'placeholder="your name"');?></p>
    <p><?=$cms->get_field('email', 'placeholder="your email"');?></p>
    <p><?=$cms->get_field('enquiry', 'placeholder="your enquiry"');?></p>
    <button type="submit">Submit</button>
</form>
<?php } ?>

<?php
//load jquery and cms.js for validation
load_js('cms');
?>

recaptchav3

In order to use recaptcha v3 you will need to do the following:

  1. add the recaptcha keys to the configure area.
  2. load the recaptcha script e.g.
<?php load_js(['cms', 'recaptchav3']); ?>
  1. enable the recaptchav3 parameter
$cms->submit(['recaptchav3'=>true, 'notify'=>true]);
  1. In order to record the spam score, the section should include a decimal field called "score".

get_field

$cms->get_field( string $name, string $attribs='' );

This outputs a form field like a text input box or a rich text area.
$attribs are added into the field so that you can add class names and other attributes.

save

$cms->save();

Takes post data and uses it to save to the currently set section.
If an item is selected it will be updated - otherwise, a new item will be created.

delete_items

$cms->delete_items( string $section, mixed $items );

Deletes a single id or an array of ids from the provided CMS section.

⚠️ **GitHub.com Fallback** ⚠️