/[drupal]/contributions/modules/outline_designer/ajax.php
ViewVC logotype

Contents of /contributions/modules/outline_designer/ajax.php

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


Revision 1.1.1.1 - (show annotations) (download) (as text) (vendor branch)
Thu Mar 27 20:24:37 2008 UTC (19 months, 4 weeks ago) by btopro
Branch: MAIN
CVS Tags: DRUPAL-5--1-3-2, DRUPAL-5--1-3-1, DRUPAL-5--1-0-BETA2, DRUPAL-5--1-2-3, DRUPAL-5--1-0-BETA1, DRUPAL-5--1-2-1-BETA2, DRUPAL-5--1-3, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.1: +0 -0 lines
File MIME type: text/x-php
Oops, forgot to include the ajax.php file that is REQUIRED to work.
1 <?php
2 //ELMS: Outline Designer - Ajax book / general usability improvements for Drupal 5.x
3 //Copyright (C) 2008 The Pennsylvania State University
4 //
5 //Bryan Ollendyke
6 //bto108@psu.edu
7 //
8 //Keith D. Bailey
9 //kdb163@psu.edu
10 //
11 //12 Borland
12 //University Park, PA 16802
13 //
14 //This program is free software; you can redistribute it and/or modify
15 //it under the terms of the GNU General Public License as published by
16 //the Free Software Foundation; either version 2 of the License, or
17 //(at your option) any later version.
18 //
19 //This program is distributed in the hope that it will be useful,
20 //but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 //GNU General Public License for more details.
23 //
24 //You should have received a copy of the GNU General Public License along
25 //with this program; if not, write to the Free Software Foundation, Inc.,
26 //51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require_once './includes/bootstrap.inc';
28
29 if(isset($_POST['action'])){
30 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
31 switch($_POST['action']){
32 //in: node title, node parent id
33 //action: Check the parent / nodes around this one we're inserting and guess about what content type to use, otherwise make it the system default
34 //out: node id, node type, icon to render
35 case 'add_node':
36 $node->title = $_POST['title'];
37 $node->status = 1;
38 $node->uid = $user->uid;
39 //take into account that we could be creating a new book
40 if($_POST['parent'] == 0 || !user_access('change content types')){
41 $node->type = variable_get("outline_designer_default_type", "page");
42 }else{
43 //figure out if there are currently any children
44 //if there aren't then just give the child the parent's properties
45 $result = db_query("SELECT type FROM node JOIN book ON book.vid = node.vid WHERE parent=" . $_POST['parent'] . " ORDER BY weight DESC");
46 $num_rows = db_num_rows($result);
47 if($num_rows == 0){
48 $parent = node_load($_POST['parent']);
49 if(user_access('create ' . $parent->type . ' content')){
50 $node->type = $parent->type;
51 }else{
52 $node->type = variable_get("outline_designer_default_type", "page");
53 }
54 }else{
55 $value = db_fetch_array($result);
56 if(user_access('create ' . $value['type'] . ' content')){
57 $node->type = $value['type'];
58 }else{
59 $node->type = variable_get("outline_designer_default_type", "page");
60 }
61 }
62 }
63 //set these incase this is the book type which uses them on save, otherwise these are ignored the first time
64 $node->weight = -15;
65 $node->parent = $_POST['parent'];
66 node_save($node);
67 //we need to make sure this isn't of type book or else it will cause a duplicate key entry error because it's save hook has already used the parent value where as non-book types don't the first time
68 if($node->type != 'book'){
69 db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $_POST['parent'], -15);
70 }
71 if($node->type == 'book'){
72 $term = 'pages';
73 }else{
74 $term = 'content';
75 }
76 if(user_access('edit ' . $node->type . ' ' . $term) || user_access('edit own ' . $node->type . ' ' . $term)){
77 $allow_edit = 1;
78 }else{
79 $allow_edit = 0;
80 }
81 print serialize(array($node->nid,$node->type,variable_get("outline_designer_" . $node->type . "_icon",drupal_get_path('module','outline_designer') . "/images/node.png"),$allow_edit));
82 break;
83 //in: node id, node type name
84 //action: change node type and resave
85 //out: icon to render
86 case 'change_type':
87 $node = node_load($_POST['nid']);
88 $type = db_result(db_query("SELECT type FROM node_type WHERE name='" . $_POST['new_type'] . "'"));
89 if($node->type != $type){
90 $node->type = $type;
91 node_save($node);
92 }
93 print variable_get("outline_designer_" . $node->type . "_icon",drupal_get_path('module','outline_designer') . "/images/node.png");
94 break;
95 //in: list of node ids to delete
96 //action: delete nodes
97 //out: completion response
98 case 'delete':
99 $ary = unserialize($_POST['ids']);
100 for($i=0;$i<count($ary);$i++){
101 node_delete($ary[$i]);
102 }
103 print 'Nodes successfully removed!';
104 break;
105 //in: node id moved, new parent id it was moved under
106 //action: change node parent
107 //out: nothing
108 case 'drag_drop_update':
109 $node = node_load($_POST['nid']);
110 $node->parent = $_POST['parent'];
111 $node->log = 'Outline Designer reweighting update on drag-and-drop';
112 node_save($node);
113 break;
114 //in: root node id
115 //action: duplicate the node and
116 //out: new root node
117 case 'duplicate_nodes':
118 $tree = array();
119 //pull the whole node tree but only get the nid's
120 $value = db_fetch_array(db_query("SELECT node.nid FROM book JOIN node ON node.vid=book.vid WHERE node.nid=" . $_POST['root']));
121 $tree[$value['nid']] = 0;
122 $tree = _outline_designer_recurse_duplicate_nodes($_POST['root'],$tree);
123 //load each node in the node tree
124
125 foreach($tree as $old_nid => $new_nid){
126 $node = node_load($old_nid);
127 $node->nid = null;
128 $node->created = null;
129 if($old_nid == $_POST['root']){
130 //do nothing to the parent because this accounts for coping just a branch
131 $node->title = "Duplicate of " . $node->title;
132 }else{
133 $node->parent = $tree[$node->parent];
134 }
135 node_save($node);
136 if($node->type != 'book'){
137 db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
138 }
139 $tree[$old_nid] = $node->nid;
140 }
141 //return the new root
142 print_r($tree[$_POST['root']]);
143 break;
144 //in: nothing
145 //action: determine what node types the user is allowed to create / switch their content to. User's ability to change content type is querried, then system defined variable list, and then takes user create type content permissions into account
146 //out: node type names and icon to render
147 case 'get_icons':
148 if(user_access('change content types')){
149 $ary = array();
150 $ary2 = array();
151 $ary = variable_get('outline_designer_content_types',array('page'));
152 foreach($ary as $value){
153 $name = db_result(db_query("SELECT name FROM node_type WHERE type='" . $value . "'"));
154 //only allow users with the create "type" privlege perform this operation
155 if(user_access("create $value content")){
156 array_push($ary2,array($name,variable_get("outline_designer_" . $value . "_icon",drupal_get_path('module','outline_designer') . "/images/node.png")));
157 }
158 }
159 print serialize($ary2);
160 }
161 break;
162 //in:nothing
163 //action: find all nodes without parents (these are the root nodes)
164 //out: all the node roots
165 case 'get_book_roots':
166 $roots = array();
167 $result = db_query("SELECT node.nid,title FROM node JOIN book ON book.vid = node.vid WHERE parent=0 ORDER BY node.nid");
168 while($value = db_fetch_array($result)){
169 array_push($roots,array($value['nid'],$value['title']));
170 }
171 print serialize($roots);
172 break;
173 //in: root node id
174 //action: load all nodes from a root node
175 //out: node id, parent association, title, icon to render, allow edit
176 case 'load_tree':
177 //pull everything
178 $tree = array();
179 $value = db_fetch_array(db_query("SELECT node.uid,node.nid,parent,title,type FROM book JOIN node ON node.vid=book.vid WHERE node.nid=" .$_POST['nid'] . " ORDER BY weight"));
180 if($value['type'] == 'book'){
181 $term = 'pages';
182 }else{
183 $term = 'content';
184 }
185 if(user_access('edit ' . $value['type'] . ' ' . $term)){
186 $allow_edit = 1;
187 }elseif($user->uid = $value['uid'] && user_access('edit own ' . $value['type'] . ' ' . $term)){
188 $allow_edit = 1;
189 }else{
190 $allow_edit = 0;
191 }
192 array_push($tree,array($value['nid'],$value['parent'],$value['title'],variable_get("outline_designer_" . $value['type'] . "_icon",drupal_get_path('module','outline_designer') . "/images/node.png"),$allow_edit));
193 $tree = _outline_designer_tree_recurse($_POST['nid'],$tree);
194 print serialize($tree);
195 break;
196 //in: node id, title
197 //action: rename a single node
198 //out: nothing
199 case 'rename':
200 $node = node_load($_POST['nid']);
201 $node->title = $_POST['newtitle'];
202 $node->revision = 1;
203 node_save($node);
204 break;
205 //in: array of arrays with node id, parent id, weight, and title of each node
206 //action: save all nodes currently displayed
207 //out: array of node id and icon to render
208 case 'save_tree':
209 $ary = unserialize($_POST['tree']);
210 $type = array();
211 for($no=0;$no<count($ary);$no++){
212 $nid = $ary[$no][0];
213 $parent = $ary[$no][1];
214 $weight = $no;
215 $title = $ary[$no][2];
216 $node = node_load($nid);
217 //figure out if the type is set and make it the same as the level it's currently on
218 //if it doesn't have a type that means it's new and we should update instead of create
219 if($node->type == ''){
220 $node->type = variable_get("outline_designer_default_type", "page");
221 print serialize(array($node->nid,variable_get("outline_designer_" . $node->type . "_icon",drupal_get_path('module','outline_designer') . "/images/node.png")));
222 }else{
223 //try to set the nid, this way if the parent is referenced later it will take
224 //the properties of the parent
225 $type[$nid] = array($node->type);
226 //associate the parent id to the node type of the elements under it (by default)
227 //this will also override the parent's settings to default to the children
228 //if they end up existing
229 $type[$parent] = array($node->type);
230 }
231 //weight is currently being hacked to fit our new book purposes. It will render correctly but never be able to update correctly if it goes beyond 31 in any branch
232 //weight orders them on the page and then they are indented based on who their parent is
233 if($node->weight != $weight || $node->parent != $parent || $node->title != $title){
234 $node->weight = $weight;
235 $node->parent = $parent;
236 $node->title = $title;
237 $node->revision = 1;
238 node_save($node);
239 }
240 }
241 break;
242 //in: node id, weight
243 //action: alter weights across all nodes
244 //out: nothing
245 case 'update_weights':
246 $ary = unserialize($_POST['weight']);
247 foreach($ary as $weight){
248 $node = node_load($weight[1]);
249 $node->weight = $weight[0];
250 $node->log = 'Outline Designer reweighting update';
251 node_save($node);
252 }
253 break;
254 }
255 }
256 ?>

  ViewVC Help
Powered by ViewVC 1.1.2