Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Markdown support #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Alert classification is a complicated task, but with Opsweekly a few simple ques
* A webserver
* PHP 5.4 (or higher), including the curl extensions for PHP, MySQL extension, and short_open_tags enabled
* MySQL for data storage

* PHPMarkdown PEAR package

## Installation/configuration
1. Download/clone the repo into an appropriate folder either in your
Expand Down Expand Up @@ -173,6 +173,15 @@ The `$sleep_providers` array handles the definition and configuring of the plugi
* `lib`: The path to the PHP file that contains your provider code, e.g. `providers/sleep/up.php`
* You are also allowed to pass any other arbritray key/value pairs in. As the entire config array is passed to the plugin, you can retrieve any values that are applicable to Opsweekly as a whole, rather than per user (which are specified above)

### Editor configuration
In this section you can define and configure additional editors. To add an editor, first copy `editors/example.php` and implement both the `printEditor` and `formatEntry` methods. Next, add your editor to this array.

The `$editors` array tells the app what editors are available. The following values are required for each entry:

* `name`: A friendly display name for this editor.
* `description`: A description of this editor.
* `class`: The class name that implements this editor.
* `lib`: The path to the PHP file that contains the code for this editor.

### Generic configuration
There are a few other configuration options, which are documented in the example config file. Some highlights include:
Expand Down
11 changes: 6 additions & 5 deletions add.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

$page_title = getTeamName() . " Weekly Updates - Add new update";
include_once('phplib/header.php');
include_once('phplib/nav.php')
include_once('phplib/nav.php');

$editor = getEditorByUser($my_username, $_GET['editor']);
?>

<div class="container">
Expand Down Expand Up @@ -70,12 +71,12 @@

?>
<form action="add_generic_weekly.php" method="POST">
<textarea class="textarea span7" name="weeklyupdate" placeholder="Enter Update" style="height: 500px"><?php echo $previous_report ?></textarea>
<script>
$('.textarea').wysihtml5({"image": false, "color": false});
</script>
<textarea class="textarea span7" name="weeklyupdate" placeholder="Enter Update" style="height: 500px"><?php echo $previous_report ?></textarea>
<?php echo $editor->printEditor(); ?>
<input type="hidden" name="editor" value="<?php echo $editor::TYPE ?>">
<input type="hidden" name="range_start" value="<?php echo $start_ts ?>">
<input type="hidden" name="range_end" value="<?php echo $end_ts ?>">
<?php printEditorsList() ?>
<button class="btn btn-primary" type="submit">Save Weekly Update</button>
<button class="btn btn-success" name="do_email" type="submit" value="true"><i class="icon-white icon-envelope"></i> Save and email this report</button>
</form>
Expand Down
3 changes: 2 additions & 1 deletion add_generic_weekly.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
$report_id = generateWeeklyReportID($username, $range_start, $range_end);
$state = "final";
$report = mysql_real_escape_string($_POST['weeklyupdate']);
$query = "INSERT INTO generic_weekly (report_id, range_start, range_end, timestamp, user, state, report) VALUES ('$report_id', '$range_start', '$range_end', '$timestamp', '$username', '$state', '$report')";
$report_type = mysql_real_escape_string($_POST['editor']);
$query = "INSERT INTO generic_weekly (report_id, range_start, range_end, timestamp, user, state, report, report_type) VALUES ('$report_id', '$range_start', '$range_end', '$timestamp', '$username', '$state', '$report', '$report_type')";
if (!mysql_query($query)) {
echo "Database update failed, error: " . mysql_error();
} else {
Expand Down
3 changes: 2 additions & 1 deletion add_meeting_notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
$range_end = mysql_real_escape_string($_POST['range_end']);
$report_id = generateMeetingNotesID($range_start, $range_end);
$notes = mysql_real_escape_string($_POST['weeklynotes']);
$query = "INSERT INTO meeting_notes (report_id, range_start, range_end, timestamp, user, notes) VALUES ('$report_id', '$range_start', '$range_end', '$timestamp', '$username', '$notes')";
$notes_type = mysql_real_escape_string($_POST['editor']);
$query = "INSERT INTO meeting_notes (report_id, range_start, range_end, timestamp, user, notes, notes_type) VALUES ('$report_id', '$range_start', '$range_end', '$timestamp', '$username', '$notes', '$notes_type')";
if (!mysql_query($query)) {
echo "Database update failed, error: " . mysql_error();
} else {
Expand Down
17 changes: 17 additions & 0 deletions assets/editors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$(function() {
var unsaved_changes = false;

$('textarea').on('change', function() {
unsaved_changes = true;
});

$('button[type=submit]').on('click', function() {
unsaved_changes = false;
});

window.onbeforeunload = function() {
if (unsaved_changes) {
return "You have unsaved changes";
}
};
});
15 changes: 15 additions & 0 deletions edit_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</select>
</fieldset>
<br />

<fieldset>
<legend>Sleep Tracking</legend>
<label class="radio">
Expand All @@ -70,6 +71,20 @@
?>
</fieldset>

<br />
<fieldset>
<legend>Editor</legend>
<?php
foreach ($editors as $editor_key => $e_config) {
$checked = ($profile['editor'] == $editor_key) ? " checked" : "";
echo '<label class="radio">';
echo "<input type='radio' name='editor' id='editor-{$editor_key}' value='{$editor_key}'{$checked}>";
echo "<strong>{$e_config['name']}</strong> - {$e_config['description']}";
echo "</label>";
}
?>
</fieldset>

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="button" class="btn">Cancel</button>
Expand Down
8 changes: 8 additions & 0 deletions editors/editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

abstract class Editor {
const TYPE = 'INVALID';

abstract public function printEditor();
abstract public function formatEntry($data);
}
33 changes: 33 additions & 0 deletions editors/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This is a editor.
*/

require_once('editor.php');

class ExampleEditor extends Editor {
const TYPE = 'example';

/*
* printEditor
* Return any html necessary to render the editor
*
* @return string Some html
*/
public function printEditor() {
return '';
}

/*
* formatEntry
* Format the entry using this editor
*
* @param string $data The input data
*
* @return string The output format
*/
public function formatEntry($data) {
return '';
}
}
15 changes: 15 additions & 0 deletions editors/markdowneditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

include_once('markdown.php');
require_once('editor.php');

class MarkdownEditor extends Editor {
const TYPE = 'markdown';

public function printEditor() {
return '';
}
public function formatEntry($data) {
return Markdown($data);
}
}
14 changes: 14 additions & 0 deletions editors/wysiwygeditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once('editor.php');

class WYSIWYGEditor extends Editor {
const TYPE = 'wysiwyg';

public function printEditor() {
return "<script>$('.textarea').wysihtml5({'image': false, 'color': false});</script>";
}
public function formatEntry($data) {
return $data;
}
}
1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

include_once("phplib/base.php");


$time_requested = getOrSetRequestedDate();

$start_end = getWeekRange($time_requested);
Expand Down
12 changes: 6 additions & 6 deletions meeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
$page_title = getTeamName() . " Weekly Updates - Meeting View";
include_once('phplib/header.php');
include_once('phplib/nav.php');

$editor = getEditorByUser($my_username, $_GET['editor']);
?>

<script>
Expand Down Expand Up @@ -73,14 +75,12 @@ function setDateToLastWeek() {
?>
</small></h2>
<form action="<?php echo $ROOT_URL; ?>/add_meeting_notes.php" method="POST" id="weekly-notes">
<textarea class="textarea span12" name="weeklynotes" placeholder="Enter Meeting Notes, e.g. Hiring, Launches, Corp IT information" style="height: 200px">
<?php echo $previous_report ?>
</textarea>
<script>
$('.textarea').wysihtml5({"image": false, "color": false});
</script>
<textarea class="textarea span12" name="weeklynotes" placeholder="Enter Meeting Notes, e.g. Hiring, Launches, Corp IT information" style="height: 200px"><?php echo $previous_report ?></textarea>
<?php echo $editor->printEditor() ?>
<input type="hidden" name="editor" value="<?php echo $editor::TYPE ?>">
<input type="hidden" name="range_start" value="<?php echo $start_ts ?>">
<input type="hidden" name="range_end" value="<?php echo $end_ts ?>">
<?php printEditorsList() ?>
<button class="btn btn-primary" type="submit">Save Meeting Notes</button>
</form>

Expand Down
4 changes: 4 additions & 0 deletions opsweekly.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CREATE TABLE `oncall_weekly` (
`state` varchar(20) NOT NULL,
`contact` varchar(255) NOT NULL,
`output` text NOT NULL,
`output_type` varchar(255) NOT NULL,
`tag` varchar(255),
`sleep_state` int(1) signed,
`mtts` int(5) signed,
Expand All @@ -28,6 +29,7 @@ CREATE TABLE `generic_weekly` (
`user` varchar(255) NOT NULL,
`state` varchar(255) NOT NULL,
`report` text NOT NULL,
`report_type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `report_name` (`report_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand All @@ -40,6 +42,7 @@ CREATE TABLE `meeting_notes` (
`timestamp` int(10) unsigned NOT NULL,
`user` varchar(255) NOT NULL,
`notes` text NOT NULL,
`notes_type` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `report_name` (`report_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand All @@ -50,6 +53,7 @@ CREATE TABLE `user_profile` (
`timezone` varchar(10) NOT NULL,
`sleeptracking_provider` varchar(255) NOT NULL,
`sleeptracking_settings` text NOT NULL,
`editor` varchar(255) NOT NULL,
PRIMARY KEY (`ldap_username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

51 changes: 49 additions & 2 deletions phplib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ function printHeaderNav() {
}
}

function getEditor($editor_name) {
global $editors;
$editor_name = array_key_exists($editor_name, $editors) ? $editor_name:array_keys($editors)[0];

include_once($editors[$editor_name]['lib']);
return new $editors[$editor_name]['class']();
}

function getEditorNameByUser($username) {
$editor_name = '';
$username = mysql_real_escape_string($username);
if (connectToDB()) {
$results = mysql_query("SELECT editor FROM user_profile where ldap_username='{$username}' LIMIT 1");
if (mysql_num_rows($results) == 1) {
$editor_name = mysql_fetch_assoc($results)['editor'];
}
}

return $editor_name;
}

function getEditorByUser($username, $editor_name=null) {
if (is_null($editor_name)) {
$editor_name = getEditorNameByUser($username);
}
return getEditor($editor_name);
}

function getGenericWeeklyReportsForWeek($range_start, $range_end) {
connectToDB();
$query = "SELECT * FROM generic_weekly WHERE id IN (SELECT max(id) FROM generic_weekly where range_start='{$range_start}' AND range_end='{$range_end}' GROUP BY(user)) ORDER BY user ASC;";
Expand Down Expand Up @@ -481,22 +509,41 @@ function formatOnCallRowForPrint(array $n) {
}

function formatWeeklyReportForPrint(array $data) {
$editor = getEditor($data['report_type']);
$content = $editor->formatEntry($data['report']);
$pretty_time = getPrettyTime($data['timestamp']);
$html = "<h3>{$data['user']}<small> written {$pretty_time}</small></h3>";
$html .= "<div class='well well-small'><p>{$data['report']}</p></div>";
$html .= "<div class='well well-small'><p>{$content}</p></div>";

return $html;
}

function formatMeetingNotesForPrint(array $data, $small_header = false) {
$editor = getEditor($data['notes_type']);
$content = $editor->formatEntry($data['notes']);
$html = ($small_header) ? "<h4>Notes " : "<h2>Meeting Notes <small>";
$html .= "taken by {$data['user']} at the " . getTeamName() ." Meeting held on " . date("l jS F Y", $data['timestamp']);
$html .= ($small_header) ? "</h4>" : "</small></h2>";
$html .= "<div class='well well-small'>{$data['notes']}</div>";
$html .= "<div class='well well-small'>{$content}</div>";

return $html;
}

function printEditorsList() {
global $editors;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this not global but have a function to get the available editors or something like that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current design grabs the config data for each editor from config.php. I don't think that is the best way of doing it, but having a function that just returns large globs of static data seems odd. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I feel like this is more consistent with the current architecture of the app. 🎱


echo '<div class="btn-group dropup">';
echo '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">';
echo 'Editor <span class="caret"></span>';
echo '</a>';
echo '<ul class="dropdown-menu">';
foreach ($editors as $name=>$val) {
printf('<li><a href="?editor=%s">%s</a></li>', $name, $name);
}
echo '</ul>';
echo '</div>';
}

function printWeeklyHints($username, $from, $to) {
$wanted_hints = getTeamConfig('weekly_hints');

Expand Down
18 changes: 18 additions & 0 deletions phplib/config.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,24 @@ $sleep_providers = array(
"graphite_host" => "http://graphite.mycompany.com")
);

/**
* Editors
*/
$editors = array(
'wysiwyg' => array(
"name" => "WYSIWYG",
"description" => "You get what you see!",
"class" => "WYSIWYGEditor",
"lib" => "editors/wysiwygeditor.php"
),
'markdown' => array(
"name" => "Markdown",
"description" => "Less markup with markdown",
"class" => "MarkdownEditor",
"lib" => "editors/markdowneditor.php"
)
);

// The number of search results per page
$search_results_per_page = 25;

Expand Down
1 change: 1 addition & 0 deletions phplib/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<link href="<?php echo $ROOT_URL;?>/assets/cal-heatmap.css" rel="stylesheet">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>google.load("visualization", "1", {packages:["corechart"]});</script>
<script type="text/javascript" src="/assets/editors.js"></script>
</head>

<body>
5 changes: 3 additions & 2 deletions save_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
$tz = mysql_real_escape_string($_POST['timezone']);
$sleep_provider = mysql_real_escape_string($_POST['sleeptracking_provider']);
$sleep_settings = mysql_real_escape_string(json_encode($_POST['sleeptracking']));
$editor = mysql_real_escape_string($_POST['editor']);

$query = "REPLACE INTO user_profile (ldap_username, full_name, timezone, sleeptracking_provider, sleeptracking_settings)
VALUES ('$username', '$full_name', '$tz', '$sleep_provider', '$sleep_settings')";
$query = "REPLACE INTO user_profile (ldap_username, full_name, timezone, sleeptracking_provider, sleeptracking_settings, editor)
VALUES ('$username', '$full_name', '$tz', '$sleep_provider', '$sleep_settings', '$editor')";
if (!mysql_query($query)) {
echo "Database update failed, error: " . mysql_error();
} else {
Expand Down