CMS  Version 3.9
menu_move_form.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
34 /**************************************************************
35  *
36  * Description: Move a page within the menu
37  * hierarchy.
38  *
39  * Reshma Joshi for Sonjara, Inc.
40  *
41  * modified, Janice Gallant for Sonjara, Inc., adapted
42  * from help page code.
43  *
44  * 9/30/2010
45  *
46  ****************************************************************/
47 
48 
49 Fakoli::using("menu", "site", "role", "page", "field_renderers");
50 Fakoli::usingFile("framework/auto_form.inc","framework/tree.inc");
51 
52 $menu_item_id = checkNumeric($_GET["menu_item_id"]);
53 $parent_id = checkNumeric($_GET["parent_id"]);
54 
56 
57 if ($menu_item_id)
58 {
59  $menuItem->load($menu_item_id);
60  $title = "Move Menu Item: {$menuItem->title}";
61 }
62 else
63 {
64  redirect("menus");
65 }
66 
67 $form = new AutoForm($menuItem);
68 
69 $form->hide("title", "page_id", "sort_order", "identifier", "menu_id", "url", "role", "permissions", "required_parameters");
70 $form->alias("parent_id", "Move Menu Item To: ");
72 
73 $redirect = "/admin/menu_item_form?menu_item_id={$menu_item_id}&parent_id={$parent_id}";
74 $form->button("Cancel", $redirect);
75 
76 if($parent_id)
77  $form->submitLabel = "Move Page";
78 
79 $menuItems = Query::create(MenuItem, "WHERE menu_id=:m ORDER BY sort_order")
80  ->bind(":m", $menuItem->menu_id)
81  ->execute();
82 
83 $menuItemTree = new TreeControl("parent_id");
84 $menuItemTree->width = 500;
85 $menuItemTree->height = 280;
86 $menuItemTree->selectMode = "single";
87 
88 if ($method == "POST")
89 {
90  if($form->save())
91  redirect($redirect);
92 }
93 
94 if (count($menuItems) > 0)
95 {
96  $root_checked = ($parent_id == 0);
97  $emptyNode = new TreeNode("emptyNode", "Top Level", "0", $root_checked, "tree_node_closed", "tree_node_open");
98  $menuItemTree->add($emptyNode);
99 
100  foreach ($menuItems as $menuItem)
101  {
102  $checked = ($menuItem->menu_item_id == $parentkey);
103 
104  if( ($menuItem->menu_item_id == $menu_item_id ) || ($menuItem->parent_id == $menu_item_id) )
105  {
106  $node = new TreeNode("menuItemPage_{$menuItem->menu_item_id}", $menuItem->title, "", false, "tree_node_closed", "tree_node_open");
107  }
108  else
109  {
110  $node = new TreeNode("menuItemPage_{$menuItem->menu_item_id}", $menuItem->title, $menuItem->menu_item_id, $checked, "tree_node_closed", "tree_node_open");
111  }
112 
113  $tmp = Array(
114  'parent' => $menuItem->parent_id,
115  'node' => $node);
116  $displays[$menuItem->menu_item_id] = $tmp;
117  }
118 
119  foreach ($displays as $display)
120  {
121  if (empty($display['parent']))
122  {
123  $menuItemTree->add($display['node']);
124  }
125  else
126  {
127  $parentNode = $displays[ $display['parent'] ]['node'];
128  $parentNode->add($display['node']);
129  }
130  }
131 }
132 
133 $treeSelect = new TreeSelectFieldRenderer($form, "parent_id", $menuItemTree);
134 
135 echo
136 $script = $form->writeScript();
137 $script .= "<link type='text/css' rel='stylesheet' href='/css/tree.css'/>";
138 ?>
139 <h4>Note: when you move a page, that page's child pages will move with it. The page's roles will change to match its new position: any roles selected for this page that are not also selected for its new parent page will be removed.</h4>
140 <?
141 $form->drawForm();
142 
143 /*
144  * We need to update this page's and its child pages to have the
145  * same roles as the new parent page. If the parent page does not
146  * have one or more roles that this page has, then we must
147  * remove that role from this page and its child pages.
148  *
149  */
151 {
152  $menuItem = $form->data;
153  $parent = $menuItem->Parent();
154 
155  $tobeRemoved = getRoleDiff($menuItem->role, $parent->role);
156 
157  updateChildRoles($menuItem, $tobeRemoved);
158 }
159 
160 
161 /*
162  * Get the roles that are in the first string but not in the 2nd.
163  */
164 function getRoleDiff($string1, $string2)
165 {
166  $in1Not2 = array();
167 
168  if($string1 != $string2)
169  {
170  $roles1 = ($string1) ? explode(",", $string1) : array();
171  $roles2 = ($string2) ? explode(",", $string2) : array();
172 
173  // get roles that are in roles1 but not in roles2
174  $in1Not2 = array_diff($roles1, $roles2);
175  }
176 
177  return $in1Not2;
178 }
179 
180 
181 function updateChildRoles($menuItem, $tobeRemovedRoles)
182 {
183  if(count($tobeRemovedRoles) > 0)
184  {
185  $childPages = array();
186  $childPages = getChildMenuItems($menuItem, $childPages);
187  foreach($tobeRemovedRoles as $oldRole)
188  {
189  foreach($childPages as $child)
190  {
191  $child->role = preg_replace("/$oldRole,*/", "", $child->role);
192  $child->filter = new InclusionFilter("role");
193 
194  // remove comma as first char, if it exists
195  $child->role = preg_replace("/^,*/", "", $child->role);
196  $child->save();
197  }
198  }
199  }
200 }
201 
202 
203 /*
204  * Recursively create an array of all child pages of $menuItem down to the
205  * leaf page.
206  */
207 function getChildMenuItems($menuItem, &$childPages)
208 {
209  $children = $menuItem->Children();
210 
211  if($children)
212  {
213  foreach($children as $child)
214  {
215  array_push($childPages, $child);
216  getChildPages($child, $childPages);
217  }
218  }
219 
220  return $childPages;
221 }
222 ?>
$tabs page
$displays
Definition: menu_items.inc:59
$parent
Definition: templates.inc:42
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
$method
Pull out a simple reference to the request method.
Definition: core.inc:1573
$forumRequest to
$parent_id
getChildMenuItems($menuItem, &$childPages)
UpdateRolesToMatchNewParent($form)
if($method=="POST") if(count($menuItems) > 0) $treeSelect
getRoleDiff($string1, $string2)
$menu_item_id
$menuItemTree
$menuItem
$redirect
updateChildRoles($menuItem, $tobeRemovedRoles)
$menuItems