/** Crosswords management. */ function crossword($crossword = null, $operation = null) { if ('save' === $operation) { OutputModes('ajax'); } if (!CheckPermissions('office')) { return; } if (null !== $crossword && is_numeric($crossword)) { $crossword = (int) $crossword; $crosswords = $this->crosswords_model->GetCrosswords($crossword); if (count($crosswords) === 0) { show_404(); } $crossword_info = $crosswords[0]; $this->load->model('permissions_model'); $data = array('Permissions' => array('modify' => $this->permissions_model->hasUserPermission('CROSSWORD_MODIFY'), 'stats_basic' => $this->permissions_model->hasUserPermission('CROSSWORD_STATS_BASIC')), 'Crossword' => &$crossword_info); if (null === $operation) { if (!CheckRolePermissions('CROSSWORD_VIEW')) { return; } $puzzle = 0; $worked = $this->crosswords_model->LoadCrossword($crossword_info['id'], $puzzle); if (!$worked) { show_404(); } $crosswordView = new CrosswordView($puzzle); $crosswordView->setClueTypes($crossword_info['has_quick_clues'], $crossword_info['has_cryptic_clues']); $crosswordView->setReadOnly(true, true); $data['Grid'] =& $crosswordView; $data['Tips'] = new CrosswordTipsList(null, $crossword_info['id'], true, false); $this->pages_model->SetPageCode('crosswords_office_xword_view'); $this->main_frame->SetContentSimple('crosswords/office/crossword_view', $data); } else { if ('save' === $operation) { if (!CheckRolePermissions('CROSSWORD_VIEW', 'CROSSWORD_MODIFY')) { return; } if (isset($_POST['xw']['save'])) { $puzzle = new CrosswordPuzzle(); $worked = $puzzle->importData($_POST['xw']); if ($worked) { $this->crosswords_model->SaveCrossword($crossword, $puzzle); $status = 'success'; } else { $this->main_frame->Error(array('class' => 'error', 'text' => 'Invalid crossword data.')); $status = 'fail'; } } else { $this->main_frame->Error(array('class' => 'error', 'text' => 'Unable to edit crossword.')); $status = 'fail'; } $root = array('_tag' => 'crossword', 'status' => $status); $this->main_frame->SetXml($root); $this->main_frame->Load(); return; } else { if ('edit' === $operation) { if (!CheckRolePermissions('CROSSWORD_VIEW', 'CROSSWORD_MODIFY')) { return; } $this->pages_model->SetPageCode('crosswords_office_xword_edit'); $this->load->helper('input_date'); $this->load->helper('input_progress'); $puzzle = 0; $worked = $this->crosswords_model->LoadCrossword($crossword, $puzzle); if (!$worked) { $puzzle = new CrosswordPuzzle(13, 13); } $crosswordView = new CrosswordView($puzzle, true); $data = array(); // MAIN CONFIGURATION $config = new InputInterfaces(); $quick_clues_interface = new InputCheckboxInterface('has_quick_clues', $crossword_info['has_quick_clues']); $config->Add('Quick clues', $quick_clues_interface); $cryptic_clues_interface = new InputCheckboxInterface('has_cryptic_clues', $crossword_info['has_cryptic_clues']); $config->Add('Cryptic Clues', $cryptic_clues_interface); $categories = $this->crosswords_model->GetAllCategories(); $category_names = array(); foreach ($categories as $id => $category) { $category_names[$id] = $category['name']; } $category_interface = new InputSelectInterface('category_id', $crossword_info['category_id']); $category_interface->SetOptions($category_names); $config->Add('Category', $category_interface); $layouts = $this->crosswords_model->GetAllLayouts(); $layout_names = array(); foreach ($layouts as $id => $layout) { $layout_names[$id] = $layout['name']; } $layout_interface = new InputSelectInterface('layout_id', $crossword_info['layout_id']); $layout_interface->SetOptions($layout_names); $config->Add('Layout', $layout_interface); $deadline_interface = new InputDateInterface('deadline', $crossword_info['deadline'], true); $config->Add('Deadline', $deadline_interface); $publication_interface = new InputDateInterface('publication', $crossword_info['publication'], true); $config->Add('Publication', $publication_interface); $expiry_interface = new InputDateInterface('expiry', $crossword_info['expiry'], true); $config->Add('Expiry', $expiry_interface); $winners_value = $crossword_info['winners']; $winners_interface = new InputIntInterface('winners', $winners_value, $winners_value > 0); $winners_interface->SetRange(1, 100); $config->Add('Winners', $winners_interface); $completeness_interface = new InputProgressInterface('completeness', $crossword_info['completeness']); $config->Add('Progress', $completeness_interface); $authors_interface = new InputSelectInterface('authors', $crossword_info['author_ids']); $authors = $this->crosswords_model->GetAllAuthors(); $author_options = array(); foreach ($authors as $author) { $author_options[(int) $author['id']] = $author['fullname']; } foreach ($crossword_info['authors'] as $author) { if (!isset($author_options[$author['id']])) { $author_options[$author['id']] = $author['fullname']; } } $authors_interface->SetOptions($author_options); $config->Add('Authors', $authors_interface); // VALIDATION $num_errors = $config->Validate(); if (0 == $num_errors && $config->Updated()) { $values = $config->ChangedValues(); $error = false; if (count($values) == 0) { $this->messages->AddMessage('information', "You did not make any changes"); $error = true; } // Apply rules to changes here $integrated_values = $crossword_info; foreach ($values as $id => $value) { $integrated_values[$id] = $value; } // can't have deadline after publishing if ($integrated_values['deadline'] !== null && $integrated_values['publication'] !== null && $integrated_values['deadline'] > $integrated_values['publication']) { $this->messages->AddMessage('error', 'Deadline should not be set after publication'); $error = true; } // can't have expiry before publishing if ($integrated_values['publication'] !== null && $integrated_values['expiry'] !== null && $integrated_values['publication'] > $integrated_values['expiry']) { $this->messages->AddMessage('error', 'Expiry should not be set before publication'); $error = true; } if (!$error) { if (isset($values['authors'])) { $authors = $values['authors']; $values['authors'] = array(); foreach ($authors as $author_id) { $values['authors'][(int) $author_id] = array('id' => (int) $author_id, 'fullname' => $author_options[(int) $author_id]); } } $values['id'] = $crossword_info['id']; if (!$this->crosswords_model->UpdateCrossword($values)) { $this->messages->AddMessage('error', 'Changes could not be saved'); } else { $this->messages->AddMessage('success', 'Changes have been saved successfully'); foreach ($values as $id => $value) { $crossword_info[$id] = $value; } } } } // Which clues are enabled may have just changed $crosswordView->setClueTypes($crossword_info['has_quick_clues'], $crossword_info['has_cryptic_clues']); $data['Configuration'] =& $config; $data['Tips'] = new CrosswordTipsList(null, $crossword_info['id'], true); $data['Grid'] =& $crosswordView; $data['Paths'] = array('view' => site_url("office/crosswords/crossword/{$crossword}"), 'save' => site_url("office/crosswords/crossword/{$crossword}/save")); $this->main_frame->SetContentSimple('crosswords/office/crossword_edit', $data); } else { if ('stats' === $operation) { if (!CheckRolePermissions('CROSSWORD_STATS_BASIC')) { return; } // find information about this crossword $data['Stats'] = $this->crosswords_model->CalculateStats($crossword_info['id'], array('saves', 'save_users')); $data['Stats']['winners'] = $crossword_info['winners_so_far']; $data['StatLabels'] = array('save_users' => 'Number of users who have attempted crossword (based on saves)', 'save_mean_per_user' => 'Mean number of saves per user (approx 30 secs between saves)', 'winners' => 'Number of winners'); $this->main_frame->SetContentSimple('crosswords/office/crossword_stats', $data); } elseif ($operation == 'preview') { if (!CheckRolePermissions('CROSSWORD_VIEW')) { return; } $this->crosswords_model->GetCrosswordThumbnail($crossword); return; } else { show_404(); } } } } } else { show_404(); } $this->main_frame->Load(); }
function __construct($category_id = null, $crossword_id = null, $office = false, $allow_add = true) { $this->category_id = $category_id; $this->crossword_id = $crossword_id; $this->office = $office; $ci =& get_instance(); if ($office) { $ci->load->helper('input'); $ci->load->helper('input_wikitext'); $categories = $ci->crosswords_model->GetTipCategories(); $category_options = array(); foreach ($categories as &$category) { $category_options[$category['id']] = $category['name']; } // Allow adding of new tips to specific crosswords if (null != $crossword_id && $allow_add) { // Can't add if there aren't any categories! if (!empty($category_options)) { $this->add_form = new InputInterfaces(); $new_tip = array('category_id' => $this->category_id === null ? $categories[0]['id'] : (int) $this->category_id, 'crossword_id' => $this->crossword_id, 'content_wikitext' => '', 'content_xhtml' => ''); if (null === $this->category_id) { // Tip category $category_interface = new InputSelectInterface('new_tip_category', $new_tip['category_id']); $category_interface->SetOptions($category_options); $this->add_form->Add('Tip category', $category_interface); } else { $category_interface = null; } // Wikitext $content_interface = new InputWikitextInterface('new_tip_content', $new_tip['content_wikitext']); $content_interface->SetRequired(true); $content_interface->SetWikiparser(); $this->add_form->Add('Content (wikitext)', $content_interface); $num_errors = $this->add_form->Validate(); if (0 == $num_errors && $this->add_form->Updated()) { if (null === $this->category_id) { $new_tip['category_id'] = (int) $category_interface->Value(); } $new_tip['content_wikitext'] = $content_interface->Value(); $new_tip['content_xhtml'] = $content_interface->ValueXhtml(); if (null !== $ci->crosswords_model->AddTip($new_tip)) { $ci->messages->AddMessage('success', 'Tip added'); $content_interface->Reset(); } else { $ci->messages->AddMessage('error', 'Tip could not be added'); } } } else { $ci->messages->AddMessage('information', 'There are no crossword tip categories. You will need to <a href="/office/crosswords/tips/add?ret=' . urlencode($ci->uri->uri_string()) . '">create one</a> before you can add any tips for this crossword.'); } } } $this->tips = $ci->crosswords_model->GetTips($category_id, $crossword_id, null, $office ? null : true); if ($this->office) { foreach ($this->tips as $index => &$tip) { $form = new InputInterfaces(); $tip['edit_form'] =& $form; $name = 'tip_' . $tip['id'] . '_'; $inputs = array(); $tip['inputs'] =& $inputs; // Delete tip $inputs['delete'] = new InputCheckboxInterface($name . 'delete', false); $form->Add('Delete tip', $inputs['delete']); // Tip category $inputs['category'] = new InputSelectInterface($name . 'category', $tip['category_id']); $inputs['category']->SetOptions($category_options); $form->Add('Tip category', $inputs['category']); // Wikitext $inputs['content'] = new InputWikitextInterface($name . 'content', $tip['content_wikitext']); $inputs['content']->SetRequired(true); $inputs['content']->SetWikiparser(); $form->Add('Content (wikitext)', $inputs['content']); // Delete it? if ($inputs['delete']->Value()) { $success = $ci->crosswords_model->DeleteTipById($tip['id']); if ($success) { unset($this->tips[$index]); $ci->messages->AddMessage('success', 'Tip deleted'); } else { $ci->messages->AddMessage('error', 'Tip could not be deleted'); } } else { $num_errors = $form->Validate(); if (0 == $num_errors && $form->Changed()) { $changes = $form->ChangedValues(); $values = array(); if (isset($changes[$name . 'category'])) { $values['category_id'] = $changes[$name . 'category']; } if (isset($changes[$name . 'content'])) { $values['content_wikitext'] = $changes[$name . 'content']; $values['content_xhtml'] = $inputs['content']->ValueXhtml(); } $values['id'] = $tip['id']; if (!$ci->crosswords_model->UpdateTip($values)) { $ci->messages->AddMessage('error', 'Tip could not be saved'); } else { $ci->messages->AddMessage('success', 'Tip saved successfully'); foreach ($values as $id => $value) { $tip[$id] = $value; } if (isset($values['category_id'])) { $tip['category_name'] = $category_options[$values['category_id']]; // Remove if no longer matching criteria if (null !== $this->category_id && $this->category_id != $values['category_id']) { unset($this->tips[$index]); } } $form->ResetDefaults(); } } } unset($form); unset($inputs); } } }