Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: Yoshi2889 on Jul 22nd, 2012  |  syntax: PHP  |  size: 15.60 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /* FXTracker Main File
  4.  * Initializes FXTracker and the main functions.
  5.  */
  6.  
  7. function BugTrackerMain()
  8. {
  9.         // Our usual stuff.
  10.         global $context, $txt, $sourcedir, $scripturl;
  11.  
  12.         // Load the language and template. Oh, don't forget our CSS file, either.
  13.         loadLanguage('BugTracker');
  14.         loadTemplate('BugTracker');
  15.         loadTemplate(false, 'bugtracker');
  16.  
  17.         // Are we allowed to view this?
  18.         isAllowedTo('bugtracker_view');
  19.  
  20.         // Load FXDB
  21.         require_once($sourcedir . '/FXTracker/Class-Database.php');
  22.  
  23.         // A list of all actions we can take.
  24.         // 'action' => 'bug tracker function',
  25.         $sactions = array(
  26.                 'credits' => 'Credits',
  27.  
  28.                 'edit' => 'Edit',
  29.                 'edit2' => 'SubmitEdit',
  30.  
  31.                 'home' => 'Home',
  32.  
  33.                 'mark' => 'MarkEntry',
  34.                 'mark2' => 'SubmitMarkEntry',
  35.  
  36.                 'new' => 'NewEntry',
  37.                 'new2' => 'SubmitNewEntry',
  38.  
  39.                 'projectindex' => 'ViewProject',
  40.  
  41.                 'remove' => 'RemoveEntry',
  42.  
  43.                 'maintenance' => 'Maintenance',
  44.                 'maintenance2' => 'PerformMaintenance',
  45.  
  46.                 'view' => 'View',
  47.         );
  48.  
  49.         // Allow mod creators to easily snap in.
  50.         call_integration_hook('integrate_bugtracker_actions', array(&$sactions));
  51.  
  52.         // Default is home.
  53.         $action = 'home';
  54.  
  55.         // Try to see if we have any other action to use!
  56.         if (!empty($_GET['sa']) && !empty($sactions[$_GET['sa']]) && function_exists('BugTracker' . $sactions[$_GET['sa']]))
  57.                 $action = $_GET['sa'];
  58.  
  59.         // And add a bit onto the linktree.
  60.         $context['linktree'][] = array(
  61.                 'url' => $scripturl . '?action=bugtracker',
  62.                 'name' => $txt['bugtracker'],
  63.         );
  64.  
  65.         // Then, execute the function!
  66.         call_user_func('BugTracker' . $sactions[$action]);
  67. }
  68.  
  69. function BugTrackerHome()
  70. {
  71.         // Global some stuff
  72.         global $smcFunc, $context, $user_info, $user_profile, $txt;
  73.  
  74.         // Set the page title.
  75.         $context['page_title'] = $txt['bugtracker_index'];
  76.  
  77.         // Grab the projects.
  78.         $request = $smcFunc['db_query']('', '
  79.                 SELECT
  80.                         id, name, description, issuenum, featurenum
  81.                 FROM {db_prefix}bugtracker_projects'
  82.         );
  83.  
  84.         // Start empty...
  85.         $context['bugtracker']['projects'] = array();
  86.         while ($project = $smcFunc['db_fetch_assoc']($request))
  87.         {
  88.                 $context['bugtracker']['projects'][$project['id']] = array(
  89.                         'id' => $project['id'],
  90.                         'name' => $project['name'],
  91.                         'num' => array(
  92.                                 'issues' => (int) $project['issuenum'],
  93.                                 'features' => (int) $project['featurenum'],
  94.                         ),
  95.                         'description' => parse_bbc($project['description']),
  96.                         'entries' => array(),
  97.                 );
  98.         }
  99.  
  100.         // Clean up.
  101.         $smcFunc['db_free_result']($request);
  102.  
  103.         // Grab the entries we are allowed to view.
  104.         $where = !allowedTo('bugtracker_viewprivate') ? 'WHERE private = 0' : '';
  105.         $request = $smcFunc['db_query']('', '
  106.                 SELECT
  107.                         id, name, description, type,
  108.                         tracker, private, project,
  109.                         status, attention, progress
  110.  
  111.                 FROM {db_prefix}bugtracker_entries
  112.                 ' . $where
  113.         );
  114.  
  115.         // If we have zero or less(?), don't bother fetching them.
  116.         $context['bugtracker']['entries'] = array();
  117.         $context['bugtracker']['feature'] = array();
  118.         $context['bugtracker']['issue'] = array();
  119.         $context['bugtracker']['attention'] = array();
  120.         while ($entry = $smcFunc['db_fetch_assoc']($request))
  121.         {
  122.                 // Then we're ready for some action.
  123.                 $context['bugtracker']['entries'][$entry['id']] = array(
  124.                         'id' => $entry['id'],
  125.                         'name' => $entry['name'],
  126.                         'shortdesc' => shorten_subject($entry['description'], 50),
  127.                         'desc' => $entry['description'], // As there may be a LOT of entries, do *NOT* use parse_bbc() here!
  128.                         'type' => $entry['type'],
  129.                         'tracker' => $entry['tracker'], // Again, if there are a lot of entries, loading member data for everything may *horribly* slow down the place.
  130.                         'private' => $entry['private'], // Is a boolean anyway.
  131.                         'project' => array(),
  132.                         'status' => $entry['status'],
  133.                         'attention' => $entry['attention'],
  134.                         'progress' => (empty($entry['progress']) ? '0' : $entry['progress']) . '%'
  135.                 );
  136.  
  137.                 $pid = $entry['project'];
  138.                 if (array_key_exists($pid, $context['bugtracker']['projects']))
  139.                         $context['bugtracker']['entries'][$entry['id']]['project'] = $context['bugtracker']['projects'][$pid];
  140.  
  141.                 // Also create a list of issues and features!
  142.                 $context['bugtracker'][$entry['type']][] = $context['bugtracker']['entries'][$entry['id']];
  143.  
  144.                 // Is the status of this entry "attention"? If so, add it to the list of attention requirements thingies!
  145.                 if ($entry['attention'])
  146.                         $context['bugtracker']['attention'][] = $context['bugtracker']['entries'][$entry['id']];
  147.         }
  148.  
  149.         // Clean up.
  150.         $smcFunc['db_free_result']($request);
  151.  
  152.         // Put the last 5 entries of each category in a new array.
  153.         $context['bugtracker']['latest']['issues'] = array_reverse(array_slice($context['bugtracker']['issue'], -5));
  154.         $context['bugtracker']['latest']['features'] = array_reverse(array_slice($context['bugtracker']['feature'], -5));
  155.  
  156.         // What's our template, doc?
  157.         $context['sub_template'] = 'TrackerHome';
  158. }
  159.  
  160. function BugTrackerView()
  161. {
  162.         // Our usual variables.
  163.         global $context, $smcFunc, $user_info, $user_profile, $txt, $scripturl;
  164.  
  165.         // Grab the info for this issue, along with the project, and if we can't, tell the user that the issue does not exist.
  166.         $request = $smcFunc['db_query']('', '
  167.                 SELECT
  168.                         e.id, e.name AS entry_name, e.description, e.type,
  169.                         e.tracker, e.private, e.startedon, e.project,
  170.                         e.status, e.attention, e.progress,
  171.                         p.id, p.name As project_name
  172.                 FROM {db_prefix}bugtracker_entries AS e
  173.                 INNER JOIN {db_prefix}bugtracker_projects AS p ON (e.project = p.id)
  174.                 WHERE e.id = {int:entry}',
  175.                 array(
  176.                         'entry' => $_GET['entry'],
  177.                 )
  178.         );
  179.  
  180.         // Do we have anything? Or too much?
  181.         if ($smcFunc['db_num_rows']($request) == 0 || $smcFunc['db_num_rows']($request) >= 2)
  182.                 fatal_lang_error('entry_no_exist');
  183.  
  184.         // Pick our data.
  185.         $data = $smcFunc['db_fetch_assoc']($request);
  186.  
  187.         // Are we allowed to view private issues, and is this one of them?
  188.         if (!allowedTo('bugtracker_viewprivate') && $data['private'] == 1)
  189.                 fatal_lang_error('entry_is_private', false);
  190.  
  191.         // Load the data for the tracker.
  192.         loadMemberData($data['tracker']);
  193.  
  194.         // Put the data in $context for the template!
  195.         $context['bugtracker']['entry'] = array(
  196.                 'id' => $data['id'],
  197.                 'name' => $data['entry_name'],
  198.                 'desc' => parse_bbc($data['description']),
  199.                 'type' => $data['type'],
  200.                 'tracker' => $user_profile[$data['tracker']],
  201.                 'private' => $data['private'],
  202.                 'started' => $data['startedon'],
  203.                 'project' => array(
  204.                         'id' => (int) $data['project'],
  205.                         'name' => $data['project_name'],
  206.                 ),
  207.                 'status' => $data['status'],
  208.                 'attention' => $data['attention'],
  209.                 'progress' => (empty($data['progress']) ? '0' : $data['progress']) . '%',
  210.                 'is_new' => isset($_GET['new']),
  211.         );
  212.  
  213.         // Stuff the linktree.
  214.         $context['linktree'][] = array(
  215.                 'url' => $scripturl . '?action=bugtracker;sa=projectindex;project=' . $project['id'],
  216.                 'name' => $data['project_name'],
  217.         );
  218.         $context['linktree'][] = array(
  219.                 'url' => $scripturl . '?action=bugtracker;sa=view;entry=' . $data['id'],
  220.                 'name' => sprintf($txt['entrytitle'], $data['id'], $data['entry_name']),
  221.         );
  222.  
  223.         // Setup permissions... Not just one of them!
  224.         $own_any = array('mark', 'mark_new', 'mark_wip', 'mark_done', 'mark_reject', 'mark_attention', 'reply', 'edit', 'remove');
  225.         $is_own = $context['user']['id'] == $data['tracker'];
  226.         foreach ($own_any as $perm)
  227.         {
  228.                 $context['can_bt_' . $perm . '_any'] = allowedTo('bt_' . $perm . '_any');
  229.                 $context['can_bt_' . $perm . '_own'] = allowedTo('bt_' . $perm . '_own') && $is_own;
  230.         }
  231.        
  232.         // If we can mark something.... tell us!
  233.         $context['bt_can_mark'] = allowedTo(array('can_bt_mark_own', 'can_bt_mark_any')) && allowedTo(array('can_bt_mark_new_own', 'can_bt_mark_new_any', 'can_bt_mark_wip_own', 'can_bt_mark_wip_any', 'can_bt_mark_done_own', 'can_bt_mark_done_any', 'can_bt_mark_reject_own', 'can_bt_mark_reject_any'));
  234.  
  235.         // Set the title.
  236.         $context['page_title'] = sprintf($txt['view_title'], $data['id']);
  237.  
  238.         // Then tell SMF what template to load.
  239.         $context['sub_template'] = 'TrackerView';
  240. }
  241.  
  242. function BugTrackerMarkEntry()
  243. {
  244.         // Globalizing...
  245.         global $context, $scripturl, $smcFunc;
  246.  
  247.         // Load data associated with this entry, if it exists.
  248.         $data = fxdb::grabEntry($_GET['id']);
  249.  
  250.         // No entry? No marking.
  251.         if (!$data)
  252.                 fatal_lang_error('entry_no_exists');
  253.  
  254.         // Then, are we allowed to do this kind of stuff?
  255.         if (allowedTo('bt_mark_any') || (allowedTo('bt_mark_own') && $context['user']['id'] == $data['tracker']))
  256.         {
  257.                 // A list of possible types.
  258.                 $types = array('new', 'wip', 'done', 'dead', 'reject', 'attention');
  259.  
  260.                 // Allow people to integrate with this.
  261.                 call_integration_hook('bt_mark_types', &$types);
  262.                
  263.                 // Not in the list?
  264.                 if (!in_array($_GET['as'], $types))
  265.                         fatal_lang_error('entry_mark_failed');
  266.  
  267.                 // Because I like peanuts.
  268.                 if ($_GET['as'] == 'dead')
  269.                         fatal_error('You killed my entry! Murderer!', false);
  270.  
  271.                 // Are we resetting attention?
  272.                 if ($_GET['as'] == 'attention')
  273.                 {
  274.                         $smcFunc['db_query']('', '
  275.                                 UPDATE {db_prefix}bugtracker_entries
  276.                                 SET attention={int:attention}
  277.                                 WHERE id={int:id}',
  278.                                 array(
  279.                                         'attention' => $data['attention'] ? 0 : 1,
  280.                                         'id' => $data['id'],
  281.                                 )
  282.                         );
  283.  
  284.                         redirectexit($scripturl . '?action=bugtracker;sa=view;entry=' . $data['id']);
  285.                 }
  286.  
  287.                 // And 'nother hook for this...
  288.                 call_integration_hook('bt_mark', array(&$_GET['as']));
  289.  
  290.                 // So it is. Mark it!
  291.                 $smcFunc['db_query']('', '
  292.                         UPDATE {db_prefix}bugtracker_entries
  293.                         SET status={string:newstatus}
  294.                         WHERE id={int:id}',
  295.                         array(
  296.                                 'newstatus' => $_GET['as'],
  297.                                 'id' => $data['id'],
  298.                         ));
  299.  
  300.                 // And redirect us back.
  301.                 redirectexit($scripturl . '?action=bugtracker;sa=view;entry=' . $data['id']);
  302.         }
  303.         else
  304.                 fatal_lang_error('entry_unable_mark');
  305. }
  306.  
  307. function BugTrackerEdit()
  308. {
  309.         global $context, $smcFunc;
  310.  
  311.         // Are we using a valid entry id?
  312.         $result = $smcFunc['db_query']('', '
  313.                 SELECT
  314.                         id, name, description, type,
  315.                         tracker, private, project,
  316.                         status, attention, progress
  317.                 FROM {db_prefix}bugtracker_entries
  318.                 WHERE id = {int:entry}',
  319.                 array(
  320.                         'entry' => $_GET['entry'],
  321.                 )
  322.         );
  323.  
  324. }
  325.  
  326. function BugTrackerSubmitEdit()
  327. {
  328.         global $context, $smcFunc;
  329. }
  330.  
  331. function BugTrackerNewEntry()
  332. {
  333.         global $context, $smcFunc, $txt, $scripturl, $sourcedir;
  334.  
  335.         // Are we allowed to create new entries?
  336.         isAllowedTo('bt_add');
  337.  
  338.         // Load the project data.
  339.         $result = $smcFunc['db_query']('', '
  340.                 SELECT
  341.                         id, name
  342.                 FROM {db_prefix}bugtracker_projects
  343.                 WHERE id = {int:project}',
  344.                 array(
  345.                         'project' => $_GET['project']
  346.                 )
  347.         );
  348.  
  349.         // Wait.... There is no project like this? Or there's more with the *same* ID? :O
  350.         if ($smcFunc['db_num_rows']($result) == 0 || $smcFunc['db_num_rows']($result) > 1)
  351.                 fatal_lang_error('project_no_exist');
  352.  
  353.         // So we have just one...
  354.         $project = $smcFunc['db_fetch_assoc']($result);
  355.  
  356.         // Validate the stuff.
  357.         $context['bugtracker']['project'] = array(
  358.                 'id' => (int) $project['id'],
  359.                 'name' => $project['name']
  360.         );
  361.  
  362.         // We want the default SMF WYSIWYG editor.
  363.         require_once($sourcedir . '/Subs-Editor.php');
  364.  
  365.         // Some settings for it...
  366.         $editorOptions = array(
  367.                 'id' => 'entry_desc',
  368.                 'value' => '',
  369.                 'height' => '175px',
  370.                 'width' => '100%',
  371.                 // XML preview.
  372.                 'preview_type' => 2,
  373.         );
  374.         create_control_richedit($editorOptions);
  375.  
  376.         // Store the ID.
  377.         $context['post_box_name'] = $editorOptions['id'];
  378.        
  379.         // Setup the page title...
  380.         $context['page_title'] = $txt['entry_add'];
  381.  
  382.         // Set up the linktree, too...
  383.         $context['linktree'][] = array(
  384.                 'name' => $project['name'],
  385.                 'url' => $scripturl . '?action=bugtracker;sa=projectindex;project=' . $project['id']
  386.         );
  387.         $context['linktree'][] = array(
  388.                 'name' => $txt['entry_add'],
  389.                 'url' => $scripturl . '?action=bugtracker;sa=new;project=' . $project['id']
  390.         );
  391.  
  392.         // Then, set what template we should use!
  393.         $context['sub_template'] = 'BugTrackerAddNew';
  394. }
  395.  
  396. function BugTrackerSubmitNewEntry()
  397. {
  398.         global $smcFunc, $context, $sourcedir, $scripturl;
  399.  
  400.         // Start with checking if we can add new stuff...
  401.         if (!allowedTo('bt_add'))
  402.                 fatal_lang_error('save_failed');
  403.  
  404.         // Load Subs-Post.php, will need that!
  405.         include($sourcedir . '/Subs-Post.php');
  406.  
  407.         // Then, is the required is_fxt POST set?
  408.         if (!isset($_POST['is_fxt']) || empty($_POST['is_fxt']))
  409.                 fatal_lang_error('save_failed');
  410.  
  411.         // Pour over these variables, so they can be altered and done with.
  412.         $entry = array(
  413.                 'title' => $_POST['entry_title'],
  414.                 'type' => $_POST['entry_type'],
  415.                 'private' => !empty($_POST['entry_private']),
  416.                 'description' => $_POST['entry_desc'],
  417.                 'mark' => $_POST['entry_mark'],
  418.                 'attention' => !empty($_POST['entry_attention']),
  419.                 'project' => $_POST['entry_projectid']
  420.         );
  421.  
  422.         // Check if the title, the type or the description are empty.
  423.         if (empty($entry['title']))
  424.                 fatal_lang_error('no_title', false);
  425.  
  426.         // Type...
  427.         if (empty($entry['type']) || !in_array($entry['type'], array('issue', 'feature')))
  428.                 fatal_lang_error('no_type', false);
  429.  
  430.         // And description.
  431.         if (empty($entry['description']))
  432.                 fatal_lang_error('no_description', false);
  433.  
  434.         // Are we submitting a valid mark? (rare condition)
  435.         if (!in_array($entry['mark'], array('new', 'wip', 'done', 'reject')))
  436.                 fatal_lang_error('save_failed');
  437.  
  438.         // Check if the project exists.
  439.         $result = $smcFunc['db_query']('', '
  440.                 SELECT
  441.                         id
  442.                 FROM {db_prefix}bugtracker_projects
  443.                 WHERE id = {int:project}',
  444.                 array(
  445.                         'project' => $entry['project'],
  446.                 )
  447.         );
  448.  
  449.         // The "real" check ;)
  450.         if ($smcFunc['db_num_rows']($result) == 0 || $smcFunc['db_num_rows']($result) > 1)
  451.                 fatal_lang_error('project_no_exist');
  452.  
  453.         // Preparse the message.
  454.         preparsecode(&$entry['description']);
  455.  
  456.         // Okay, lets prepare the entry data itself! Create an array of the available types.
  457.         $fentry = array(
  458.                 'title' => $smcFunc['htmlspecialchars']($entry['title']),
  459.                 'type' => strtolower($entry['type']),
  460.                 'private' => (int) $entry['private'],
  461.                 'description' => $entry['description'],
  462.                 'mark' => strtolower($entry['mark']),
  463.                 'attention' => (int) $entry['attention'],
  464.                 'project' => (int) $entry['project'],
  465.         );
  466.  
  467.         // Assuming we have everything ready now, lets do this! Insert this stuff first.
  468.         $smcFunc['db_insert']('insert',
  469.                 '{db_prefix}bugtracker_entries',
  470.                 array(
  471.                         'name' => 'string',
  472.                         'description' => 'string',
  473.                         'type' => 'string',
  474.                         'tracker' => 'int',
  475.                         'private' => 'int',
  476.                         'project' => 'int',
  477.                         'status' => 'string',
  478.                         'attention' => 'int',
  479.                         'progress' => 'int'
  480.                 ),
  481.                 array(
  482.                         $fentry['title'],
  483.                         $fentry['description'],
  484.                         $fentry['type'],
  485.                         $context['user']['id'],
  486.                         $fentry['private'],
  487.                         $fentry['project'],
  488.                         $fentry['mark'],
  489.                         $fentry['attention'],
  490.                         0
  491.                 )
  492.         );
  493.                        
  494.         // Grab the ID of the entry just inserted.
  495.         $entryid = $smcFunc['db_insert_id']('{db_prefix}bugtracker_entries', 'id');
  496.  
  497.         // What type is this again?
  498.         $type = $fentry['type'] == 'issue' ? 'issue' : 'feature'; // In case this gets changed later on!
  499.        
  500.         // Then update the count at the projects.
  501.         $smcFunc['db_query']('', '
  502.                 UPDATE {db_prefix}bugtracker_projects
  503.                 SET ' . $type . 'num=' . $type . 'num+1
  504.                 WHERE id = {int:project}',
  505.                 array(
  506.                         'project' => $fentry['project'],
  507.                 )
  508.         );
  509.        
  510.         // Then we're ready to opt-out!
  511.         redirectexit($scripturl . '?action=bugtracker;sa=view;entry=' . $entryid . ';new');
  512. }
  513.  
  514. function BugTrackerViewProject()
  515. {
  516.         global $context, $smcFunc, $txt, $scripturl;
  517.  
  518.         // Load the project data.
  519.         $context['bugtracker']['project'] = fxdb::grabProject($_GET['project']) or fatal_lang_error('no_such_project');
  520.        
  521.         // Grab the entries.
  522.         fxdb::grabProjectEntries($context['bugtracker']['project']['id'], $context['bugtracker']);
  523.  
  524.         // Load the template.
  525.         loadTemplate('BugTrackerProject');
  526.  
  527.         // Also stuff the linktree.
  528.         $context['linktree'][] = array(
  529.                 'url' => $scripturl . '?action=bugtracker;sa=projindex;id=' . $context['bugtracker']['project']['id'],
  530.                 'name' => $context['bugtracker']['project']['name'],
  531.         );
  532.        
  533.         // Page title time!
  534.         $context['page_title'] = $context['bugtracker']['project']['name'];
  535.  
  536.         // And the sub template.
  537.         $context['sub_template'] = 'TrackerViewProject';
  538. }
  539.  
  540. function BugTrackerRemoveEntry()
  541. {
  542.         global $context, $smcFunc;
  543.        
  544.         // Then try to load the issue data.
  545.        
  546. }
  547.  
  548. ?>