Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_site_special_page.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 
31 {
32 
33 
54  public static function execute($settings, &$state)
55  {
56  if (empty($state['asset'])) {
57  // grab the asset if assetid is given, but not the asset.
58  if (empty($state['assetid'])) {
59  return FALSE;
60  } else {
61  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
62  }
63  }
64 
65  $use_direct_parent_sites = array_get_index($settings, 'use_direct_parent_sites', 0);
66 
67  if (($use_direct_parent_sites == 0) && (empty($settings['site_assetid']) || !$GLOBALS['SQ_SYSTEM']->am->assetExists($settings['site_assetid']))) {
68  return FALSE;
69  }
70 
71  //get the asset to be set as special page
72  $special_page = $state['asset'];
73 
74  //get site assets
75  $sites = Array();
76  if ($use_direct_parent_sites == 1) {
77  //get the links of the direct (type 1 or 2) parent assets that are of type site
78  $parent_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($special_page->id, SQ_LINK_TYPE_1|SQ_LINK_TYPE_2, 'site', TRUE, 'minor');
79 
80  //if there is no direct parent assets of type site, return FALSE
81  if (empty($parent_links)) {
82  return FALSE;
83  }
84 
85  //put the direct parent sites in the array
86  foreach ($parent_links as $parent_link) {
87  $sites[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_link['majorid']);
88  }
89 
90  } else {
91  $sites[] = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['site_assetid']);
92  }
93 
94  $special_page_type = array_get_index($settings, 'special_page_type', 'index');
95  $override = array_get_index($settings, 'override_special_page', 0);
96 
97  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
98  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
99  $success = TRUE;
100  $return_array = Array();
101 
102  foreach ($sites as $site) {
103  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($site->id, SQ_LINK_NOTICE, '', FALSE, $special_page_type);
104 
105  //if there is a current link
106  if (!empty($link)) {
107  //if the overriding flag is not set or the current special page of the site is already the one to be set, do nothing except adding values to the return array
108  if (!$override || ($link['minorid'] == $special_page->id)) {
109  $return_array[] = Array(
110  'assetid' => $link['minorid'],
111  'site_assetid' => $site->id,
112  'special_page_type' => $special_page_type,
113  );
114  continue;
115  }
116 
117  //if the overriding flag is set, delete the current link
118  $success = (bool)$site->deleteLink($link['linkid']);
119  }
120 
121  if ($success) {
122  $success = (bool)$site->createLink($special_page, SQ_LINK_NOTICE, $special_page_type);
123  }
124 
125  //if there is error, break out of the loop and rollback the transaction
126  if ($success) {
127  $return_array[] = Array(
128  'assetid' => $special_page->id,
129  'site_assetid' => $site->id,
130  'special_page_type' => $special_page_type,
131  );
132  } else {
133  break;
134  }
135  }
136 
137  $GLOBALS['SQ_SYSTEM']->doTransaction($success ? 'COMMIT' : 'ROLLBACK');
138  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
139 
140  return ($success? $return_array : FALSE);
141 
142  }//end execute()
143 
144 
155  public static function getInterface($settings, $prefix, $write_access=FALSE)
156  {
157  $site_assetid = array_get_index($settings, 'site_assetid', 0);
158  $site_exists = FALSE;
159  if ($site_assetid) {
160  $site_exists = $GLOBALS['SQ_SYSTEM']->am->assetExists($site_assetid);
161  }
162 
163  $form_element_extras = '';
164  if (!$write_access) {
165  $form_element_extras = 'disabled="disabled"';
166  }
167 
168  //get the HTML string of the special page dropdown box
169  ob_start();
170  combo_box($prefix.'[special_page_type]', self::_getSpecialPages(), FALSE, array_get_index($settings, 'special_page_type', 'index'), NULL, $form_element_extras);
171  $special_page_selection = ob_get_clean();
172 
173  //get the HTML string of the site asset finder
174  ob_start();
175  if ($write_access) {
176  asset_finder($prefix.'[site_assetid]', $site_assetid, Array('site' => 'I'));
177  } else {
178  if ($site_assetid) {
179  if ($site_exists) {
180  echo get_asset_tag_line($site_assetid);
181  } else {
182  echo '<span class="sq-backend-warning">Unknown asset (Id: #'.$site_assetid.')</span>';
183  }
184  } else {
185  echo '<b>No Asset ID specified</b>';
186  }
187  }
188  $site_asset_selection = ob_get_clean();
189 
190  //get the HTML string of the Override checkbox
191  ob_start();
192  check_box($prefix.'[override_special_page]', '1', array_get_index($settings, 'override_special_page', 0), NULL, $form_element_extras);
193  label(translate('trigger_action_set_site_special_page_override_special_page'), $prefix.'[override_special_page]');
194  $override_special_page_checkbox = ob_get_clean();
195 
196  //get the HTML string of the Use the direct parent site checkbox
197  ob_start();
198  check_box($prefix.'[use_direct_parent_sites]', '1', array_get_index($settings, 'use_direct_parent_sites', 0), NULL, $form_element_extras);
199  label(translate('trigger_action_set_site_special_page_use_direct_parent_sites'), $prefix.'[use_direct_parent_sites]');
200  $use_direct_parent_sites_checkbox = ob_get_clean();
201 
202  $contents = translate('trigger_action_set_site_special_page_basic', $special_page_selection, $site_asset_selection)
203  .'<br /><br />'
204  .$override_special_page_checkbox
205  .'<br />'
206  .$use_direct_parent_sites_checkbox;
207 
208  return $contents;
209 
210  }//end getInterface()
211 
212 
224  public static function processInterface(&$settings, $request_data)
225  {
226  //the special page type must be in the special page type list
227  $special_page_type = array_get_index($request_data, 'special_page_type', 'index');
228  if (!key_exists($special_page_type, self::_getSpecialPages())) {
229  return translate('trigger_action_set_site_special_page_special_page_type_invalid');
230  }
231 
232  $use_direct_parent_sites = array_get_index($request_data, 'use_direct_parent_sites', 0);
233  //make sure the site assetid isn't blank if "something" is not selected
234  if (($use_direct_parent_sites == 0) && empty($request_data['site_assetid']['assetid'])) {
235  return translate('trigger_action_set_site_special_page_site_missing');
236  }
237 
238  $settings['special_page_type'] = $special_page_type;
239  $settings['site_assetid'] = $request_data['site_assetid']['assetid'];
240  $settings['override_special_page'] = array_get_index($request_data, 'override_special_page', 0);
241  $settings['use_direct_parent_sites'] = $use_direct_parent_sites;
242 
243  return FALSE;
244 
245  }//end processInterface()
246 
247 
254  public static function _getSpecialPages()
255  {
256  return Array('index' => 'Index', 'not_found' => 'Not Found', 'archive' => 'Archived Asset');
257 
258  }//end _getSpecialPages()
259 
260 
261 }//end class
262 
263 ?>