Hello Xibo Community,
i created a custom module to make a API request. When I add the module and want to edit the required form fields it doesnt save them. Also the added module (which works fine with hardcoded values) can’t be edited. It doesn’t save the data. I duplicates the module in the module overfiew.
here is my code:
<?php
class CAB extends Module
{
public function __construct(database $db, user $user, $mediaid = '', $layoutid = '', $regionid = '', $lkid = '') {
// The Module Type must be set - this should be a unique text string of no more than 50 characters.
// It is used to uniquely identify the module globally.
$this->type = 'cab';
// This is the code schema version, it should be 1 for a new module and should be incremented each time the
// module data structure changes.
// It is used to install / update your module and to put updated modules down to the display clients.
$this->codeSchemaVersion = 1;
// Must call the parent class
parent::__construct($db, $user, $mediaid, $layoutid, $regionid, $lkid);
}
public function InstallFiles() {
$media = new Media();
$media->addModuleFile('modules/preview/vendor/jquery-1.11.1.min.js');
$media->addModuleFile('modules/preview/xibo-layout-scaler.js');
}
/**
* Install or Update this module
*/
public function InstallOrUpdate() {
// This function should update the `module` table with information about your module.
// The current version of the module in the database can be obtained in $this->schemaVersion
// The current version of this code can be obtained in $this->codeSchemaVersion
// $settings will be made available to all instances of your module in $this->settings. These are global settings to your module,
// not instance specific (i.e. not settings specific to the layout you are adding the module to).
// $settings will be collected from the Administration -> Modules CMS page.
//
// Layout specific settings should be managed with $this->SetOption in your add / edit forms.
$name = 'Name';
$description = 'Description';
$imageUri = 'forms/library.gif';
$previewEnabled = 1;
$assignable = 1;
if ($this->schemaVersion <= 1) {
// Install
$this->InstallModule($name, $description, $imageUri, $previewEnabled, $assignable, $settings);
}
else {
// Update
// Call "$this->UpdateModule($name, $description, $imageUri, $previewEnabled, $assignable, $settings)" with the updated items
}
// Check we are all installed
$this->InstallFiles();
// After calling either Install or Update your code schema version will match the database schema version and this method will not be called
// again. This means that if you want to change those fields in an update to your module, you will need to increment your codeSchemaVersion.
}
/**
* Form for updating the module settings
*/
public function ModuleSettingsForm() {
// Output any form fields (formatted via a Theme file)
// These are appended to the bottom of the "Edit" form in Module Administration
$formFields[] = FormManager::AddText('apiKey', __('API Key'), $this->GetSetting('apiKey'),
__('Enter your API Key from Forecast IO.'), 'a', 'required');
return $formFields;
}
/**
* Process any module settings
*/
public function ModuleSettings() {
// Process any module settings you asked for.
$apiKey = Kit::GetParam('apiKey', _POST, _STRING, '');
if ($apiKey == '')
$this->ThrowError(__('Missing API Key'));
$this->settings['apiKey'] = $apiKey;
// Return an array of the processed settings.
return $this->settings;
}
/*
public function loadTemplates()
{
// Scan the folder for template files
foreach (glob('modules/theme/module/*.template.json') as $template) {
// Read the contents, json_decode and add to the array
$this->settings['templates'][] = json_decode(file_get_contents($template), true);
}
Debug::Audit(count($this->settings['templates']));
}
*/
/**
* Return the Add Form as HTML
* @return
*/
public function AddForm()
{
// var_dump($this); die();
$this->response = new ResponseManager();
// This is the logged in user and can be used to assess permissions
$user =& $this->user;
// All modules will have:
// $this->layoutid
// $this->regionid
// You also have access to $settings, which is the array of settings you configured for your module.
// The CMS provides the region width and height in case they are needed
$rWidth = Kit::GetParam('rWidth', _REQUEST, _STRING);
$rHeight = Kit::GetParam('rHeight', _REQUEST, _STRING);
// All forms should set some meta data about the form.
// Usually, you would want this meta data to remain the same.
Theme::Set('form_id', 'ModuleForm');
Theme::Set('form_action', 'index.php?p=module&mod=' . $this->type . '&q=Exec&method=AddMedia');
Theme::Set('form_meta', '<input type="hidden" name="layoutid" value="' . $this->layoutid . '"><input type="hidden" id="iRegionId" name="regionid" value="' . $this->regionid . '"><input type="hidden" name="showRegionOptions" value="' . $this->showRegionOptions . '" />');
// Any values for the form fields should be added to the theme here.
// Tabs
$tabs = array();
$tabs[] = FormManager::AddTab('general', __('General'));
$tabs[] = FormManager::AddTab('advanced', __('Coordinates'));
Theme::Set('form_tabs', $tabs);
$formFields['general'][] = FormManager::AddText('name', __('Name'), NULL,
__('Name this module (optional)'), 'n');
$formFields['general'][] = FormManager::AddNumber('duration', __('Duration'), NULL,
__('The duration in seconds this item should be displayed.'), 'd', 'required');
$formFields['advanced'][] = FormManager::AddText('latitude', __('Latitude'), NULL,
__('Specify the Latitude of this module'), 'la');
$formFields['advanced'][] = FormManager::AddText('longitude', __('Longitude'), NULL,
__('Specify the Longitude of this module'), 'lo');
$formFields['advanced'][] = FormManager::AddText('radius', __('Radius'), NULL,
__('Specify the Search radius of this module'), 'r');
$formFields['advanced'][] = FormManager::AddCombo('providernetwork', __('Providernetwork'), $this->GetOption('providernetwork'),
$this->providernetworks(),
'id',
'value',
__('Select your desired network.'), 'ne');
// Modules should be rendered using the theme engine.
Theme::Set('form_fields_general', $formFields['general']);
Theme::Set('form_fields_advanced', $formFields['advanced']);
$this->response->html = Theme::RenderReturn('form_render');
// Any JavaScript call backs should be set (you can use text_callback to set up a text editor should you need one)
$this->response->callBack = 'testFormSetup';
$this->response->dialogTitle = __('MOdule nAME');
// You can have a bigger form
//$this->response->dialogClass = 'modal-big';
// The response object outputs the required JSON object to the browser
// which is then processed by the CMS JavaScript library (xibo-cms.js).
$this->response->AddButton(__('Cancel'), 'XiboDialogClose()');
$this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');
// The response must be returned.
return $this->response;
}
/**
* Add Media to the Database
* @return
*/
public function AddMedia()
{
$this->response = new ResponseManager();
// Same member variables as the Form call, except with POST variables for your form fields.
$layoutid = $this->layoutid;
$regionid = $this->regionid;
$mediaid = $this->mediaid;
// You are required to set a media id, which should be unique.
$this->mediaid = md5(uniqid());
// You must also provide a duration (all media items must provide this field)
$this->duration = Kit::GetParam('duration', _POST, _INT, 0, false);
// You should validate all form input using the Kit::GetParam helper classes
Kit::GetParam('duration', _POST, _INT, 0, false);
// You should also validate that fields are set to your liking
/*
* if ($text == '')
* {
* $this->response->SetError('Please enter some text');
* $this->response->keepOpen = true;
* return $this->response;
* }
*/
// You can store any additional options for your module using the SetOption method
$this->SetOption('latitude', Kit::GetParam('latitude', _POST, _DOUBLE));
$this->SetOption('longitude', Kit::GetParam('longitude', _POST, _DOUBLE));
$this->SetOption('radius', Kit::GetParam('radius', _POST, _INT));
$this->SetOption('providernetwork', Kit::GetParam('providernetwork', _POST, _INT));
// You may also store raw XML/HTML using SetRaw. You should provide a containing node (in this example: <text>)
$this->SetRaw('<text><![CDATA[' . $text . ']]></text>');
// Should have built the media object entirely by this time
// This saves the Media Object to the Region
$this->UpdateRegion();
// Usually you will want to load the region options form again once you have added your module.
// In some cases you will want to load the edit form for that module
$this->response->loadForm = true;
$this->response->loadFormUri = "index.php?p=timeline&layoutid=$this->layoutid®ionid=$this->regionid&q=RegionOptions";
//$this->response->loadFormUri = "index.php?p=module&mod=$this->type&q=Exec&method=EditForm&layoutid=$this->layoutid®ionid=$regionid&mediaid=$this->mediaid";
return $this->response;
}
/**
* Return the Edit Form as HTML
* @return
*/
public function EditForm()
{
$this->response = new ResponseManager();
// Edit forms are the same as add forms, except you will have the $this->mediaid member variable available for use.
if (!$this->auth->edit)
{
$this->response->SetError('You do not have permission to edit this assignment.');
$this->response->keepOpen = false;
return $this->response;
}
// All forms should set some meta data about the form.
// Usually, you would want this meta data to remain the same.
Theme::Set('form_id', 'ModuleForm');
Theme::Set('form_action', 'index.php?p=module&mod=' . $this->type . '&q=Exec&method=AddMedia');
Theme::Set('form_meta', '<input type="hidden" name="layoutid" value="' . $this->layoutid . '"><input type="hidden" id="iRegionId" name="regionid" value="' . $this->regionid . '"><input type="hidden" name="showRegionOptions" value="' . $this->showRegionOptions . '" />');
$tabs = array();
$tabs[] = FormManager::AddTab('general', __('General'));
$tabs[] = FormManager::AddTab('advanced', __('Coordinates'));
Theme::Set('form_tabs', $tabs);
$formFields['general'][] = FormManager::AddText('name', __('Name'), NULL,
__('Name this module (otional)'), 'n');
$formFields['general'][] = FormManager::AddNumber('duration', __('Duration'), NULL,
__('The duration in seconds this item should be displayed.'), 'd', 'required');
$formFields['advanced'][] = FormManager::AddText('latitude', __('Latitude'), NULL,
__('Specify the Latitude of this module'), 'la');
$formFields['advanced'][] = FormManager::AddText('longitude', __('Longitude'), NULL,
__('Specify the Longitude of this module'), 'lo');
$formFields['advanced'][] = FormManager::AddText('radius', __('Radius'), NULL,
__('Specify the Search radius of this module'), 'r');
$formFields['advanced'][] = FormManager::AddCombo('providernetwork', __('Providernetwork'), $this->GetOption('providernetwork'),
$this->providernetworks(),
'id',
'value',
__('Select your desired network.'), 'ne');
// Dependencies (some fields should be shown / hidden)
// $this->SetFieldDependencies();
// Modules should be rendered using the theme engine.
Theme::Set('form_fields_general', $formFields['general']);
Theme::Set('form_fields_advanced', $formFields['advanced']);
$this->response->html = Theme::RenderReturn('form_render');
// Any JavaScript call backs should be set (you can use text_callback to set up a text editor should you need one)
$this->response->callBack = 'flinksterFormSetup';
$this->response->dialogTitle = __('CallABike');
// You can have a bigger form
//$this->response->dialogClass = 'modal-big';
// The response object outputs the required JSON object to the browser
// which is then processed by the CMS JavaScript library (xibo-cms.js).
$this->response->AddButton(__('Cancel'), 'XiboDialogClose()');
$this->response->AddButton(__('Save'), '$("#ModuleForm").submit()');
// The response must be returned.
return $this->response;
}
/**
* Edit Media in the Database
* @return
*/
public function EditMedia()
{
$this->response = new ResponseManager();
// Edit calls are the same as add calls, except you will to check the user has permissions to do the edit
if (!$this->auth->edit)
{
$this->response->SetError('You do not have permission to edit this assignment.');
$this->response->keepOpen = false;
return $this->response;
}
// You can store any additional options for your module using the SetOption method
$this->SetOption('latitude', Kit::GetParam('latitude', _POST, _DOUBLE));
$this->SetOption('longitude', Kit::GetParam('longitude', _POST, _DOUBLE));
$this->SetOption('radius', Kit::GetParam('radius', _POST, _INT));
$this->SetOption('providernetwork', Kit::GetParam('providernetwork', _POST, _INT));
// You may also store raw XML/HTML using SetRaw. You should provide a containing node (in this example: <text>)
$this->SetRaw('<text><![CDATA[' . $text . ']]></text>');
// Should have built the media object entirely by this time
// This saves the Media Object to the Region
$this->UpdateRegion();
// Usually you will want to load the region options form again once you have added your module.
// In some cases you will want to load the edit form for that module
if ($this->showRegionOptions) {
$this->response->callBack = 'refreshPreview("' . $this->regionid . '")';
$this->response->loadForm = true;
$this->response->loadFormUri = "index.php?p=timeline&layoutid=$this->layoutid®ionid=$this->regionid&q=RegionOptions";
}
// Usually you will want to load the region options form again once you have added your module.
// In some cases you will want to load the edit form for that module
$this->response->loadForm = true;
$this->response->loadFormUri = "index.php?p=timeline&layoutid=$this->layoutid®ionid=$this->regionid&q=RegionOptions";
//$this->response->loadFormUri = "index.php?p=module&mod=$this->type&q=Exec&method=EditForm&layoutid=$this->layoutid®ionid=$regionid&mediaid=$this->mediaid";
return $this->response;
}
/**
* GetResource
* Return the rendered resource to be used by the client (or a preview)
* for displaying this content.
* @param integer $displayId If this comes from a real client, this will be the display id.
*/
public function GetResource($displayId = 0)
{
// Behave exactly like the client.
// A template is provided which contains a number of different libraries that might
// be useful (jQuery, etc).
// You can provide your own template, or just output the HTML directly in this method. It is up to you.
//$template = file_get_contents('modules/preview/HtmlTemplateSimple.html');
// Behave exactly like the client.
$isPreview = (Kit::GetParam('preview', _REQUEST, _WORD, 'false') == 'true');
// Load in the template
$template = file_get_contents('modules/preview/HtmlTemplateForCAB.html');
// Replace the View Port Width?
if (isset($_GET['preview']))
$template = str_replace('[[ViewPortWidth]]', $this->width, $template);
// Get the text out of RAW
$rawXml = new DOMDocument();
$rawXml->loadXML($this->GetRaw());
// HERE GOES API HANDLING
// Return that content.
return $template;
}
public function HoverPreview()
{
// Default Hover window contains a thumbnail, media type and duration
$output = parent::HoverPreview();
// You can add anything you like to this, or completely replace it
return $output;
}
public function IsValid() {
// Using the information you have in your module calculate whether it is valid or not.
// 0 = Invalid
// 1 = Valid
// 2 = Unknown
return 1;
}
private function providernetworks()
{
return array(
array('id' => '1', 'value' => 'network1'),
array('id' => '2', 'value' => 'network2'),
array('id' => '3', 'value' => 'network3'),
);
}
private function parseLibraryReferences($isPreview, $content)
{
$parsedContent = $content;
$matches = '';
preg_match_all('/\[.*?\]/', $content, $matches);
foreach ($matches[0] as $sub) {
// Parse out the mediaId
$mediaId = str_replace(']', '', str_replace('[', '', $sub));
// Only proceed if the content is actually an ID
if (!is_numeric($mediaId))
continue;
// Check that this mediaId exists and get some information about it
$entry = Media::Entries(null, array('mediaId' => $mediaId));
if (count($entry) <= 0)
continue;
// We have a valid mediaId to substitute
$replace = ($isPreview) ? 'index.php?p=module&mod=image&q=Exec&method=GetResource&mediaid=' . $entry[0]->mediaId : $entry[0]->storedAs;
// Substitute the replacement we have found (it might be '')
$parsedContent = str_replace($sub, $replace, $parsedContent);
}
return $parsedContent;
}
}
?>
I hope you can help me with this problem.