Hey, thanks for taking the time to read! So I'm new to module development and I'm having trouble understanding how to pass data in my module. The module I am making extends process and implements ConfigurableModule.( A lot of the module aspect of the code is copied from other modules.) It's purpose is to download a report that contains a list of people from a permalink and then parse and create a page for each person on the list. That part works fine, if I just run it as a script or hard code the default values into my module.
What I am having a hard time understanding is how do I access the fields that are in the module via the admin page interface? I can't seem to actually get a hold of the data that is saved in the two fields of my module. I have my module show up under the setup tab with a submit button and the module itself with the two configurable fields are under site->modules. When I press submit I need it to get the data from the two fields.
Is the best way to create global variables in my module?(Which I have tried) Or is there some other preferred way. I really am stumped on how to get it working.
My Module:
<?php
class MemberImporter extends Process implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'LGL Members Importer',
'author' => 'Tyler Williams, Sam Fleming',
'summary' => 'Import a list of LGL members who have an active website.',
'version' => .001,
'singular' => true,
);
}
//Get the module config data
protected $data;
/**
* Name used for the page created in the admin
*
*/
const adminPageName = 'member-importer';
/**
* Instance of Template, used for imported pages
*
*/
protected $template = null;
/**
* Instance of Page, representing the parent Page for imported pages
*
*/
protected $parent = null;
protected static $defaults = array(
'permalinkURL' => 'link-removed-for-privacy',
'membersPath' => '/Members'
);
protected $link = null;
protected $memPath = null;
static public function getDefaultData() {
return array(
'permalinkURL' => 'link-removed-for-privacy',
'membersPath' => '/Members'
);
}
public function __construct() {
//set defaults
foreach(self::getDefaultData() as $key => $value) {
$this->$key = $value;
}
}
public function init() {
parent::init();
ini_set('auto_detect_line_endings', true);
}
public static function getModuleConfigInputfields(array $data) {
$data = array_merge(self::getDefaultData(), $data);
$fields = new InputfieldWrapper();
$head = new InputfieldMarkup;
$head->label = __('Member Import Configuration');
$head->description = __('Set the permalink to the LGL report file and set the parent page for the Member Profiles');
$fields->add($head);
//permalink url for report
$field = wire('modules')->get('InputfieldText');
$field->attr('name', 'permalinkURL');
$field->attr('value', $data['permalinkURL']);
$field->label = "The permalink URL that downloads the LGL report";
$fields->add($field);
//members parent page
$field = wire('modules')->get('InputfieldPageListSelect');
$field->attr('name', 'membersPath');
$field->attr('value', $data['membersPath']);
$field->label = "Path to Members' profiles";
$fields->add($field);
return $fields;
}
public function getConfig(){
return ($this->get($key)) ? $this->get($key) : self::$defaults[$key];
}
protected function getInstalledPage() {
$admin = $this->pages->get($this->config->adminRootPageID);
$parent = $admin->child("name=setup");
if(!$parent->id) $parent = $admin;
$page = $parent->child("name=" . self::adminPageName);
if(!$page->id) {
$page = new Page();
$page->parent = $parent;
$page->template = $this->templates->get('admin');
$page->name = self::adminPageName;
$page->title = "Member Importer";
$page->process = $this;
$page->sort = $parent->numChildren;
$page->save();
}
return $page;
}
public function ___execute() {
if($this->input->get->import == "Import"){
//run the importer and then redirect back to page tree
$this->ImportMembers();
$this->session->redirect(wire('config')->urls->admin);
}else{
$form = $this->modules->get("InputfieldForm");
$form->method = 'get';
$form->action = './';
$head = new InputfieldMarkup;
$head->label = __('Import Members from Little Green Light');
$head->description = __('Imports members from the LGL Database that have active website URLs. List from LGL is updated daily. Press submit to update the website Members list to match that of the LGL members list.');
$form->add($head);
// add submit button
$field = $this->modules->get("InputfieldButton");
$field->type = 'submit';
$field->value = 'Import';
$field->name = 'import';
$form->add($field);
return $form->render();
}
}
public function ___ImportMembers(){
$this->ClearAllMemberProfiles();
$this->GetMemberList();
}
private function GetMemberList(){
//download the csv report from the permalink
$link = $this->getConfig('permalinkURL');
$f = fopen($this->link, 'r');
$members = stream_get_contents($f);
fclose($f);
$members = str_replace('"','', $members);
$rows = explode("\n", $members);
array_shift($rows);
foreach($rows as $row => $members)
{
$row_data = explode(',', $members);
if(trim($row_data[0]) !=''){
$info[$row]['id'] = trim($row_data[0]);
$info[$row]['name'] = trim($row_data[1]) .' '. trim($row_data[2]);
$info[$row]['website'] = trim($row_data[3]);
}
}
$this->GenerateMemberProfiles($info, $memPath);
}
private function GenerateMemberProfiles($info, $memPath){
foreach($info as $mem)
{
if(!empty($mem['website'])){
$p = new Page();
$p->template = 'member';
$p->parent = $this->getConfig('membersPath');
$p->name = $mem['name'];
$p->title =$mem['name'];
$p->save();
$p->memID = $mem['id'];
$p->memWebsite = $mem['website'];
$p->save();
}
}
}
private function ClearAllMemberProfiles(){
$members = wire('pages')->get('/members')->children();
foreach($members as $mem){
wire('pages')->delete($mem, true);
}
}
public function ___install()
{
wire('modules')->saveModuleConfigData($this, self::getDefaultData());
$p = new Page();
$p->template = $this->templates->get("admin");
$p->parent = $this->pages->get("template=admin, name=setup");
$p->title = $this->_('Member Importer');
$p->name = __CLASS__;
$p->process = $this;
$p->save();
}
public function ___uninstall()
{
$p = $this->pages->get('template=admin, name=' . __CLASS__);
if ($p->id > 0) {
$p->delete();
}
}
}
?>
**Updated to reflect current version