add/edit bug UI improvements
[couchdbimport.git] / CouchProjects / Demos / php / BugShrink / newBug.php
blob53b2df69d6d45bb1b4557a9026593687a72d2fd0
1 <?php
3 if(count($_POST) == 0) {
4 $Bug = $Author = $Body = $rev = $Subject = $State = $Project = $Category = '';
5 if($edit) {
6 $Subject = $bug_details->get('Subject');
7 $Subject = substr($Subject, 0, ($pos = strpos($Subject, "\n"))?$pos+1:strlen($Subject));
8 $Author = $bug_details->get('Author');
9 $Body = $Subject.strip_tags($bug_details->get('Body'));
10 $Rev = $bug_details->get('revision');
11 $State = $bug_details->get('State');
12 $Project = $bug_details->get('Project');
13 $Category = $bug_details->get('Category');
15 $head = $edit?"Editing Bug Number $id":'Add a Bug';
16 $icon = shrink_get_icon($State);
18 <hr>
19 <div class="bug">
21 <h4><span class="<?=$State?>"><?=$icon?></span> <?=$head?></h4>
22 <form method="post">
23 <div class="tags">
24 <div class="metaList">
25 <ul>
26 <?php
27 shrink_show_projects($Project, $count = 5);
29 </ul>
30 </div>
31 <span class="new_type">
32 <a href="#" id="new_project_link" onClick="toggleNew('project')">New Project</a>
33 <input style="display:none" type="text" name="new_project" value="" id="new_project">
34 </span>
35 <div class="label">
36 &nbsp;
37 <!-- Project -->
38 </div>
40 </div>
42 <div class="tags">
43 <div class="metaList">
44 <ul>
45 <?php
46 shrink_show_categories($Category, $count = 5);
48 </ul>
49 </div>
50 <span class="new_type">
51 <a href="#" id="new_category_link" onClick="toggleNew('category')">New Type</a>
52 <input style="display:none" type="text" name="new_category" value="" id="new_category">
53 </span>
54 <div class="label">
55 &nbsp;
56 <!-- Type -->
57 </div>
58 </div>
59 <!-- <input type="text" name="Subject" value="<?=$Subject?$Subject:'Bug title';?>"> -->
60 <div id="help">The first line becomes the Bug's title. You can use <a href="http://daringfireball.net/projects/markdown/" title="Markdown Homepage" target="_new">Markdown</a></div>
61 <textarea id="BugBody" name="Body" cols="50" rows="16"><?=$Body?$Body:'Bug description';?></textarea>
62 <div id="AuthorContainer">
63 <span id="by">by:</span>
64 <?php if ($edit): ?>
65 <?=$Author?>
66 <input type="hidden" id="Author" name="Author" value="<?=$Author?$Author:'Your Name or Email';?>" onFocus="<?=$edit?'':'this.value=\'\';'?>">
67 <?php else: ?>
68 <input type="text" id="Author" name="Author" value="<?=$Author?$Author:'Your Name or Email';?>" onFocus="<?=$edit?'':'this.value=\'\';'?>">
69 <?php endif; ?>
70 </div>
71 <input type="hidden" name="Project" id="Project" value="<?=$Project?>">
72 <input type="hidden" name="Category" id="Category" value="<?=$Category?>">
73 <?php
74 if($edit) {
78 <input type="hidden" name="id" value="<?=$id?>">
79 <input type="hidden" name="rev" value="<?=$Rev?>">
83 <input type="submit" value="<?=$edit?'Save Changes':'Add Bug';?>">
84 </form>
86 <?php
87 if($edit) {
88 $quickAction = ($bug_details->get('State') == 'Closed')?'Open':'Close';
90 <a href="?action=<?=$quickAction?>&id=<?=$id?>"><?=$quickAction?> this Bug</a>
92 <?php } ?>
94 </div>
96 <?php
97 } else {
99 function var_register_post($name)
101 if(isset($_POST[$name])) {
102 return strip_tags(htmlspecialchars($_POST[$name]));
105 return '';
108 function shrink_extract_subject($s)
110 $offset = 1;
112 if(!$pos = strpos($s, "\n")) {
113 return $s;
116 if(isset($s[$pos+2]) && $s[$pos+2] == "\n") {// catch empty newlines
117 $offset++;
120 return substr($s, 0, $pos + $offset);
123 function shrink_extract_meta($Subject)
125 $matches = array();
126 preg_match('/\[([^\s]*)\s?([^\]]*)\]/', $Subject, $matches);
127 return array($matches[1], $matches[2]);
131 $id = var_register_post('id');
132 $revision = var_register_post('rev');
133 $Body = var_register_post('Body');
134 $Project = var_register_post('Project');
135 $Category = var_register_post('Category');
137 $Subject = shrink_extract_subject(strip_tags($Body));
138 $Body = (str_replace(strip_tags($Subject), '', $Body));
140 if(($new_project = var_register_post('new_project')) && ($new_project != '')) {
141 $Project = $new_project;
144 if(($new_category = var_register_post('new_category')) && ($new_category != '')) {
145 $Category = $new_category;
148 shrink_save_meta($Project, $Category);
150 $bug_details = new CouchDocument;
151 $bug_details->id = $id;
152 $bug_details->set('Subject', $Subject);
153 $bug_details->set('Body', $Body);
155 $bug_details->set('CreationDate', date('Y-m-d:H:i:s'));
157 $bug_details->set('Author', var_register_post('Author'));
160 $bug_details->set('Project', ($Project));
161 $bug_details->set('Category', ($Category));
163 $state = var_register_post('State');
164 $bug_details->set('State', empty($state)?'Open':$state);
165 // var_dump($_POST);
166 if(!$couch->save($bug_details, $id, $revision)) {
167 //if revision conflict, show diff:
168 if($couch->lastStatusCode == 409) {
169 $lastRev = $couch->get($id, $options = array("rev" => $revision));
170 $currRev = $couch->get($id);
171 //generalize diff, file_put.., tmpfile
172 file_put_contents('/tmp/bs_diff_1', $lastRev->get('Body'));
173 file_put_contents('/tmp/bs_diff_2', $currRev->get('Body'));
174 $diff = `/usr/bin/diff -u /tmp/bs_diff_1 /tmp/bs_diff_2`;
175 if(strlen($diff) > 0) {
176 ?><h3>There was a revision conflict. See below for a diff. Save anyway?</h3><?
177 echo '<pre id="diff">'.( nl2br($diff) ). '</pre>';
179 } else {
180 echo 'Failed to save your changes, sorry!';
183 // header('Location: index.php');
184 $action = ''; //unset action