/** * @param InputInterface $input * @param OutputInterface $output * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { parent::bootstrapProcessWire($output); $name = $input->getArgument('name'); $fields = explode(",", $input->getOption('fields')); if (wire("templates")->get("{$name}")) { $output->writeln("<error>Template '{$name}' already exists!</error>"); exit(1); } $fieldgroup = new \Fieldgroup(); $fieldgroup->name = $name; $fieldgroup->add("title"); if ($input->getOption('fields')) { foreach ($fields as $field) { $this->checkIfFieldExists($field, $output); $fieldgroup->add($field); } } $fieldgroup->save(); $template = new \Template(); $template->name = $name; $template->fieldgroup = $fieldgroup; $template->save(); if (!$input->getOption('nofile')) { $this->createTemplateFile($name); } $output->writeln("<info>Template '{$name}' created successfully!</info>"); }
public function getCMSFields() { $datetimeField = DatetimeField::create("Date")->setTitle($this->fieldLabel("Date")); $datetimeField->getDateField()->setConfig("dmyfields", true); // Check if NewsImage should be saved in a seperate folder if (self::config()->save_image_in_seperate_folder == false) { $UploadField = UploadField::create("NewsImage")->setTitle($this->fieldLabel("NewsImage"))->setFolderName("news"); } else { if ($this->ID == "0") { $UploadField = FieldGroup::create(LiteralField::create("Save", $this->fieldLabel("SaveHelp")))->setTitle($this->fieldLabel("NewsImage")); } else { $UploadField = UploadField::create("NewsImage")->setTitle($this->fieldLabel("NewsImage"))->setFolderName("news/" . $this->URLSegment); } } // Create direct link to NewsArticle if ($this->ID == "0") { // Little hack to hide $urlsegment when article isn't saved yet. $urlsegment = LiteralField::create("NoURLSegmentYet", ""); } else { if ($NewsHolder = $this->NewsHolder()) { $baseLink = Controller::join_links(Director::absoluteBaseURL(), $NewsHolder->Link(), $this->URLSegment); } $urlsegment = Fieldgroup::create(LiteralField::create("URLSegment", "URLSegment")->setContent('<a href="' . $baseLink . '" target="_blank">' . $baseLink . '</a>'))->setTitle("URLSegment"); } $fields = FieldList::create(new TabSet("Root", new Tab("Main", $urlsegment, TextField::create("Title")->setTitle($this->fieldLabel("Title")), $datetimeField, HTMLEditorField::create("Content")->setTitle($this->fieldLabel("Content")), $UploadField))); $this->extend("updateCMSFields", $fields); return $fields; }
/** * Create a template * Add a fieldgroup with the 'title' field * @todo add all global fields instead */ public function update() { $t = new Template(); $t->name = $this->getTemplateName(); $fg = new Fieldgroup(); $fg->name = $this->getTemplateName(); foreach ($this->fields as $field) { // add global fields if (!($field->flags & Field::flagGlobal)) { continue; } $fg->add($field); } $fg->save(); $t->fieldgroup = $fg; $t->fieldgroup->save(); $t->save(); $this->templateSetup($t); $t->fieldgroup->save(); $t->save(); return $t; }
/** * Save the context of the given field for the given fieldgroup * * @param Field $field Field to save context for * @param Fieldgroup $fieldgroup Context for when field is in this fieldgroup * @return bool True on success * @throws WireException * */ public function ___saveFieldgroupContext(Field $field, Fieldgroup $fieldgroup) { // get field without contxt $fieldOriginal = wire('fields')->get($field->name); $data = array(); // make sure given field and fieldgroup are valid if (!($field->flags & Field::flagFieldgroupContext)) { throw new WireException("Field must be in fieldgroup context before its context can be saved"); } if (!$fieldgroup->has($fieldOriginal)) { throw new WireException("Fieldgroup {$fieldgroup} does not contain field {$field}"); } $newValues = $field->getArray(); $oldValues = $fieldOriginal->getArray(); // 0 is the same as 100 for columnWidth, so we specifically set it just to prevent this from being saved when it doesn't need to be if (!isset($oldValues['columnWidth'])) { $oldValues['columnWidth'] = 100; } // add the label and description built-in fields foreach (array('label', 'description') as $key) { $newValues[$key] = $field->{$key}; $oldValues[$key] = $fieldOriginal->{$key}; } // cycle through and determine which values should be saved foreach ($newValues as $key => $value) { $oldValue = empty($oldValues[$key]) ? '' : $oldValues[$key]; // if both old and new are empty, then don't store a blank value in the context if (empty($oldValue) && empty($value)) { continue; } // if old and new value are the same, then don't duplicate the value in the context if ($value == $oldValue) { continue; } // $value differs from $oldValue and should be saved $data[$key] = $value; } // keep all in the same order so that it's easier to compare (by eye) in the DB ksort($data); // inject updated context back into model $fieldgroup->setFieldContextArray($field->id, $data); // if there is something in data, then JSON encode it. If it's empty then make it null. $data = count($data) ? wireEncodeJSON($data, true) : null; if (is_null($data)) { $data = 'NULL'; } else { $data = "'" . $this->wire('database')->escapeStr($data) . "'"; } $field_id = (int) $field->id; $fieldgroup_id = (int) $fieldgroup->id; $database = $this->wire('database'); $query = $database->prepare("UPDATE fieldgroups_fields SET data={$data} WHERE fields_id=:field_id AND fieldgroups_id=:fieldgroup_id"); // QA $query->bindValue(':field_id', $field_id, PDO::PARAM_INT); $query->bindValue(':fieldgroup_id', $fieldgroup_id, PDO::PARAM_INT); $result = $query->execute(); return $result; }
/** * Install all the required fields, templates etc * * @access public * */ public function ___install() { // Pin Topics / Comments Permission if (!$this->fields->get('hbb_p_pin')) { $field_topic_pin = new Field(); $field_topic_pin->type = $this->modules->get("FieldtypePage"); $field_topic_pin->name = 'hbb_p_pin'; $field_topic_pin->label = 'Pin Topics / Comments'; $field_topic_pin->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_topic_pin->labelFieldName = 'name'; $field_topic_pin->inputfield = 'InputfieldCheckboxes'; $field_topic_pin->optionColumns = 1; $field_topic_pin->tags = 'HermodBB'; $field_topic_pin->icon = 'thumb-tack'; $field_topic_pin->save(); } // Lock Topics Permission if (!$this->fields->get('hbb_p_topic_lock')) { $field_topic_lock = new Field(); $field_topic_lock->type = $this->modules->get("FieldtypePage"); $field_topic_lock->name = 'hbb_p_topic_lock'; $field_topic_lock->label = 'Lock Topics'; $field_topic_lock->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_topic_lock->labelFieldName = 'name'; $field_topic_lock->inputfield = 'InputfieldCheckboxes'; $field_topic_lock->optionColumns = 1; $field_topic_lock->tags = 'HermodBB'; $field_topic_lock->icon = 'lock'; $field_topic_lock->save(); } // View Forum Permission if (!$this->fields->get('hbb_p_forum_view')) { $field_forum_view = new Field(); $field_forum_view->type = $this->modules->get("FieldtypePage"); $field_forum_view->name = 'hbb_p_forum_view'; $field_forum_view->label = 'View Forum & Topics'; $field_forum_view->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_forum_view->labelFieldName = 'name'; $field_forum_view->inputfield = 'InputfieldCheckboxes'; $field_forum_view->optionColumns = 1; $field_forum_view->tags = 'HermodBB'; $field_forum_view->icon = 'eye'; $field_forum_view->save(); } // Start Topics Permission if (!$this->fields->get('hbb_p_topic_start')) { $field_topic_post = new Field(); $field_topic_post->type = $this->modules->get("FieldtypePage"); $field_topic_post->name = 'hbb_p_topic_start'; $field_topic_post->label = 'Start Topics'; $field_topic_post->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_topic_post->labelFieldName = 'name'; $field_topic_post->inputfield = 'InputfieldCheckboxes'; $field_topic_post->optionColumns = 1; $field_topic_post->tags = 'HermodBB'; $field_topic_post->icon = 'file'; $field_topic_post->save(); } // View Comments Permission if (!$this->fields->get('hbb_p_comments_view')) { $field_comments_view = new Field(); $field_comments_view->type = $this->modules->get("FieldtypePage"); $field_comments_view->name = 'hbb_p_comments_view'; $field_comments_view->label = 'View Comments (replies)'; $field_comments_view->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_comments_view->labelFieldName = 'name'; $field_comments_view->inputfield = 'InputfieldCheckboxes'; $field_comments_view->optionColumns = 1; $field_comments_view->tags = 'HermodBB'; $field_comments_view->icon = 'eye'; $field_comments_view->save(); } // Post Comments Permission if (!$this->fields->get('hbb_p_comments_post')) { $field_comments_post = new Field(); $field_comments_post->type = $this->modules->get("FieldtypePage"); $field_comments_post->name = 'hbb_p_comments_post'; $field_comments_post->label = 'Post Comments (replies)'; $field_comments_post->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_comments_post->labelFieldName = 'name'; $field_comments_post->inputfield = 'InputfieldCheckboxes'; $field_comments_post->optionColumns = 1; $field_comments_post->tags = 'HermodBB'; $field_comments_post->icon = 'comment'; $field_comments_post->save(); } // Forum Description if (!$this->fields->get('hbb_forum_description')) { $field_forum_description = new Field(); $field_forum_description->type = $this->modules->get("FieldtypeTextarea"); $field_forum_description->name = 'hbb_forum_description'; $field_forum_description->label = 'Forum Description / Rules'; $field_forum_description->tags = 'HermodBB'; $field_forum_description->icon = 'question-circle'; $field_forum_description->save(); } // View Forum Cat Permission if (!$this->fields->get('hbb_p_forum_cat_view')) { $field_forum_cat_view = new Field(); $field_forum_cat_view->type = $this->modules->get("FieldtypePage"); $field_forum_cat_view->name = 'hbb_p_forum_cat_view'; $field_forum_cat_view->label = 'View Forum Category'; $field_forum_cat_view->template_id = wire('templates')->get("role")->id; // Get the id number of the 'role' template $field_forum_cat_view->labelFieldName = 'name'; $field_forum_cat_view->inputfield = 'InputfieldCheckboxes'; $field_forum_cat_view->optionColumns = 1; $field_forum_cat_view->tags = 'HermodBB'; $field_forum_cat_view->icon = 'eye'; $field_forum_cat_view->save(); } // Comment Field if (!$this->fields->get('hbb_comment_content')) { $field_comment = new Field(); $field_comment->type = $this->modules->get("FieldtypeTextarea"); $field_comment->name = 'hbb_comment_content'; $field_comment->label = 'Comment'; $field_comment->tags = 'HermodBB'; $field_comment->icon = 'comment'; $field_comment->save(); } // Comment ID if (!$this->fields->get('hbb_comment_id')) { $field_id = new Field(); $field_id->type = $this->modules->get("FieldtypeInteger"); $field_id->name = 'hbb_comment_id'; $field_id->label = 'Comment ID'; $field_id->tags = 'HermodBB'; $field_id->icon = 'comment'; $field_id->save(); } // Reply ID if (!$this->fields->get('hbb_reply_id')) { $field_reply_id = new Field(); $field_reply_id->type = $this->modules->get("FieldtypeInteger"); $field_reply_id->name = 'hbb_reply_id'; $field_reply_id->label = 'Reply ID'; $field_reply_id->description = 'The $page->id of the comment that was replied to (if applicable)'; $field_reply_id->tags = 'HermodBB'; $field_reply_id->icon = 'reply'; $field_reply_id->save(); } // Author IP if (!$this->fields->get('hbb_ip')) { $field_ip = new Field(); $field_ip->type = $this->modules->get("FieldtypeText"); $field_ip->name = 'hbb_ip'; $field_ip->label = 'IP Address'; $field_ip->tags = 'HermodBB'; $field_ip->icon = 'search'; $field_ip->save(); } // Author User Agent if (!$this->fields->get('hbb_agent')) { $field_agent = new Field(); $field_agent->type = $this->modules->get("FieldtypeText"); $field_agent->name = 'hbb_agent'; $field_agent->label = 'User Agent'; $field_agent->tags = 'HermodBB'; $field_agent->icon = 'search'; $field_agent->save(); } // Comment/Topic Date if (!$this->fields->get('hbb_date')) { $field_date = new Field(); $field_date->type = $this->modules->get("FieldtypeDatetime"); $field_date->name = "hbb_date"; $field_date->label = "Comment / Topic Created"; $field_date->dateOutputFormat = "U"; $field_date->dateInputFormat = "j F Y"; $field_date->timeInputFormat = "g:i A"; $field_date->defaultToday = 1; $field_date->datepicker = 1; // datepicker on click $field_date->tags = 'HermodBB'; $field_date->icon = 'calendar'; $field_date->save(); } // Comment/Topic Edited Date if (!$this->fields->get('hbb_edate')) { $field_edit_date = new Field(); $field_edit_date->type = $this->modules->get("FieldtypeDatetime"); $field_edit_date->name = "hbb_edate"; $field_edit_date->label = "Comment / Topic Last Edited"; $field_edit_date->dateOutputFormat = "U"; $field_edit_date->dateInputFormat = "j F Y"; $field_edit_date->timeInputFormat = "g:i A"; $field_edit_date->defaultToday = 1; $field_edit_date->datepicker = 1; // datepicker on click $field_edit_date->tags = 'HermodBB'; $field_edit_date->icon = 'calendar'; $field_edit_date->save(); } // Last Reply Date if (!$this->fields->get('hbb_ldate')) { $field_last_date = new Field(); $field_last_date->type = $this->modules->get("FieldtypeDatetime"); $field_last_date->name = "hbb_ldate"; $field_last_date->label = "Last Reply Date"; $field_last_date->dateOutputFormat = "U"; $field_last_date->dateInputFormat = "j F Y"; $field_last_date->timeInputFormat = "g:i A"; $field_last_date->defaultToday = 1; $field_last_date->datepicker = 1; // datepicker on click $field_last_date->tags = 'HermodBB'; $field_last_date->icon = 'calendar'; $field_last_date->save(); } // Pinned if (!$this->fields->get('hbb_pinned')) { $field_pinned = new Field(); $field_pinned->type = $this->modules->get("FieldtypeCheckbox"); $field_pinned->name = "hbb_pinned"; $field_pinned->label = "Pinned"; $field_pinned->autocheck = 0; $field_pinned->uncheckedValue = 0; $field_pinned->checkedValue = 1; $field_pinned->tags = 'HermodBB'; $field_pinned->icon = 'thumb-tack'; $field_pinned->save(); } // Private Members if (!$this->fields->get('hbb_private_members')) { $field_private_members = new Field(); $field_private_members->type = $this->modules->get("FieldtypePage"); $field_private_members->name = 'hbb_private_members'; $field_private_members->label = 'Allowed Members'; $field_private_members->description = 'Which members are allowed to view this topic? Blank = public'; $field_private_members->template_id = wire('templates')->get("user")->id; // Get the id number of the 'user' template $field_private_members->labelFieldName = 'name'; $field_private_members->inputfield = 'InputfieldAsmSelect'; $field_private_members->tags = 'HermodBB'; $field_private_members->icon = 'unlock'; $field_private_members->save(); } // Topic Viewed if (!$this->fields->get('hbb_topic_viewed')) { $field_topic_viewed = new Field(); $field_topic_viewed->type = $this->modules->get("FieldtypePage"); $field_topic_viewed->name = 'hbb_topic_viewed'; $field_topic_viewed->label = 'Topic Viewed'; $field_topic_viewed->description = 'Which members have viewed this topic?'; $field_topic_viewed->template_id = wire('templates')->get("user")->id; // Get the id number of the 'user' template $field_topic_viewed->labelFieldName = 'name'; $field_topic_viewed->inputfield = 'InputfieldSelectMultiple'; $field_topic_viewed->tags = 'HermodBB'; $field_topic_viewed->icon = 'eye'; $field_topic_viewed->save(); } // Locked if (!$this->fields->get('hbb_locked')) { $field_locked = new Field(); $field_locked->type = $this->modules->get("FieldtypeCheckbox"); $field_locked->name = "hbb_locked"; $field_locked->label = "Locked"; $field_locked->autocheck = 0; $field_locked->uncheckedValue = 0; $field_locked->checkedValue = 1; $field_locked->tags = 'HermodBB'; $field_locked->icon = 'lock'; $field_locked->save(); } // Topic Field if (!$this->fields->get('hbb_topic_content')) { $field_topic = new Field(); $field_topic->type = $this->modules->get("FieldtypeTextarea"); $field_topic->name = 'hbb_topic_content'; $field_topic->label = 'Topic'; $field_topic->tags = 'HermodBB'; $field_topic->icon = 'file'; $field_topic->save(); } // Comment Fieldgroup if (!$this->fieldgroups->get('hbb_comment')) { $fg_comment_full = new Fieldgroup(); $fg_comment_full->name = 'hbb_comment'; $fg_comment_full->add($this->fields->get('title')); $fg_comment_full->add($field_pinned); $fg_comment_full->add($field_id); $fg_comment_full->add($field_ip); $fg_comment_full->add($field_agent); $fg_comment_full->add($field_date); $fg_comment_full->add($field_edit_date); $fg_comment_full->add($field_comment); $fg_comment_full->add($field_reply_id); $fg_comment_full->save(); } // Comments Category Fieldgroup if (!$this->fieldgroups->get('hbb_comment_cat')) { $fg_comment_cat = new Fieldgroup(); $fg_comment_cat->name = 'hbb_comment_cat'; $fg_comment_cat->add($this->fields->get('title')); $fg_comment_cat->save(); } // Topic Fieldgroup if (!$this->fieldgroups->get('hbb_topic')) { $fg_topic = new Fieldgroup(); $fg_topic->name = 'hbb_topic'; $fg_topic->add($this->fields->get('title')); $fg_topic->add($field_pinned); $fg_topic->add($field_locked); $fg_topic->add($field_topic_viewed); $fg_topic->add($field_private_members); $fg_topic->add($field_ip); $fg_topic->add($field_agent); $fg_topic->add($field_date); $fg_topic->add($field_edit_date); $fg_topic->add($field_last_date); $fg_topic->add($field_topic); $fg_topic->save(); } // Forum Fieldgroup if (!$this->fieldgroups->get('hbb_forum')) { $fg_forum = new Fieldgroup(); $fg_forum->name = 'hbb_forum'; $fg_forum->add($this->fields->get('title')); $fg_forum->add($field_forum_description); $fg_forum->add($field_forum_view); $fg_forum->add($field_topic_post); $fg_forum->add($field_comments_view); $fg_forum->add($field_comments_post); $fg_forum->add($field_topic_pin); $fg_forum->add($field_topic_lock); $fg_forum->save(); } // Forum Cat Fieldgroup if (!$this->fieldgroups->get('hbb_forum_cat')) { $fg_forum_cat = new Fieldgroup(); $fg_forum_cat->name = 'hbb_forum_cat'; $fg_forum_cat->add($this->fields->get('title')); $fg_forum_cat->add($field_forum_cat_view); $fg_forum_cat->save(); } // BB / Forum Root Fieldgroup if (!$this->fieldgroups->get('hbb_bbroot')) { $fg_bbroot = new Fieldgroup(); $fg_bbroot->name = 'hbb_bbroot'; $fg_bbroot->add($this->fields->get('title')); $fg_bbroot->save(); } // Comment Template if (!$this->templates->get('hbb_comment')) { $template_comment_full = new Template(); $template_comment_full->name = 'hbb_comment'; $template_comment_full->fieldgroup = $fg_comment_full; $template_comment_full->label = 'HBB Comment'; $template_comment_full->icon = 'comment'; $template_comment_full->tags = 'HermodBB'; $template_comment_full = $template_comment_full->save(); } // Comments Category Template if (!$this->templates->get('hbb_comment_cat')) { $template_comment_cat = new Template(); $template_comment_cat->name = 'hbb_comment_cat'; $template_comment_cat->fieldgroup = $fg_comment_cat; $template_comment_cat->label = 'HBB Comments Category'; $template_comment_cat->icon = 'comments'; $template_comment_cat->tags = 'HermodBB'; $template_comment_cat = $template_comment_cat->save(); } // Topic Template if (!$this->templates->get('hbb_topic')) { $template_topic = new Template(); $template_topic->name = 'hbb_topic'; $template_topic->fieldgroup = $fg_topic; $template_topic->label = 'HBB Topic'; $template_topic->icon = 'file'; $template_topic->tags = 'HermodBB'; $template_topic = $template_topic->save(); } // Forum Template if (!$this->templates->get('hbb_forum')) { $template_forum = new Template(); $template_forum->name = 'hbb_forum'; $template_forum->fieldgroup = $fg_forum; $template_forum->label = 'HBB Forum'; $template_forum->icon = 'folder-open'; $template_forum->tags = 'HermodBB'; $template_forum->allowPageNum = 1; $template_forum->childTemplates = array(wire('templates')->get("hbb_topic")->id); $template_forum = $template_forum->save(); } // Forum Cat Template if (!$this->templates->get('hbb_forum_cat')) { $template_forum_cat = new Template(); $template_forum_cat->name = 'hbb_forum_cat'; $template_forum_cat->fieldgroup = $fg_forum_cat; $template_forum_cat->label = 'HBB Forum Category'; $template_forum_cat->icon = 'folder'; $template_forum_cat->tags = 'HermodBB'; $template_forum_cat->childTemplates = array(wire('templates')->get("hbb_forum")->id); $template_forum_cat = $template_forum_cat->save(); } // BB / Forum Root Template if (!$this->templates->get('hbb_bbroot')) { $template_bbroot = new Template(); $template_bbroot->name = 'hbb_bbroot'; $template_bbroot->fieldgroup = $fg_bbroot; $template_bbroot->label = 'HBB Forum Root'; $template_bbroot->icon = 'sitemap'; $template_bbroot->tags = 'HermodBB'; $template_bbroot->childTemplates = array(wire('templates')->get("hbb_forum_cat")->id); $template_bbroot = $template_bbroot->save(); } }
/** * Save the context of the given field for the given fieldgroup * * @param Field $field Field to save context for * @param Fieldgroup $fieldgroup Context for when field is in this fieldgroup * @return bool True on success * */ public function ___saveFieldgroupContext(Field $field, Fieldgroup $fieldgroup) { // get field without contxt $fieldOriginal = wire('fields')->get($field->name); $data = array(); // make sure given field and fieldgroup are valid if (!($field->flags & Field::flagFieldgroupContext)) { throw new WireException("Field must be in fieldgroup context before its context can be saved"); } if (!$fieldgroup->has($fieldOriginal)) { throw new WireException("Fieldgroup {$fieldgroup} does not contain field {$field}"); } $newValues = $field->getArray(); $oldValues = $fieldOriginal->getArray(); // 0 is the same as 100 for columnWidth, so we specifically set it just to prevent this from being saved when it doesn't need to be if (!isset($oldValues['columnWidth'])) { $oldValues['columnWidth'] = 100; } // add the label and description built-in fields foreach (array('label', 'description') as $key) { $newValues[$key] = $field->{$key}; $oldValues[$key] = $fieldOriginal->{$key}; } // cycle through and determine which values should be saved foreach ($newValues as $key => $value) { $oldValue = empty($oldValues[$key]) ? '' : $oldValues[$key]; if (strlen("{$value}") && $value != $oldValue) { $data[$key] = $value; } } // keep all in the same order so that it's easier to compare (by eye) in the DB ksort($data); // if there is something in data, then JSON encode it. If it's empty then make it null. $data = count($data) ? wireEncodeJSON($data, true) : null; if (is_null($data)) { $data = 'NULL'; } else { $data = "'" . $this->db->escape_string($data) . "'"; } $result = $this->db->query("UPDATE fieldgroups_fields SET data={$data} WHERE fields_id={$field->id} AND fieldgroups_id={$fieldgroup->id}"); return $result; }
/** * Given an export data array, import it back to the class and return what happened * * Changes are not committed until the item is saved * * @param Fieldgroup $fieldgroup * @param array $data * @return array Returns array( * [property_name] => array( * 'old' => 'old value', // old value, always a string * 'new' => 'new value', // new value, always a string * 'error' => 'error message or blank if no error' * ) * @throws WireException if given invalid data * */ public function ___setImportData(Fieldgroup $fieldgroup, array $data) { $return = array('fields' => array('old' => '', 'new' => '', 'error' => array()), 'contexts' => array('old' => '', 'new' => '', 'error' => array())); $fieldgroup->setTrackChanges(true); $fieldgroup->errors("clear"); $_data = $this->getExportData($fieldgroup); $rmFields = array(); if (isset($data['fields'])) { // field data $old = "\n" . implode("\n", $_data['fields']) . "\n"; $new = "\n" . implode("\n", $data['fields']) . "\n"; if ($old !== $new) { $return['fields']['old'] = $old; $return['fields']['new'] = $new; // figure out which fields should be removed foreach ($fieldgroup as $field) { $fieldNames[$field->name] = $field->name; if (!in_array($field->name, $data['fields'])) { $fieldgroup->remove($field); $label = "-{$field->name}"; $return['fields']['new'] .= $label . "\n"; $rmFields[] = $field->name; } } // figure out which fields should be added foreach ($data['fields'] as $name) { $field = $this->wire('fields')->get($name); if (in_array($name, $rmFields)) { continue; } if (!$field) { $error = sprintf($this->_('Unable to find field: %s'), $name); $return['fields']['error'][] = $error; $label = str_replace("\n{$name}\n", "\n?{$name}\n", $return['fields']['new']); $return['fields']['new'] = $label; continue; } if (!$fieldgroup->hasField($field)) { $label = str_replace("\n{$field->name}\n", "\n+{$field->name}\n", $return['fields']['new']); $return['fields']['new'] = $label; $fieldgroup->add($field); } else { $field = $fieldgroup->getField($field->name, true); // in context $fieldgroup->add($field); $label = str_replace("\n{$field->name}\n", "\n{$field->name}\n", $return['fields']['new']); $return['fields']['new'] = $label; } } } $return['fields']['new'] = trim($return['fields']['new']); $return['fields']['old'] = trim($return['fields']['old']); } if (isset($data['contexts'])) { // context data foreach ($data['contexts'] as $key => $value) { // remove items where they are both empty if (empty($value) && empty($_data['contexts'][$key])) { unset($data['contexts'][$key], $_data['contexts'][$key]); } } foreach ($_data['contexts'] as $key => $value) { // remove items where they are both empty if (empty($value) && empty($data['contexts'][$key])) { unset($data['contexts'][$key], $_data['contexts'][$key]); } } $old = wireEncodeJSON($_data['contexts'], true, true); $new = wireEncodeJSON($data['contexts'], true, true); if ($old !== $new) { $return['contexts']['old'] = trim($old); $return['contexts']['new'] = trim($new); foreach ($data['contexts'] as $name => $context) { $field = $fieldgroup->getField($name, true); // in context if (!$field) { if (!empty($context)) { $return['contexts']['error'][] = sprintf($this->_('Unable to find field to set field context: %s'), $name); } continue; } $id = $field->id; $fieldContexts = $fieldgroup->getFieldContextArray(); if (isset($fieldContexts[$id]) || !empty($context)) { $fieldgroup->setFieldContextArray($id, $context); $fieldgroup->trackChange('fieldContexts'); } } } } // other data foreach ($data as $key => $value) { if ($key == 'fields' || $key == 'contexts') { continue; } $old = isset($_data[$key]) ? $_data[$key] : null; if (is_array($old)) { $old = wireEncodeJSON($old, true, false); } $new = is_array($value) ? wireEncodeJSON($value, true, false) : $value; if ($old == $new) { continue; } $fieldgroup->set($key, $value); $error = (string) $fieldgroup->errors("first clear"); $return[$key] = array('old' => $old, 'new' => $value, 'error' => $error); } if (count($rmFields)) { $return['fields']['error'][] = sprintf($this->_('Warning, all data in these field(s) will be permanently deleted (please confirm): %s'), implode(', ', $rmFields)); } $fieldgroup->errors('clear'); return $return; }
public function install(array $settings) { $field = $this->fields->get('email_images'); if (!$field) { $field = new Field(); $field->name = 'email_images'; $field->type = $this->modules->get('FieldtypeImage'); $field->label = 'Email Images'; $field->extensions = 'jpeg jpg gif png'; $field->maxFiles = 0; $field->unzip = 0; $field->descriptionRows = 1; $field->entityEncode = 1; $field->save(); $this->message("Created field: {$field->name}"); } $settings['images_field_id'] = $field->id; $field2 = $this->fields->get('email_image_body'); if (!$field2) { $field2 = new Field(); $field2->name = 'email_image_body'; $field2->type = $this->modules->get('FieldtypeTextarea'); $field2->label = 'Body Text'; $field2->textformatters = array('TextformatterEntities'); $field2->inputfieldClass = 'InputfieldTextarea'; $field2->rows = 5; $field2->save(); $this->message("Created field: {$field2->name}"); } // parent fieldgroup $parentFieldgroup = $this->fieldgroups->get('email-images'); if (!$parentFieldgroup) { $parentFieldgroup = new Fieldgroup(); $parentFieldgroup->name = 'email-images'; $parentFieldgroup->save(); $parentFieldgroup->add('title'); $parentFieldgroup->save(); $this->message("Created fieldgroup: {$parentFieldgroup->name}"); } // parent template $parentTemplate = $this->templates->get('email-images'); if (!$parentTemplate) { $parentTemplate = new Template(); $parentTemplate->name = 'email-images'; $parentTemplate->fieldgroup = $parentFieldgroup; $parentTemplate->allowPageNum = 1; $parentTemplate->save(); $this->message("Created template: {$parentTemplate->name}"); } $settings['parent_template_id'] = $parentTemplate->id; // child fieldgroup $childFieldgroup = $this->fieldgroups->get('email-image'); if (!$childFieldgroup) { $childFieldgroup = new Fieldgroup(); $childFieldgroup->name = 'email-image'; $childFieldgroup->save(); $childFieldgroup->add('title'); $childFieldgroup->add($field); // email_images $childFieldgroup->add($field2); // email_image_body $childFieldgroup->save(); $this->message("Created fieldgroup: {$childFieldgroup->name}"); } // child template $childTemplate = $this->templates->get('email-image'); if (!$template) { $childTemplate = new Template(); $childTemplate->name = 'email-image'; $childTemplate->fieldgroup = $childFieldgroup; $childTemplate->noChildren = 1; $childTemplate->parentTemplates = array($parentTemplate->id); $childTemplate->save(); $this->message("Created template: {$childTemplate->name}"); } $settings['child_template_id'] = $childTemplate->id; $parentPage = $this->pages->get('/email-images/'); if (!$parentPage->id) { $parentPage = new Page(); $parentPage->template = $parentTemplate; $parentPage->parent = '/'; $parentPage->name = 'email-images'; $parentPage->title = 'Email Images'; $parentPage->addStatus(Page::statusHidden); $parentPage->sortfield = '-created'; $parentPage->save(); $this->message("Created page: {$parentPage->path}"); } $settings['parent_page_id'] = $parentPage->id; // update settings for parentTemplate $parentTemplate->childTemplates = array($childTemplate->id); $parentTemplate->noParents = 1; $parentTemplate->save(); $this->modules->saveModuleConfigData('EmailImage', $settings); // install template file if (is_writable(wire('config')->paths->templates)) { if (@copy(dirname(__FILE__) . '/email-images.php', wire('config')->paths->templates . 'email-images.php')) { $this->message("Installed template file: email-images.php"); } } }
/** * Install the module and related modules * */ public function ___install() { $configData = array(); if ($this->templates->get(LanguageSupport::languageTemplateName)) { throw new WireException("There is already a template installed called 'language'"); } if ($this->fields->get(LanguageSupport::languageFieldName)) { throw new WireException("There is already a field installed called 'language'"); } $adminPage = $this->pages->get($this->config->adminRootPageID); $setupPage = $adminPage->child("name=setup"); if (!$setupPage->id) { throw new WireException("Unable to locate {$adminPage->path}setup/"); } // create the languages parent page $languagesPage = new Page(); $languagesPage->parent = $setupPage; $languagesPage->template = $this->templates->get('admin'); $languagesPage->process = $this->modules->get('ProcessLanguage'); // INSTALL ProcessLanguage module $this->message("Installed ProcessLanguage"); $languagesPage->name = 'languages'; $languagesPage->title = 'Languages'; $languagesPage->status = Page::statusSystem; $languagesPage->sort = $setupPage->numChildren; $languagesPage->save(); $configData['languagesPageID'] = $languagesPage->id; // create the 'language_files' field used by the 'language' fieldgroup $field = new Field(); $field->type = $this->modules->get("FieldtypeFile"); $field->name = 'language_files'; $field->label = 'Language Translation Files'; $field->extensions = 'json'; $field->maxFiles = 0; $field->inputfieldClass = 'InputfieldFile'; $field->unzip = 1; $field->descriptionRows = 1; $field->flags = Field::flagSystem | Field::flagPermanent; $field->save(); $this->message("Created field: language_files"); // create the fieldgroup to be used by the language template $fieldgroup = new Fieldgroup(); $fieldgroup->name = LanguageSupport::languageTemplateName; $fieldgroup->add($this->fields->get('title')); $fieldgroup->add($field); // language_files $fieldgroup->save(); $this->message("Created fieldgroup: " . LanguageSupport::languageTemplateName . " ({$fieldgroup->id})"); // create the template used by Language pages $template = new Template(); $template->name = LanguageSupport::languageTemplateName; $template->fieldgroup = $fieldgroup; $template->parentTemplates = array($adminPage->template->id); $template->slashUrls = 1; $template->pageClass = 'Language'; $template->pageLabelField = 'name'; $template->noGlobal = 1; $template->noMove = 1; $template->noTrash = 1; $template->noUnpublish = 1; $template->noChangeTemplate = 1; $template->nameContentTab = 1; $template->flags = Template::flagSystem; $template->save(); $this->message("Created Template: " . LanguageSupport::languageTemplateName); // create the default language page $default = new Language(); $default->template = $template; $default->parent = $languagesPage; $default->name = 'default'; $default->title = 'Default'; $default->status = Page::statusSystem; $default->save(); $configData['defaultLanguagePageID'] = $default->id; $configData['otherLanguagePageIDs'] = array(); // non-default language IDs placeholder $this->message("Created Default Language Page: {$default->path}"); // create the translator page and process $translatorPage = new Page(); $translatorPage->parent = $setupPage; $translatorPage->template = $this->templates->get('admin'); $translatorPage->status = Page::statusHidden | Page::statusSystem; $translatorPage->process = $this->modules->get('ProcessLanguageTranslator'); // INSTALL ProcessLanguageTranslator $this->message("Installed ProcessLanguageTranslator"); $translatorPage->name = 'language-translator'; $translatorPage->title = 'Language Translator'; $translatorPage->save(); $configData['languageTranslatorPageID'] = $translatorPage->id; $this->message("Created Language Translator Page: {$translatorPage->path}"); // save the module config data $this->modules->saveModuleConfigData('LanguageSupport', $configData); // install 'language' field that will be added to the user fieldgroup $field = new Field(); $field->type = $this->modules->get("FieldtypePage"); $field->name = LanguageSupport::languageFieldName; $field->label = 'Language'; $field->derefAsPage = 1; $field->parent_id = $languagesPage->id; $field->labelFieldName = 'title'; $field->inputfield = 'InputfieldRadios'; $field->required = 1; $field->flags = Field::flagSystem | Field::flagPermanent; $field->save(); $this->message("Created Langage Field: " . LanguageSupport::languageFieldName); // make the 'language' field part of the profile fields the user may edit $profileConfig = $this->modules->getModuleConfigData('ProcessProfile'); $profileConfig['profileFields'][] = 'language'; $this->modules->saveModuleConfigData('ProcessProfile', $profileConfig); // add to 'user' fieldgroup $userFieldgroup = $this->templates->get('user')->fieldgroup; $userFieldgroup->add($field); $userFieldgroup->save(); $this->message("Added field 'language' to user profile"); // update all users to have the default value set for this field $n = 0; foreach ($this->users as $user) { $user->set('language', $default); $user->save(); $n++; } $this->message("Added default language to {$n} user profiles"); $this->message("Language Support Installed! Click to the 'Setup' menu to begin defining languages."); }