function add()
 {
     if (!CheckPermissions('office')) {
         return;
     }
     if (!CheckRolePermissions('ANNOUNCEMENT_SEND')) {
         return;
     }
     // AJAX
     $this->load->library('xajax');
     $this->xajax->registerFunction(array('_getRecipients', &$this, '_getRecipients'));
     $this->xajax->processRequests();
     $this->pages_model->SetPageCode('office_announcements');
     $data['roles'] = $this->notifications_model->getAllUserRoles();
     $data['bylines'] = $this->notifications_model->getUserBylines();
     if (isset($_POST['preview']) || isset($_POST['post'])) {
         if (!empty($_POST['sender'])) {
             foreach ($data['bylines'] as $byline) {
                 if ($byline->id == $_POST['sender']) {
                     $data['preview']['byline'] = $byline;
                     break;
                 }
             }
         }
         if (empty($data['preview']['byline'])) {
             $this->main_frame->AddMessage('error', 'You need to have a byline to post an announcement. Go and make one now!');
         } else {
             $this->load->library('wikiparser');
             $data['preview']['content'] = $this->wikiparser->parse($_POST['content']);
             if (isset($_POST['post'])) {
                 if (empty($_POST['subject']) || empty($_POST['content'])) {
                     $this->main_frame->AddMessage('error', 'Please make sure you have provided a subject and message for this announcement.');
                 } elseif (empty($_POST['sendto'])) {
                     $this->main_frame->AddMessage('error', 'Please select which group of users you wish to send the announcement to.');
                 } elseif (empty($_POST['sender'])) {
                     $this->main_frame->AddMessage('error', 'Please choose the byline you wish to post the announcement with.');
                 } else {
                     $this->notifications_model->postAnnouncement($_POST['subject'], $_POST['content'], $_POST['sendto'], $_POST['sender']);
                     $this->main_frame->AddMessage('success', 'New announcement has been posted.');
                     redirect('/office/announcements');
                 }
             }
         }
     }
     // Set up the content
     $this->main_frame->IncludeCss('/stylesheets/office_interface.css');
     $this->main_frame->SetExtraHead($this->xajax->getJavascript(null, '/javascript/xajax.js'));
     $this->main_frame->SetContentSimple('office/announcements/post', $data);
     $this->main_frame->Load();
 }
 function _remap($chart_type = 0)
 {
     if (!CheckPermissions('office')) {
         return;
     }
     if (!CheckRolePermissions('ORGCHART_VIEW')) {
         return;
     }
     $data = array();
     $this->pages_model->SetPageCode('office_orgchart');
     $data['data'] = $this->pages_model->GetPropertyText('chart_data');
     $data['type'] = $chart_type;
     $this->main_frame->SetContentSimple('office/orgchart/chart', $data);
     $this->main_frame->Load();
 }
 /** 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 create()
 {
     if (!CheckPermissions('office')) {
         return;
     }
     if (!CheckRolePermissions('ARTICLE_ADD')) {
         return;
     }
     $deadline = date('Y-m-d H:i:s', mktime() + 60 * 60 * 24);
     $article_id = $this->article_model->create($this->user_auth->entityId, $deadline);
     redirect('/office/article/' . $article_id);
 }
 function update()
 {
     if (!CheckPermissions('office', false)) {
         return;
     }
     // Allow admins to do this, in case somebody screws with permissions.
     if (GetUserLevel() != 'admin') {
         $requiredPermissions = array();
         if (isset($_POST['roles'])) {
             $requiredPermissions[] = 'PERMISSIONS_MODIFY_ROLES';
         }
         if (isset($_POST['users'])) {
             $requiredPermissions[] = 'PERMISSIONS_MODIFY_USERS';
         }
         if (!CheckRolePermissions($requiredPermissions)) {
             return;
         }
     } else {
         $this->load->model('permissions_model');
     }
     // Confirm changes
     if (isset($_POST['roles'])) {
         $roleChangesPost = $_POST['roles'];
         $roleChanges = array();
         foreach ($roleChangesPost as $addrem => $changes) {
             foreach ($changes as $change_info) {
                 if (isset($change_info['r']) && isset($change_info['p'])) {
                     $roleChanges[$addrem][$change_info['r']][] = $change_info['p'];
                 }
             }
         }
         if (isset($roleChanges[1])) {
             $this->permissions_model->removeRolePermissions($roleChanges[1]);
         }
         if (isset($roleChanges[0])) {
             $this->permissions_model->addRolePermissions($roleChanges[0]);
         }
     }
     if (isset($_POST['users'])) {
         $userChangesPost = $_POST['users'];
         $userChanges = array();
         foreach ($userChangesPost as $addrem => $changes) {
             foreach ($changes as $change_info) {
                 if (isset($change_info['u']) && isset($change_info['r'])) {
                     $userChanges[$addrem][$change_info['u']][] = $change_info['r'];
                 }
             }
         }
         if (isset($userChanges[1])) {
             $this->permissions_model->removeUserRoles($userChanges[1]);
         }
         if (isset($userChanges[0])) {
             $this->permissions_model->addUserRoles($userChanges[0]);
         }
     }
 }
 function view_byline($byline_id = NULL)
 {
     if (!CheckPermissions('office')) {
         return;
     }
     if (!CheckRolePermissions('BYLINES_VIEW')) {
         return;
     }
     if ($byline_id === NULL) {
         redirect('/office/bylines/');
     } else {
         $data = array();
         $data['byline_info'] = $this->businesscards_model->GetBylineInfo($byline_id);
         if (count($data['byline_info']) == 0) {
             $this->main_frame->AddMessage('error', 'The byline you were trying to access does not exist, please try again.');
             redirect('/office/bylines/');
         } elseif (!$this->permissions_model->hasUserPermission('BYLINES_USERS') && $data['byline_info']['business_card_user_entity_id'] != $this->user_auth->entityId) {
             $this->main_frame->AddMessage('error', 'You do not have access to view or edit the requested byline, please try again.');
             redirect('/office/bylines/');
         } else {
             /// Get byline teams
             $data['groups'] = $this->businesscards_model->GetBylineTeams();
             /// Process edit byline request
             $this->load->library('validation');
             $this->validation->set_error_delimiters('<li>', '</li>');
             /// Validation rules
             $rules['card_name'] = 'trim|required|xss_clean';
             $rules['card_title'] = 'trim|required|xss_clean';
             $rules['group_id'] = 'trim|required|numeric';
             $fields['card_name'] = 'name';
             $fields['card_title'] = 'title';
             $fields['card_course'] = 'course';
             $fields['card_email'] = 'e-mail';
             $fields['card_about'] = 'about';
             $fields['postal_address'] = 'postal address';
             $fields['phone_internal'] = 'phone (internal)';
             $fields['phone_external'] = 'phone (external)';
             $fields['phone_mobile'] = 'phone (mobile)';
             $fields['group_id'] = 'byline team';
             $fields['date_from_day'] = 'display from date (day)';
             $fields['date_from_month'] = 'display from date (month)';
             $fields['date_from_year'] = 'display from date (year)';
             $fields['date_to_day'] = 'display to date (day)';
             $fields['date_to_month'] = 'display to date (month)';
             $fields['date_to_year'] = 'display to date (year)';
             $fields['aboutus'] = 'about us only';
             /// Set rules on field inputs
             $this->validation->set_rules($rules);
             $this->validation->set_fields($fields);
             /// Run validation checks
             $errors = array();
             if ($this->validation->run()) {
                 $group_check = false;
                 foreach ($data['groups'] as $group) {
                     if ($group['business_card_group_id'] == $this->input->post('group_id')) {
                         $group_check = true;
                         break;
                     }
                 }
                 if (!$group_check) {
                     $errors[] = 'Please assign the byline to a team that exists.';
                 }
                 if (!checkdate($this->input->post('date_from_month'), $this->input->post('date_from_day'), $this->input->post('date_from_year'))) {
                     $errors[] = 'The display from date you have specified is not a valid date, please correct it and try again.';
                 }
                 if (!checkdate($this->input->post('date_to_month'), $this->input->post('date_to_day'), $this->input->post('date_to_year'))) {
                     $errors[] = 'The display to date you have specified is not a valid date, please correct it and try again.';
                 }
                 $from_timestamp = mktime(0, 0, 0, $this->input->post('date_from_month'), $this->input->post('date_from_day'), $this->input->post('date_from_year'));
                 $to_timestamp = mktime(0, 0, 0, $this->input->post('date_to_month'), $this->input->post('date_to_day'), $this->input->post('date_to_year'));
                 if ($from_timestamp > $to_timestamp) {
                     $errors[] = 'Please ensure that the display to date is after the display from date.';
                 }
                 /// If no errors, update byline
                 if (count($errors) == 0) {
                     $from_timestamp = date('Y-m-d', $from_timestamp);
                     $to_timestamp = date('Y-m-d', $to_timestamp);
                     if ($this->input->post('aboutus') == 'yes') {
                         $aboutus = 1;
                     } else {
                         $aboutus = 0;
                     }
                     $update = $this->businesscards_model->UpdateBuisnessCard($data['byline_info']['business_card_user_entity_id'], $this->input->post('group_id'), $data['byline_info']['business_card_image_id'], $this->input->post('card_name'), $this->input->post('card_title'), $this->input->post('card_about') == '' ? NULL : $this->input->post('card_about'), $this->input->post('card_course') == '' ? NULL : $this->input->post('card_course'), $this->input->post('card_email') == '' ? NULL : $this->input->post('card_email'), $this->input->post('phone_mobile') == '' ? NULL : $this->input->post('phone_mobile'), $this->input->post('phone_internal') == '' ? NULL : $this->input->post('phone_internal'), $this->input->post('phone_external') == '' ? NULL : $this->input->post('phone_external'), $this->input->post('postal_address') == '' ? NULL : $this->input->post('postal_address'), $data['byline_info']['business_card_order'], $from_timestamp, $to_timestamp, $byline_id, 0, $aboutus);
                     if ($update) {
                         $this->main_frame->AddMessage('success', 'The changes you have requested to the below byline have been sent to an editor for approval.');
                         if ($data['byline_info']['business_card_user_entity_id'] != $this->user_auth->entityId) {
                             $this->notifications_model->sendToUsers('byline', 'Edited Byline', 'has edited one of your [[office/bylines/view_byline/' . $byline_id . '|bylines]].', $data['byline_info']['business_card_user_entity_id']);
                         }
                     } else {
                         $this->main_frame->AddMessage('error', 'There was an error updating the byline\'s information, please try again.');
                     }
                     redirect('/office/bylines/view_byline/' . $byline_id . '/');
                 }
             }
             /// Validation errors occured
             if ($this->validation->error_string != "") {
                 $this->main_frame->AddMessage('error', 'We were unable to process the information you submitted for the following reasons:<ul>' . $this->validation->error_string . '</ul>');
             } elseif (count($errors) > 0) {
                 $temp_msg = '<li>' . implode('</li><li>', $errors) . '</li>';
                 $this->main_frame->AddMessage('error', 'We were unable to process the information you submitted for the following reasons:<ul>' . $temp_msg . '</ul>');
             } else {
                 // First time form has been loaded so populate fields
                 $this->validation->card_name = $data['byline_info']['business_card_name'];
                 $this->validation->card_title = $data['byline_info']['business_card_title'];
                 $this->validation->card_course = $data['byline_info']['business_card_course'];
                 $this->validation->card_email = $data['byline_info']['business_card_email'];
                 $this->validation->card_about = $data['byline_info']['business_card_blurb'];
                 $this->validation->postal_address = $data['byline_info']['business_card_postal_address'];
                 $this->validation->phone_internal = $data['byline_info']['business_card_phone_internal'];
                 $this->validation->phone_external = $data['byline_info']['business_card_phone_external'];
                 $this->validation->phone_mobile = $data['byline_info']['business_card_mobile'];
                 $this->validation->group_id = $data['byline_info']['business_card_business_card_group_id'];
                 $this->validation->date_from_day = date('j', $data['byline_info']['business_card_start_date']);
                 $this->validation->date_from_month = date('n', $data['byline_info']['business_card_start_date']);
                 $this->validation->date_from_year = date('Y', $data['byline_info']['business_card_start_date']);
                 $this->validation->date_to_day = date('j', $data['byline_info']['business_card_end_date']);
                 $this->validation->date_to_month = date('n', $data['byline_info']['business_card_end_date']);
                 $this->validation->date_to_year = date('Y', $data['byline_info']['business_card_end_date']);
                 $this->validation->aboutus = $data['byline_info']['business_card_about_us'];
             }
             /// Process byline image
             $this->load->library('image');
             if ($data['byline_info']['business_card_image_id'] === NULL) {
                 $data['byline_info']['business_card_image_href'] = '';
             } else {
                 $data['byline_info']['business_card_image_href'] = $this->image->getPhotoURL($data['byline_info']['business_card_image_id'], 'userimage');
             }
             // Get page properties information
             $this->pages_model->SetPageCode('office_bylines_view');
             $data['whats_this_heading'] = $this->pages_model->GetPropertyText('whats_this_heading');
             $data['whats_this_text'] = $this->pages_model->GetPropertyWikiText('whats_this_text');
             // Load the page
             $navbar = $this->_navbar('user');
             $this->main_frame->SetContentSimple('office/bylines/byline_view', $data);
             $this->main_frame->Load();
         }
     }
 }