public function executeCustomField() { $term = \Terms::retrieveById($this->request()->get('id')); $this->setView('custom_fields'); $session = Session::getInstance(); if (!$term) { $session->setFlash('term_message', t('Term not found with' . $this->request()->get('id'))); $this->redirect($this->createUrl('category', array('taxonomy' => $this->request()->get('taxonomy')))); } $error = array(); $message = array(); $input = new \TermCustomFields(); if ($this->request()->isPostRequest()) { $input->hydrate($this->request()->post('custom_fields')); $input->setTermId($term->getId()); if (!($acceptValues = $this->request()->post('accept_values'))) { $acceptValues = explode("\n", $acceptValues); $input->setAcceptValue(json_encode($acceptValues)); } if ($input->save()) { $message = t('Save new custom fields success!'); } else { if (!$input->isValid()) { foreach ($input->getValidationFailures() as $validationFailures) { $error[$validationFailures->getColumn()] = $validationFailures->getMessage(); } } } } $customFields = \TermCustomFields::findByTermId($term->getId()); $this->setView('custom_fields'); $this->view()->assign(array('error' => $error, 'message' => $message, 'term' => $term, 'input' => $input, 'custom_fields' => $customFields)); return $this->renderComponent(); }
public function executeLoadCustomFieldFrm() { $category_id = $this->request()->post('category_id'); $category = \Terms::retrieveById($category_id); if (!$category) { //error return $this->renderText(''); } $post_id = $this->request()->post('post_id'); //load category custom fields $categoryCfs = \TermCustomFields::findByTermId($category_id); if (empty($categoryCfs)) { return $this->renderText(''); } /** @var \PostCustomFields[] $postCfs */ $postCfs = array(); //load item custom field value if exist if ($post_id) { $_postCfs = \PostCustomFields::findByPostId($post_id); for ($i = 0, $size = sizeof($_postCfs); $i < $size; ++$i) { $postCfs[$_postCfs[$i]->getCfId()] = $_postCfs; } unset($_postCfs); } //end load item custom field value $data = array(); foreach ($categoryCfs as $catCf) { $d = (object) $catCf->toArray(); $d->value = ''; if (isset($postCfs[$catCf->getId()])) { //exist items $i = $postCfs[$catCf->getId()]; switch ($catCf->getFormat()) { case 'NUMBER': $d->value = (double) $i->getNumberValue(); break; case 'BOOL': $d->value = (bool) $i->getBoolValue(); break; case 'DATETIME': $d->value = $i->getDatetimeValue(); break; default: $d->value = $i->getTextValue(); } } $data[] = $d; } $data = Plugin::applyFilters('custom_' . $category->getTaxonomy() . '_cf_form_data', $data); $buf = $this->renderPartial(array('data' => $data)); $buf = Plugin::applyFilters('custom_' . $category->getTaxonomy() . '_cf_form', $buf); return $buf; }