/[drupal]/contributions/modules/carbon/carbon_edit.inc
ViewVC logotype

Contents of /contributions/modules/carbon/carbon_edit.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Tue May 5 09:17:36 2009 UTC (6 months, 3 weeks ago) by johnackers
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.6: +3 -6 lines
File MIME type: text/x-php
add replaced with new
1 <?php
2 /*
3 * $Id: carbon_edit.inc,v 1.6 2009/04/29 21:07:28 johnackers Exp $
4 *
5 * Created on 19-Feb-2008
6 *
7 *
8 * Provide the helper functions that support row editing in a
9 * table with ajax transfers. Functions are listed in the order
10 * that they would be invoked.
11 */
12
13 /**
14 * Wrap a standard table theme finction with a form wrapper.
15 * Invoked when the whole table is created.
16 *
17 *
18 * @param $header
19 * @param $rows
20 * @return unknown_type
21 */
22
23 function theme_table_inside_form($header, $rows)
24 {
25 $path = carbon_include_core_js();
26 $output = "<form class='ajaj'>" // must match class that is hard coded in carbon.js
27 . theme("table", $header, $rows)
28 . '</form>';
29 return $output ;
30 }
31
32
33 /**
34 * This function is called to replace a row of fixed data in a table with a
35 * number of form fields that represent the editable fields of the node.
36 *
37 * Menu entry point.
38 *
39 * @param $node
40 * @return unknown_type
41 */
42
43 function carbon_ajax_edit($account, $tablename, $verb, $node)
44 {
45 global $nid_update ; $nid_update = 0 ; // set when node updated/added
46
47 if ($verb == "delete")
48 {
49 node_delete($node->nid); // force new node to be created
50 $fragment = "<tr class='deletion'><td colspan=10>Item deleted</td></tr>";
51 $json = drupal_json($fragment);
52 print($json);
53 exit(0);
54 }
55
56 if ($verb == "clone")
57 {
58 $node->cid = $node->nid ;
59 unset($node->nid); // force new node to be created
60 }
61
62 if ($verb == "add")
63 {
64 $node->cid = $node->nid ;
65 $node->reading = $node->enddate = null ; // zero out key fields
66 unset($node->nid); // force new node to be created
67 }
68
69 $output = drupal_get_form("carbon_editable_row_form", $node, $account, $tablename, "carbon_stamp_submit");
70
71 // todo: presume that status messages might get stripped out which is not ideal!
72 // need to get them back to the user.
73
74 // if we have just completed an update, discard the forms and render a new
75 // row for the table using the same routine that created the original table.
76
77 if ($nid_update > 0)
78 {
79 $singleRow = ($verb == 'add') ? 'insertion' : 'replacement' ;
80 $output = carbon_view_stamp_one($account, $tablename, $nid_update, $singleRow);
81 }
82 // we want to use standard drupal form creation but you cannot use/add the
83 // form tag to an existing page (at least that's what i found).
84 // So the original page should already be wrapped in form tags.
85 // So before injecting the html fragment, we extract the html that is wrapped
86 // by the outside <TD>..col.1..col.2..col.3..</TD> tags.
87
88 $fragment = extract_string($output, "<tr", "</tr>");
89
90 $json = drupal_json($fragment);
91 print($json);
92 exit(0);
93 }
94
95 /**
96 * Cut out a fragment of a string
97 *
98 * @param $haystack the target string
99 * @param $start start marker of desired string
100 * @param $end end marker of desired string (reverse search)
101 * @return string fragment including the start and end markers
102 */
103
104 function extract_string($haystack, $start, $end)
105 {
106 $p1 = strpos($haystack, $start);
107 if ($p1 !== false)
108 {
109 $p2 = strrpos($haystack, $end);
110 if ($p2 !== false)
111 {
112 return substr($haystack, $p1, $p2 + strlen($end) - $p1);
113 }
114 }
115 return t("<tr><td colspan=10>Error: Item not available, refresh page</td></tr>");
116 }
117
118
119 function carbon_editable_row_form($form_state, $node, $account, $tablename, $submit_hook)
120 {
121 $form = array();
122 $form["aid"] = array("#type"=>"hidden", "#name"=>"aid", "#value" => $account->nid);
123 $form["uid"] = array("#type"=>"hidden", "#name"=>"uid", "#value" => $account->uid);
124
125 if ($node != null)
126 {
127 if (isset($node->nid))
128 $form["nid"] = array("#type"=>"hidden", "#name"=>"nid", "#value" => $node->nid);
129 if (isset($node->cid))
130 $form["cid"] = array("#type"=>"hidden", "#name"=>"cid", "#value" => $node->cid);
131 }
132
133 _stamp_form($form, $node, CarbonSources::getDefault());
134
135 // pass $header structure to $form so cols can be rendered in correct order
136
137 $get_header = "_get_".$tablename."_header";
138 $form['#header'] = $get_header(); // adding non standard field
139
140 $form['save'] = array("#type" => "submit", "#value" => "save");
141 // $form['cancel'] = array("#type" => "submit", "#value" => "cancel"); //todo, put in op box
142 $form['#submit'] = array($submit_hook, 'carbon_ajax_submit');
143 return $form ;
144 }
145
146
147 /**
148 *
149 * Invoked by drupal's form routines to render a set of fields that will
150 * evenutually be inserted in a table. Drupal can render this form
151 * without our help, however we want to create a set of minimal set of
152 * fields without titles and descriptions and most importantly rendered
153 * as a set of columns.
154 *
155 * @param $form form that should be rendered
156 * @return html string
157 *
158 */
159
160
161 function theme_carbon_editable_row_form(&$form)
162 {
163 global $cols ; $cols = array();
164
165 // first of all render the fields that are named
166 // in the header
167
168 foreach ($form['#header'] as $h)
169 {
170 $fieldname = isset($h['form']) ? $h['form'] : $h['field'] ;
171
172 $field = &$form[$fieldname]; // $field is large array here
173 if (!isset($field))
174 $field = &$form['stamp'][$fieldname]; // $field is large array here
175
176 if (is_array($field))
177 {
178 if ($field["#type"] == "hidden") // save hidden fields
179 continue ;
180
181 if ($field["#type"] == "submit") // save submit buttons
182 continue;
183
184 // prevent title and description being placed around field
185 $fcopy = $field ; // this should create new copy
186 unset($fcopy['#title']) ; unset($fcopy['#description']);
187
188 $cols[] .= drupal_render($fcopy);
189 }
190 else
191 $cols[] = t("not avail.");
192
193 $field['#printed'] = true ; // mark as done
194 }
195 // put all the submit buttons in one column
196
197 foreach($form as $key => $f)
198 {
199 if ($f["#type"] == "submit")
200 $cols[sizeof($cols)-1] = drupal_render($f) . " " ;
201 }
202 // at this stage, we have handled all the fields required
203 // for the table. But markup the other fields as hidden.
204
205 _markFieldsAsHidden($form);
206
207 // add the tokens, unique form identifier etc and render the rest of the form
208 $other_fields = drupal_render($form); // the whole form has been rendered
209
210 // shove the other fields in the last column,
211 // we don't want to put an extra column into
212 // the existing table
213
214 if (sizeof($cols) == 0) $cols[0] = "" ;
215 $cols[sizeof($cols)-1] .= $other_fields ;
216
217 // render form in a table with one row
218 // we'll extract the signle row later on.
219
220 $class = "replacement" ;
221 return theme_table(array(), array(array("data" => $cols, 'id'=> 0, 'class'=> $class)));
222 }
223
224
225 /**
226 * We don't want to render all fields because there isn't room in each
227 * row of the table so mark them as #hidden so they are not rendered
228 * but the default values (like scope) are available for saving.
229 *
230 * @param $form
231 * @return undefined
232 */
233
234
235 function _markFieldsAsHidden(&$form)
236 {
237 foreach($form as $key => &$f)
238 {
239 if (!is_array($f))
240 continue ; // never do anything with a single attribute
241
242 if ($f["#printed"]) // skip rendered fields
243 continue ;
244
245 if ($f["#type"] == "hidden") // keep hidden fields
246 continue ;
247
248 if ($f["#type"] == "token") // keep token fields
249 continue ;
250
251 if ($f['#type'] == 'fieldset')
252 {
253 _markFieldsAsHidden($form[$key]);
254 $f['#printed'] = true ;
255 continue ;
256 }
257 $f['#type'] = 'hidden' ;
258 }
259 }
260
261
262
263 function form_render($col)
264 {
265 if (is_array($col)) return $col[0];
266 return $col ;
267 }
268
269 /**
270 * create the links for different operations available on each line
271 * on the table
272 *
273 * @param $account
274 * @param $tablename
275 * @param $node
276 * @param $query
277 * @return unknown_type
278 */
279
280
281 function _format_link_insitu_edit($account, $tablename, $node, $query="")
282 {
283 return _fancy_link($node,"", "edit stamp",
284 "carbon/".$account->nid."/".$tablename."/edit/".$node->nid, $query, "edit").
285 _fancy_link($node,"","copy stamp, attach to this account and then edit",
286 "carbon/".$account->nid."/".$tablename."/clone/".$node->nid, $query, "clone").
287 _fancy_link($node,"","delete stamp",
288 "carbon/".$account->nid."/".$tablename."/delete/".$node->nid, $query, "delete") ;
289 }
290
291
292 function _format_link_insitu_add($account, $tablename, $node)
293 {
294 $nid = isset($node) ? $node->nid : "" ;
295 return _fancy_link($node,t("New"), "add a new stamp",
296 "carbon/".$account->nid."/".$tablename."/add/".$nid, "", "new");
297 }
298
299
300
301 ?>

  ViewVC Help
Powered by ViewVC 1.1.2