next major version. uses the latest CouchDb PHP Library. Lots of interface goodies...
[couchdbimport.git] / CouchProjects / Demos / php / BugShrink / newBug.php
bloba46c7d317301d1a2212f685ed42470fdb7c18b64
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 = 100);
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 = 100);
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 <?php else: ?>
67 <input type="text" id="Author" name="Author" value="<?=$Author?$Author:'Your Name or Email';?>" onFocus="<?=$edit?'':'this.value=\'\';'?>">
68 <?php endif; ?>
69 </div>
70 <input type="hidden" name="Project" id="Project" value="<?=$Project?>">
71 <input type="hidden" name="Category" id="Category" value="<?=$Category?>">
72 <?php
73 if($edit) {
77 <input type="hidden" name="id" value="<?=$id?>">
78 <input type="hidden" name="rev" value="<?=$Rev?>">
82 <input type="submit" value="<?=$edit?'Save Changes':'Add Bug';?>">
83 </form>
85 <?php
86 if($edit) {
87 $quickAction = ($bug_details->get('State') == 'Closed')?'Open':'Close';
89 <a href="?action=<?=$quickAction?>&id=<?=$id?>"><?=$quickAction?> this Bug</a> | <a href="?action=delete&id=<?=$id?>" onClick="return confirm('Are you sure?')">Delete this Bug</a>
91 <?php } ?>
93 </div>
95 <?php
96 } else {
98 function var_register_post($name)
100 if(isset($_POST[$name])) {
101 return strip_tags(htmlspecialchars($_POST[$name]));
104 return '';
107 function shrink_extract_subject($s)
109 $offset = 1;
111 if(!$pos = strpos($s, "\n")) {
112 return $s;
115 if(isset($s[$pos+2]) && $s[$pos+2] == "\n") {// catch empty newlines
116 $offset++;
119 return substr($s, 0, $pos + $offset);
122 function shrink_extract_meta($Subject)
124 $matches = array();
125 preg_match('/\[([^\s]*)\s?([^\]]*)\]/', $Subject, $matches);
126 return array($matches[1], $matches[2]);
130 $id = var_register_post('id');
131 $revision = var_register_post('rev');
132 $Body = var_register_post('Body');
133 $Project = var_register_post('Project');
134 $Category = var_register_post('Category');
136 $Subject = shrink_extract_subject(strip_tags($Body));
137 $Body = (str_replace(strip_tags($Subject), '', $Body));
139 if(($new_project = var_register_post('new_project')) && ($new_project != '')) {
140 $Project = $new_project;
143 if(($new_category = var_register_post('new_category')) && ($new_category != '')) {
144 $Category = $new_category;
147 shrink_save_meta($Project, $Category);
149 $bug_details = new CouchDocument;
150 $bug_details->id = $id;
151 $bug_details->set('Subject', $Subject);
152 $bug_details->set('Body', $Body);
154 $bug_details->set('CreationDate', date('Y-m-d:H:i:s'));
155 $bug_details->set('Author', var_register_post('Author'));
156 $bug_details->set('Project', ($Project));
157 $bug_details->set('Category', ($Category));
159 $state = var_register_post('State');
160 $bug_details->set('State', empty($state)?'Open':$state);
161 // var_dump($_POST);
162 if(!$couch->save($bug_details, $id, $revision)) {
163 //if revision conflict, show diff:
164 if($couch->lastStatusCode == 409) {
165 $lastRev = $couch->get($id, $options = array("rev" => $revision));
166 $currRev = $couch->get($id);
167 //generalize diff, file_put.., tmpfile
168 file_put_contents('/tmp/bs_diff_1', $lastRev->get('Body'));
169 file_put_contents('/tmp/bs_diff_2', $currRev->get('Body'));
170 $diff = `/usr/bin/diff -u /tmp/bs_diff_1 /tmp/bs_diff_2`;
171 if(strlen($diff) > 0) {
172 ?><h3>There was a revision conflict. See below for a diff. Save anyway?</h3><?
173 echo '<pre id="diff">'.( nl2br($diff) ). '</pre>';
175 } else {
176 echo 'Failed to save your changes, sorry!';
179 // header('Location: index.php');
180 $action = ''; //unset action