/** * Save the tags * * @param int $otherId The id of the item to tag. * @param mixed $tags The tags for the item. * @param string $module The module wherin the item is located. * @param string[optional] $language The language wherin the tags will be inserted, if not provided the workinglanguage will be used. */ public static function saveTags($otherId, $tags, $module, $language = null) { $otherId = (int) $otherId; $tags = is_array($tags) ? (array) $tags : (string) $tags; $module = (string) $module; $language = $language != null ? (string) $language : BackendLanguage::getWorkingLanguage(); // redefine the tags as an array if (!is_array($tags)) { $tags = (array) explode(',', $tags); } // make sure the list of tags is unique $tags = array_unique($tags); // get db $db = BackendModel::getDB(true); // get current tags for item $currentTags = (array) $db->getPairs('SELECT i.tag, i.id FROM tags AS i INNER JOIN modules_tags AS mt ON i.id = mt.tag_id WHERE mt.module = ? AND mt.other_id = ? AND i.language = ?', array($module, $otherId, $language)); // remove old links if (!empty($currentTags)) { $db->delete('modules_tags', 'tag_id IN (' . implode(', ', array_values($currentTags)) . ') AND other_id = ?', $otherId); } // tags provided if (!empty($tags)) { // loop tags foreach ($tags as $key => $tag) { // cleanup $tag = trim($tag); // unset if the tag is empty if ($tag == '') { unset($tags[$key]); } else { $tags[$key] = $tag; } } // get tag ids $tagsAndIds = (array) $db->getPairs('SELECT i.tag, i.id FROM tags AS i WHERE i.tag IN ("' . implode('", "', $tags) . '") AND i.language = ?', array($language)); // loop again and create tags that don't already exist foreach ($tags as $tag) { // doesn' exist yet if (!isset($tagsAndIds[$tag])) { // insert tag $tagsAndIds[$tag] = self::insert($tag, $language); } } // init items to insert $rowsToInsert = array(); // loop again foreach ($tags as $tag) { // get tagId $tagId = (int) $tagsAndIds[$tag]; // not linked before so increment the counter if (!isset($currentTags[$tag])) { $db->execute('UPDATE tags SET number = number + 1 WHERE id = ?', $tagId); } // add to insert array $rowsToInsert[] = array('module' => $module, 'tag_id' => $tagId, 'other_id' => $otherId); } // insert the rows at once if there are items to insert if (!empty($rowsToInsert)) { $db->insert('modules_tags', $rowsToInsert); } } // add to search index BackendSearchModel::editIndex($module, $otherId, array('tags' => implode(' ', (array) $tags)), $language); // decrement number foreach ($currentTags as $tag => $tagId) { // if the tag can't be found in the new tags we lower the number of tags by one if (array_search($tag, $tags) === false) { $db->execute('UPDATE tags SET number = number - 1 WHERE id = ?', $tagId); } } // remove all tags that don't have anything linked $db->delete('tags', 'number = ?', 0); }
/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { $this->meta->setUrlCallback('BackendFaqModel', 'getURL', array($this->record['id'])); $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('QuestionIsRequired')); $this->frm->getField('answer')->isFilled(BL::err('AnswerIsRequired')); $this->frm->getField('category_id')->isFilled(BL::err('CategoryIsRequired')); $this->meta->validate(); if ($this->frm->isCorrect()) { // build item $item['id'] = $this->id; $item['meta_id'] = $this->meta->save(true); $item['category_id'] = $this->frm->getField('category_id')->getValue(); $item['language'] = $this->record['language']; $item['question'] = $this->frm->getField('title')->getValue(); $item['answer'] = $this->frm->getField('answer')->getValue(true); $item['hidden'] = $this->frm->getField('hidden')->getValue(); // update the item BackendFaqModel::update($item); BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item)); // edit search index BackendSearchModel::editIndex('faq', $item['id'], array('title' => $item['question'], 'text' => $item['answer'])); // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('index') . '&report=saved&var=' . urlencode($item['question']) . '&highlight=row-' . $item['id']); } } }
/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); $this->frm->getField('text')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('publish_on_date')->isValid(BL::err('DateIsInvalid')); $this->frm->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid')); $this->frm->getField('category_id')->isFilled(BL::err('FieldIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // build item $item['id'] = $this->id; $item['revision_id'] = $this->record['revision_id']; // this is used to let our model know the status (active, archive, draft) of the edited item $item['meta_id'] = $this->meta->save(); $item['category_id'] = (int) $this->frm->getField('category_id')->getValue(); $item['user_id'] = $this->frm->getField('user_id')->getValue(); $item['language'] = BL::getWorkingLanguage(); $item['title'] = $this->frm->getField('title')->getValue(); $item['introduction'] = $this->frm->getField('introduction')->getValue(); $item['text'] = $this->frm->getField('text')->getValue(); $item['publish_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('publish_on_date'), $this->frm->getField('publish_on_time'))); $item['edited_on'] = BackendModel::getUTCDate(); $item['hidden'] = $this->frm->getField('hidden')->getValue(); $item['allow_comments'] = $this->frm->getField('allow_comments')->getChecked() ? 'Y' : 'N'; $item['status'] = $status; if ($this->imageIsAllowed) { $item['image'] = $this->record['image']; // the image path $imagePath = FRONTEND_FILES_PATH . '/blog/images'; // if the image should be deleted if ($this->frm->getField('delete_image')->isChecked()) { // delete the image SpoonFile::delete($imagePath . '/source/' . $item['image']); // reset the name $item['image'] = null; } // new image given? if ($this->frm->getField('image')->isFilled()) { // delete the old image SpoonFile::delete($imagePath . '/source/' . $this->record['image']); // build the image name $item['image'] = $this->meta->getURL() . '.' . $this->frm->getField('image')->getExtension(); // upload the image $this->frm->getField('image')->moveFile($imagePath . '/source/' . $item['image']); } elseif ($item['image'] != null) { // get the old file extension $imageExtension = SpoonFile::getExtension($imagePath . '/source/' . $item['image']); // get the new image name $newName = $this->meta->getURL() . '.' . $imageExtension; // only change the name if there is a difference if ($newName != $item['image']) { // move the old file to the new name SpoonFile::move($imagePath . '/source/' . $item['image'], $imagePath . '/source/' . $newName); // assign the new name to the database $item['image'] = $newName; } } } else { $item['image'] = null; } // update the item $item['revision_id'] = BackendBlogModel::update($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item)); // recalculate comment count so the new revision has the correct count BackendBlogModel::reCalculateCommentCount(array($this->id)); // save the tags BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // active if ($item['status'] == 'active') { // edit search index BackendSearchModel::editIndex($this->getModule(), $item['id'], array('title' => $item['title'], 'text' => $item['text'])); // ping if (BackendModel::getModuleSetting($this->URL->getModule(), 'ping_services', false)) { BackendModel::ping(SITE_URL . BackendModel::getURLForBlock($this->URL->getModule(), 'detail') . '/' . $this->meta->getURL()); } // build URL $redirectUrl = BackendModel::createURLForAction('index') . '&report=edited&var=' . urlencode($item['title']) . '&id=' . $this->id . '&highlight=row-' . $item['revision_id']; } elseif ($item['status'] == 'draft') { // everything is saved, so redirect to the edit action $redirectUrl = BackendModel::createURLForAction('edit') . '&report=saved-as-draft&var=' . urlencode($item['title']) . '&id=' . $item['id'] . '&draft=' . $item['revision_id'] . '&highlight=row-' . $item['revision_id']; } // append to redirect URL if ($this->categoryId != null) { $redirectUrl .= '&category=' . $this->categoryId; } // everything is saved, so redirect to the overview $this->redirect($redirectUrl); } } }
/** * Validate the form * * @return void */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // validate redirect $redirectValue = $this->frm->getField('redirect')->getValue(); if ($redirectValue == 'internal') { $this->frm->getField('internal_redirect')->isFilled(BL::err('FieldIsRequired')); } if ($redirectValue == 'external') { $this->frm->getField('external_redirect')->isURL(BL::err('InvalidURL')); } // init var $templateId = (int) $this->frm->getField('template_id')->getValue(); // loop blocks in template for ($i = 0; $i < $this->templates[$templateId]['num_blocks']; $i++) { // get the extra id $extraId = (int) $this->frm->getField('block_extra_id_' . $i)->getValue(); // reset some stuff if ($extraId > 0) { // type of block if (isset($this->extras[$extraId]['type']) && $this->extras[$extraId]['type'] == 'block') { // home can't have blocks if ($this->record['id'] == 1) { $this->frm->getField('block_html_' . $i)->addError(BL::err('HomeCantHaveBlocks')); $this->frm->addError(BL::err('HomeCantHaveBlocks')); } } } } // set callback for generating an unique URL $this->meta->setURLCallback('BackendPagesModel', 'getURL', array($this->record['id'], $this->record['parent_id'], $this->frm->getField('is_action')->getChecked())); // cleanup the submitted fields, ignore fields that were edited by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // init var $data = null; // build data if ($this->frm->getField('is_action')->isChecked()) { $data['is_action'] = true; } if ($redirectValue == 'internal') { $data['internal_redirect'] = array('page_id' => $this->frm->getField('internal_redirect')->getValue(), 'code' => '301'); } if ($redirectValue == 'external') { $data['external_redirect'] = array('url' => $this->frm->getField('external_redirect')->getValue(), 'code' => '301'); } // build page record $page['id'] = $this->record['id']; $page['user_id'] = BackendAuthentication::getUser()->getUserId(); $page['parent_id'] = $this->record['parent_id']; $page['template_id'] = (int) $this->frm->getField('template_id')->getValue(); $page['meta_id'] = (int) $this->meta->save(); $page['language'] = BackendLanguage::getWorkingLanguage(); $page['type'] = $this->record['type']; $page['title'] = $this->frm->getField('title')->getValue(); $page['navigation_title'] = $this->frm->getField('navigation_title')->getValue() != '' ? $this->frm->getField('navigation_title')->getValue() : $this->frm->getField('title')->getValue(); $page['navigation_title_overwrite'] = $this->frm->getField('navigation_title_overwrite')->isChecked() ? 'Y' : 'N'; $page['hidden'] = $this->frm->getField('hidden')->getValue(); $page['status'] = $status; $page['publish_on'] = BackendModel::getUTCDate(null, $this->record['publish_on']); $page['created_on'] = BackendModel::getUTCDate(null, $this->record['created_on']); $page['edited_on'] = BackendModel::getUTCDate(); $page['allow_move'] = $this->record['allow_move']; $page['allow_children'] = $this->record['allow_children']; $page['allow_edit'] = $this->record['allow_edit']; $page['allow_delete'] = $this->record['allow_delete']; $page['sequence'] = $this->record['sequence']; $page['data'] = $data !== null ? serialize($data) : null; // set navigation title if ($page['navigation_title'] == '') { $page['navigation_title'] = $page['title']; } // insert page, store the id, we need it when building the blocks $page['revision_id'] = BackendPagesModel::update($page); // init var $hasBlock = false; // build blocks $blocks = array(); // no blocks should go to waste; even if the new template has fewer blocks, retain existing content $maxNumBlocks = max(count($this->blocksContent), $this->templates[$page['template_id']]['num_blocks']); // loop blocks in template for ($i = 0; $i < $maxNumBlocks; $i++) { // check if this block has been submitted if (isset($_POST['block_extra_id_' . $i])) { // get the extra id $extraId = (int) $this->frm->getField('block_extra_id_' . $i)->getValue(); // reset some stuff if ($extraId <= 0) { $extraId = null; } // init var $html = null; // extra-type is HTML if ($extraId === null) { // reset vars $extraId = null; $html = (string) $this->frm->getField('block_html_' . $i)->getValue(); } else { // type of block if (isset($this->extras[$extraId]['type']) && $this->extras[$extraId]['type'] == 'block') { // home can't have blocks if ($this->record['id'] == 1) { throw new BackendException('Home can\'t have any blocks.'); } // set error if ($hasBlock) { throw new BackendException('Can\'t add 2 blocks'); } // reset var $hasBlock = true; } } // build block $block = array(); $block['id'] = isset($this->blocksContent[$i]['id']) ? $this->blocksContent[$i]['id'] : BackendPagesModel::getMaximumBlockId() + ($i + 1); $block['revision_id'] = $page['revision_id']; $block['extra_id'] = $extraId; $block['html'] = $html; $block['status'] = 'active'; $block['created_on'] = isset($this->blocksContent[$i]['created_on']) ? BackendModel::getUTCDate(null, $this->blocksContent[$i]['created_on']) : BackendModel::getUTCDate(); $block['edited_on'] = BackendModel::getUTCDate(); } else { $block = $this->blocksContent[$i]; $block['revision_id'] = $page['revision_id']; } // add block $blocks[] = $block; } // update the blocks BackendPagesModel::updateBlocks($blocks, $hasBlock); // trigger an event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $page)); // save tags BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // build cache BackendPagesModel::buildCache(BL::getWorkingLanguage()); // active if ($page['status'] == 'active') { // edit search index if (is_callable(array('BackendSearchModel', 'editIndex'))) { // init var $text = ''; // build search-text foreach ($blocks as $block) { $text .= ' ' . $block['html']; } // add BackendSearchModel::editIndex($this->getModule(), $page['id'], array('title' => $page['title'], 'text' => $text)); } // build URL $redirectUrl = BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=edited&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id']; } elseif ($page['status'] == 'draft') { // everything is saved, so redirect to the edit action $redirectUrl = BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=saved-as-draft&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id'] . '&draft=' . $page['revision_id']; } // everything is saved, so redirect to the overview $this->redirect($redirectUrl); } } }
/** * Validate the form * * @return void */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // set callback for generating an unique URL $this->meta->setUrlCallback('BackendBlogModel', 'getURL', array($this->record['id'])); // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); $this->frm->getField('text')->isFilled(BL::err('FieldIsRequired')); $this->frm->getField('publish_on_date')->isValid(BL::err('DateIsInvalid')); $this->frm->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid')); $this->frm->getField('category_id')->isFilled(BL::err('FieldIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // build item $item['id'] = $this->id; $item['revision_id'] = $this->record['revision_id']; // this is used to let our model know the status (active, archive, draft) of the edited item $item['meta_id'] = $this->meta->save(); $item['category_id'] = (int) $this->frm->getField('category_id')->getValue(); $item['user_id'] = $this->frm->getField('user_id')->getValue(); $item['language'] = BL::getWorkingLanguage(); $item['title'] = $this->frm->getField('title')->getValue(); $item['introduction'] = $this->frm->getField('introduction')->getValue(); $item['text'] = $this->frm->getField('text')->getValue(); $item['publish_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('publish_on_date'), $this->frm->getField('publish_on_time'))); $item['edited_on'] = BackendModel::getUTCDate(); $item['hidden'] = $this->frm->getField('hidden')->getValue(); $item['allow_comments'] = $this->frm->getField('allow_comments')->getChecked() ? 'Y' : 'N'; $item['status'] = $status; // update the item $item['revision_id'] = BackendBlogModel::update($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item)); // recalculate comment count so the new revision has the correct count BackendBlogModel::reCalculateCommentCount(array($this->id)); // save the tags BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // active if ($item['status'] == 'active') { // edit search index if (is_callable(array('BackendSearchModel', 'editIndex'))) { BackendSearchModel::editIndex($this->getModule(), $item['id'], array('title' => $item['title'], 'text' => $item['text'])); } // ping if (BackendModel::getModuleSetting($this->URL->getModule(), 'ping_services', false)) { BackendModel::ping(SITE_URL . BackendModel::getURLForBlock($this->URL->getModule(), 'detail') . '/' . $this->meta->getURL()); } // build URL $redirectUrl = BackendModel::createURLForAction('index') . '&report=edited&var=' . urlencode($item['title']) . '&id=' . $this->id . '&highlight=row-' . $item['revision_id']; } elseif ($item['status'] == 'draft') { // everything is saved, so redirect to the edit action $redirectUrl = BackendModel::createURLForAction('edit') . '&report=saved-as-draft&var=' . urlencode($item['title']) . '&id=' . $item['id'] . '&draft=' . $item['revision_id'] . '&highlight=row-' . $item['revision_id']; } // append to redirect URL if ($this->categoryId != null) { $redirectUrl .= '&category=' . $this->categoryId; } // everything is saved, so redirect to the overview $this->redirect($redirectUrl); } } }
/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // validate redirect $redirectValue = $this->frm->getField('redirect')->getValue(); if ($redirectValue == 'internal') { $this->frm->getField('internal_redirect')->isFilled(BL::err('FieldIsRequired')); } if ($redirectValue == 'external') { $this->frm->getField('external_redirect')->isURL(BL::err('InvalidURL')); } // set callback for generating an unique URL $this->meta->setURLCallback('BackendPagesModel', 'getURL', array($this->record['id'], $this->record['parent_id'], $this->frm->getField('is_action')->getChecked())); // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // init var $data = null; // build data if ($this->frm->getField('is_action')->isChecked()) { $data['is_action'] = true; } if ($redirectValue == 'internal') { $data['internal_redirect'] = array('page_id' => $this->frm->getField('internal_redirect')->getValue(), 'code' => '301'); } if ($redirectValue == 'external') { $data['external_redirect'] = array('url' => $this->frm->getField('external_redirect')->getValue(), 'code' => '301'); } // build page record $page['id'] = $this->record['id']; $page['user_id'] = BackendAuthentication::getUser()->getUserId(); $page['parent_id'] = $this->record['parent_id']; $page['template_id'] = (int) $this->frm->getField('template_id')->getValue(); $page['meta_id'] = (int) $this->meta->save(); $page['language'] = BackendLanguage::getWorkingLanguage(); $page['type'] = $this->record['type']; $page['title'] = $this->frm->getField('title')->getValue(); $page['navigation_title'] = $this->frm->getField('navigation_title')->getValue() != '' ? $this->frm->getField('navigation_title')->getValue() : $this->frm->getField('title')->getValue(); $page['navigation_title_overwrite'] = $this->frm->getField('navigation_title_overwrite')->isChecked() ? 'Y' : 'N'; $page['hidden'] = $this->frm->getField('hidden')->getValue(); $page['status'] = $status; $page['publish_on'] = BackendModel::getUTCDate(null, $this->record['publish_on']); $page['created_on'] = BackendModel::getUTCDate(null, $this->record['created_on']); $page['edited_on'] = BackendModel::getUTCDate(); $page['allow_move'] = $this->record['allow_move']; $page['allow_children'] = $this->record['allow_children']; $page['allow_edit'] = $this->record['allow_edit']; $page['allow_delete'] = $this->record['allow_delete']; $page['sequence'] = $this->record['sequence']; $page['data'] = $data !== null ? serialize($data) : null; if ($this->isGod) { $page['allow_move'] = in_array('move', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_children'] = in_array('children', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_edit'] = in_array('edit', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_delete'] = in_array('delete', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; } // set navigation title if ($page['navigation_title'] == '') { $page['navigation_title'] = $page['title']; } // insert page, store the id, we need it when building the blocks $page['revision_id'] = BackendPagesModel::update($page); // loop blocks foreach ($this->blocksContent as $i => $block) { // add page revision id to blocks $this->blocksContent[$i]['revision_id'] = $page['revision_id']; // validate blocks, only save blocks for valid positions if (!in_array($block['position'], $this->templates[$this->frm->getField('template_id')->getValue()]['data']['names'])) { unset($this->blocksContent[$i]); } } // insert the blocks BackendPagesModel::insertBlocks($this->blocksContent); // trigger an event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $page)); // save tags BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // build cache BackendPagesModel::buildCache(BL::getWorkingLanguage()); // active if ($page['status'] == 'active') { // init var $text = ''; // build search-text foreach ($this->blocksContent as $block) { $text .= ' ' . $block['html']; } // add to search index BackendSearchModel::editIndex($this->getModule(), $page['id'], array('title' => $page['title'], 'text' => $text)); // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=edited&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id']); } elseif ($page['status'] == 'draft') { // everything is saved, so redirect to the edit action $this->redirect(BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=saved-as-draft&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id'] . '&draft=' . $page['revision_id']); } } } }