Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая Сычева Ксения.doc
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
950.78 Кб
Скачать

Приложение 3

Код модуля JobSearch

<?php // $Id: job.module,v 1.19.2.9 2010/06/01 02:04:29 surgemartin Exp $ // Copyright Khalid Baheyeldin 2006-2009 http://2bits.com define('JOB_NODE_TYPE', 'job_node_type_'); define('RESUME_NODE_TYPE', 'resume_node_type_'); define('JOB_EMAIL', 'job_email'); define('JOB_PERM_APPLY', 'apply for jobs'); define('JOB_PERM_WITHDRAW', 'cancel own job applications'); define('JOB_PERM_MANAGE', 'manage job applications'); define('JOB_PERM_REMOVE', 'cancel any job applications'); /** * Реализация hook_help. */ function job_help($path, $arg) { switch ($path) { case 'admin/help#job': return t('Allows users to apply for jobs posted.'); } } /** * Реализация hook_permp(). */ function job_perm() { return array( JOB_PERM_APPLY, JOB_PERM_WITHDRAW, JOB_PERM_MANAGE, JOB_PERM_REMOVE, ); } /** * Реализация hook_menu(). */ function job_menu() { $items = array(); $items['admin/settings/job'] = array( 'title' => t('Job'), 'description' => t('Job settings'), 'page callback' => 'drupal_get_form', 'page arguments' => array('job_admin_settings'), 'access arguments' => array('administer site configuration'), ); $items['job/apply'] = array( 'page callback' => 'job_apply', 'type' => MENU_CALLBACK, 'access callback' => "_job_perm_apply_node", 'access arguments' => array(2), ); $items['job/clear'] = array( 'page callback' => 'job_clear', 'type' => MENU_CALLBACK, 'access callback' => "_job_perm_clear", 'access arguments' => array(2,3), ); $items['job/applications'] = array( 'page callback' => 'job_view', 'title' => t('Job Applications Received'), 'type' => MENU_SUGGESTED_ITEM, 'access arguments' => array(JOB_PERM_MANAGE), ); $items['job/appliedfor'] = array( 'page callback' => 'job_post_view', 'title' => t('Jobs Applied For'), 'type' => MENU_SUGGESTED_ITEM, 'access arguments' => array(JOB_PERM_APPLY), ); return $items; } function _job_perm_clear($nid, $uid) { global $user; $node = node_load($nid); if (user_access(JOB_PERM_REMOVE) || ($uid && (int) $uid == $user->uid && user_access(JOB_PERM_WITHDRAW))) { return true; } return false; } function _job_perm_apply_node($nid) { global $user; $uid = $user-> uid; // find who posted the job $node = node_load($nid); $nuid = $node->uid; // Can't apply for own job if (user_access(JOB_PERM_APPLY) && $nuid != $uid && !_job_check($node->nid, $uid)) { return true; } return false; } function job_admin_settings() { $set = 'job'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('Enable job application for these content types'), ); foreach (node_get_types() as $type => $name) { $form[$set][JOB_NODE_TYPE . $type] = array( '#type' => 'checkbox', '#title' => $type, '#return_value' => 1, '#default_value' => variable_get(JOB_NODE_TYPE . $type, 0), ); } $form["job_email_files"] = array( '#type' => 'checkbox', '#title' => t("Include file links in notification emails"), '#return_value' => 1, '#default_value' => variable_get("job_email_files", 0), '#description' => t("Select this option if you want application email notifications to include links directly to resume files"), ); return system_settings_form($form); } /** * Реализация hook_link (). */ function job_link($type, $node = NULL, $teaser = FALSE) { global $user; $links = array(); // Если нет работы узла, не беспокойтесь if ($type != 'node' || variable_get(JOB_NODE_TYPE . $node->type, 0) == 0) { return $links; } if (_job_perm_apply_node($node->nid)) { // ОК, чтобы применить $links['job_apply'] = array( 'title' => t('Apply for this job'), 'href' => "job/apply/$node->nid", ); } else { // Теперь, определим, почему пользователю было отказано if (!$user->uid) { // Пользователь не вошел в систему // ПРИМЕЧАНИЕ: обратите внимание на сообщение здесь $links['job_apply'] = array( 'title' => t('Please !login or !register to apply', array( '!login' => l(t('login'), 'user/login'), '!register' => l(t('register'), 'user/register') )), 'html' => true, ); } else if (_job_check($node->nid, $user->uid)) { // Пользователь подал заявку на эту вакансию // ПРИМЕЧАНИЕ: обратите внимание на ссылку, чтобы вывести приложение здесь, если это допускается // - нужно будет проверить статус разрешения и применения // - Если статус = 0, невозможно вывести $links['job_apply'] = array( 'title' => t('You applied for this job'), ); } else { // Уловка для авторизованных пользователей без разрешений // ПРИМЕЧАНИЕ: обратите внимание на сообщение здесь // на данный момент, ничего не покажет; } } return $links; } /** * Реализация hook_user (). */ function job_user($op, &$edit, &$account, $category = NULL) { global $user; switch ($op) { case 'view': if ($user->uid == $account->uid) { // User is viewing own account if (user_access(JOB_PERM_MANAGE)) { // Создание приложений Работа, Полученный заголовок $account->content['job_applications'] = array( '#type' => 'user_profile_category', '#title' => t('Job Applications Received'), '#weight' => 0, '#attributes' => array( 'class' => 'user-member', ), ); // Создание приложений Работа, Содержимое $account->content['job_applications']['view_job_applications'] = array( '#type' => 'user_profile_item', '#value' => l(t('View job applications'), 'job/applications'), '#weight' => 0 ); } if (user_access(JOB_PERM_APPLY)) { // Создание рабочих мест, применяется для заголовка $account->content['job_applied_for'] = array( '#type' => 'user_profile_category', '#title' => t('Jobs Applied For'), '#weight' => 0, '#attributes' => array( 'class' => 'user-member', ), ); // Создание рабочих мест, применяется для содержимого $account->content['job_applied_for']['view_job_applied_for'] = array( '#type' => 'user_profile_item', '#value' => l(t('View job postings'), 'job/appliedfor'), '#weight' => 0 ); } } break; } } function job_apply() { global $user; $job_nid = (int)arg(2); $resume_nid = (int)arg(3); if (!$user->uid) { drupal_set_message(t('Please !login or !register to apply', array( '!login' => l(t('login'), 'user/login'), '!register' => l(t('register'), 'user/register') ))); drupal_goto("node/$nid"); } if (!user_access(JOB_PERM_APPLY)) { drupal_set_message(t('You are not authorized to apply for jobs.')); drupal_goto("node/$job_nid"); } if (!$job_nid) { drupal_set_message(t('No job specified.')); drupal_goto("node/$job_nid"); } if ($resume_nid) { job_apply_do($job_nid, $resume_nid); drupal_goto("node/$job_nid"); } $job = node_load(array('nid' => $job_nid)); $resume_list = job_resume_list($user->uid); if (!$resume_list) { foreach (node_get_types() as $type => $name) { $resume_node = variable_get(RESUME_NODE_TYPE . $type, 0); if ($resume_node == 1) { $resume_node = $type; break; } } $msg = t('Please !create to apply', array('!create' => l(t('create a resume'), 'node/add/' . $resume_node))); drupal_set_message($msg); drupal_goto("node/$job_nid"); } if(count($resume_list) == 1) { drupal_goto("job/apply/$job_nid/$resume_nid{$resume_list[0]['nid']}"); } $output .= '<br/>' . t('Position: ') . $job->title . '<br>'; $output .= '<br/>' . t('Select from your resumes below, or !create', array('!create' => l(t('create a new resume'), "node/add"))); $output .= '<br/>'; foreach($resume_list as $resume) { $view = l(t('view'), 'node/' . $resume['nid']); $apply = l(t('apply'), "job/apply/$job_nid/$resume_nid" . $resume['nid']); $edit = l(t('edit'), 'node/' . $resume['nid'] . '/edit'); $rows[] = array( $resume['title'], format_interval(time() - $resume['changed']), $view . ' ' . $apply . ' ' . $edit ); } $headers = array(t('Resume Title'), t('Last Changed'), t('Operations')); $output .= theme('table', $headers, $rows); print theme('page', $output); } function job_resume_list($uid) { $resume_types = array(); foreach(node_get_types() as $type => $name) { if (variable_get(RESUME_NODE_TYPE . $type, 0)) { $resumes_types[] = "'". $type ."'"; } } if (!count($resumes_types)) { return array(); } $result = db_query("SELECT n.nid, n.title, n.changed FROM {node} n WHERE n.type IN (". implode(',', $resumes_types) . ") AND n.uid = %d" , $uid); while($row = db_fetch_array($result)) { $data[] = $row; } return $data; } function job_apply_do($job_nid, $resume_nid) { global $user; if (job_user_applied($user->uid, $job_nid)) { drupal_set_message(t('You already applied for this position.')); drupal_goto("node/$job_nid"); } db_query('INSERT INTO {job} (nid, uid, resume_nid, timestamp, status) VALUES (%d, %d, %d, %d, %d)', $job_nid, $user->uid, $resume_nid, time(), 1); job_send_email($job_nid, $resume_nid); drupal_set_message(t('Thank you. Your application has been submitted.')); drupal_goto("node/$job_nid"); } function job_user_applied($uid, $job_nid) { if (db_result(db_query("SELECT COUNT(*) FROM {job} j INNER JOIN {node} r ON j.resume_nid = r.nid INNER JOIN {node} n ON n.nid = r.nid WHERE n.uid = %d

AND j.nid = %d", $uid, $job_nid))) { return TRUE; } else { return FALSE; } } function job_clear($nid, $uid) { if ($nid && $uid) { if (strcmp($uid, 'all') == 0) { db_query("UPDATE {job} SET status = 0 WHERE nid = %d", $nid, $uid); drupal_set_message(t('All job applications for #@nid have been cleared.', array('@nid' => $nid))); } else { db_query("UPDATE {job} SET status = 0 WHERE nid = %d AND uid = %d", $nid, $uid); drupal_set_message(t('Job application #@nid has been cleared.', array('@nid' => $nid))); } } if (user_access(JOB_PERM_MANAGE)) { drupal_goto('job/applications'); } else { drupal_goto('job/appliedfor'); } } function job_view() { global $user; print theme('page', theme('job_view', job_get_list($user->uid))); } function job_get_list($uid = NULL) { $rows = array(); $sql = "SELECT n.nid, j.uid, j.resume_nid, j.timestamp FROM {node} n INNER JOIN {job} j USING(nid) WHERE n.uid = %d AND j.status = 1 ORDER by j.timestamp ASC"; $result = db_query($sql, $uid); while ($data = db_fetch_object($result)) { $resume = node_load(array('nid' => $data->resume_nid)); $job = node_load(array('nid' => $data->nid)); $rows[] = array( 'applicant' => user_load(array('uid' => $data->uid)), 'resume' => $resume, 'job' => $job, 'timestamp' => $data->timestamp, ); } return $rows; } function job_post_view() { global $user; print theme('page', theme('job_post_view', job_get_post_list($user->uid))); } function job_get_post_list($uid = NULL) { $rows = array(); $sql = "SELECT n.nid, n.uid, j.resume_nid, j.timestamp, j.uid AS juid FROM {node} n INNER JOIN {job} j USING(nid) WHERE j.uid = %d AND j.status = 1 ORDER by j.timestamp ASC"; $result = db_query($sql, $uid); while ($data = db_fetch_object($result)) { $resume = node_load(array('nid' => $data->resume_nid)); $job = node_load(array('nid' => $data->nid)); $rows[] = array( 'poster' => user_load(array('uid' => $data->uid)), 'resume' => $resume, 'job' => $job, 'timestamp' => $data->timestamp, 'applicant' => user_load(array('uid' => $data->juid)), ); } return $rows; } function theme_job_view($list = array()) { $rows = array(); $header = array(t('Job'), t('Applicant'), t('Resume'), t('Date'), t('Operations')); if (count($list)) { foreach($list as $data) { $applicant = $data['applicant']; $resume = $data['resume']; $job = $data['job']; $title = l($job->title, "node/$job->nid"); $resume = l($resume->title, "node/" . $resume->nid); $user = l($applicant->name, "user/$applicant->uid"); $ops = l(t('Clear'), "job/clear/$job->nid/" . $applicant->uid); $timestamp = format_date($data['timestamp'], 'custom', 'Y-m-d H:i'); $rows[] = array( 'data' => array_merge( array($title), array($user), array($resume), array($timestamp), array($ops) ) ); } } else { $rows[] = array('data' => array(t('No job applications.'))); } return theme('table', $header, $rows); } /** * Реализация hook_theme (). * */ function job_theme() { return array( 'job_view' => array( 'file' => 'job.module', 'arguments' => array( 'list' => array(), ), ), 'job_post_view' => array( 'file' => 'job.module', 'arguments' => array( 'list' => array(), ), ), 'job_mail' => array( 'file' => 'job.module', 'arguments' => array( 'job_node' => NULL, 'job_user' => NULL, 'resume_node' => NULL, 'resume_user' => NULL, ), ), 'job_search_item' => array( 'file' => 'job.module', 'arguments' => array( 'item' => NULL, ), ), ); } function theme_job_post_view($list = array()) { $rows = array(); $header = array(t('Poster'), t('Job'), t('Resume'), t('Date'), t('Operations')); if (count($list)) { foreach($list as $data) { $poster = $data['poster']; $applicant = $data['applicant']; $resume = $data['resume']; $job = $data['job']; $title = l($job->title, "node/$job->nid"); $resume = l($resume->title, "node/" . $resume->nid); $user = l($poster->name, "user/$poster->uid"); $ops = l(t('Clear'), "job/clear/$job->nid/" . $applicant->uid); $timestamp = format_date($data['timestamp'], 'custom', 'Y-m-d H:i'); $rows[] = array( 'data' => array_merge( array($user), array($title), array($resume), array($timestamp), array($ops) ) ); } } else { $rows[] = array('data' => array(t('No jobs applied for.'))); } return theme('table', $header, $rows); } /** * Реализация hook_nodeapi (). */ function job_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { switch ($op) { case 'delete': // Node is being deleted, delete it from the job table db_query("DELETE FROM {job} WHERE nid = %d", $node->nid); break; } } function _job_check($nid, $uid) { return db_result(db_query("SELECT COUNT(*) FROM {job} WHERE nid = %d AND uid = %d" , $nid, $uid)); } /** * Реализация hook_mail (). */ function job_mail($key, &$message, $params) { $result = theme('job_mail', $params['job_node'], $params['job_user'], $params['resume_node'], $params['resume_user']); $message['subject'] = $result['subject']; $message['body'] = $result['body']; } function job_send_email($job_nid, $resume_nid) { global $user; $params['job_node'] = $job_node = node_load(array('nid' => $job_nid)); $params['job_user'] = $job_user = user_load(array('uid' => $job_node->uid)); $params['resume_node'] = $resume_node = node_load(array('nid' => $resume_nid)); $params['resume_user'] = $resume_user = user_load(array('uid' => $resume_node->uid)); $from = $resume_user->mail; $language = user_preferred_language($user); drupal_mail('job', 'job_apply', $job_user->mail, $language, $params, $from); watchdog('job', t("%name applied for job $job_node->nid.", array('%name' => theme('placeholder', $resume_user->name . " <$from>")))); } function theme_job_mail($job_node, $job_user, $resume_node, $resume_user) { global $base_url; $site = variable_get('site_name', 'drupal site'); $subject = t("[$site] [Job application] for [$job_node->title] by [$resume_user->name]"); $body = t("The following user has applied for the above job.\n"); $body .= t("\nJob: @title", array('@title' => $job_node->title)); $body .= t("\nJob URL: @url", array('@url' => $base_url . url("node/$job_node->nid"))); $body .= t("\nApplicant name: @name", array('@name' => $resume_user->name)); $body .= t("\nApplicant email: @email", array('@email' => $resume_user->mail)); $body .= t("\nApplicant URL: @url", array('@url' => $base_url . url("user/$resume_user->uid"))); $body .= t("\nResume: @title", array('@title' => $resume_node->title)); $body .= t("\nResume URL: @url", array('@url' => $base_url . url("node/$resume_node->nid"))); // Отправьте ссылку на файлы, если они подключены if (variable_get("job_email_files", 0)) { $file_links = array(); // Во-первых, постарайтесь включить файлы из любого поля файла if (module_exists('filefield') && $fields = filefield_get_field_list($resume_node->type)) { foreach ($fields as $field) { $files = $resume_node->$field['field_name']; foreach ($files as $file) { $file_links[] = $base_url . url($file['filepath']); } } } // Далее проверьте наличие файла вложения из основного upload.module if (is_array($resume_node->files) && count($resume_node->files)) { foreach ($resume_node->files as $file_obj) { // Если этот файл не "в списке", пропустите его if (empty($file_obj->list)) { continue; } // Примечание: для электронной почты HTML раскомментируйте следующую строку и закомментируйте вместо нее последнюю //$file_links[] = l($file_obj->description, $base_url . url($file_obj->filepath)); $file_links[] = $base_url . url($file_obj->filepath); } } if (count($file_links)) { $body .= t("\n\nResume file(s):\n"). implode($file_links, "/n"); } } $body .= t("\n\nManage job applications at @manage", array('@manage' => $base_url . url("job/applications"))); return(array( 'subject' => $subject, 'body' => $body, )); } function job_search_item($item) { return theme('job_search_item', $item); } function theme_job_search_item($item) { $output .= '<div id="job_search">'; $output .= l($item['title'], $item['link']) . '<br />'; $output .= $item['snippet']. '<br />'; $output .= $item['user'] . ' | ' . format_date($item['date'], 'small'). '<br /><br />'; $output .= '</div>'; return $output ; } /** * Реализация hook_views_api (). */ function job_views_api() { return array( 'api' => 2, 'path' => drupal_get_path('module', 'job') .'/views', ); }