Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
asset_status_editing.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_status/asset_status.inc';
18 
37 {
38 
39 
45  function Asset_Status_Editing($owner)
46  {
48  $this->status_tag = SQ_STATUS_EDITING;
49 
50  }//end constructor
51 
52 
60  public static function getDescription()
61  {
62  return translate('status_editing');
63 
64  }//end getDescription()
65 
66 
74  function getAvailableStatii()
75  {
76  $statii = Array();
77 
78  // cant do anything without write access
79  if (!$this->owner->writeAccess('')) return $statii;
80 
81  // any editor can cancel the safe edit
82  $statii[SQ_STATUS_LIVE] = translate('status_change_cancel_safe_edit');
83 
84  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
85  $running_schemas = $wfm->getSchemas($this->owner->id, true, true);
86 
87  if (!empty($running_schemas)) {
88  // workflow is currently running, but it should not be
89  // because EDITING does not require workflow
90  pre_echo('FIXME: SOMETHING HAS GONE WRONG HERE');
91  } else {
92  // workflow is not currently running for this asset
93  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
94  if ($mm->allowsMetadata($this->owner->id)) {
95  $is_dependant = $GLOBALS['SQ_SYSTEM']->am->isDependant($this->owner->id);
96 
97  if (($mm->requiredFieldsComplete($this->owner->id) && !$is_dependant) || $is_dependant) {
98  // metadata is complete
99  $schemas = $wfm->getSchemas($this->owner->id, true);
100  if (empty($schemas)) {
101  // no workflow schemas applied, so any admin can make this live
102  if ($this->owner->adminAccess('')) {
103  $statii[SQ_STATUS_EDITING_APPROVED] = translate('status_change_approve');
104  }
105  } else {
106  // workflow applied but not running;
107  // see whether the current user could publish if we started workflow
108  if ($this->owner->adminAccess('')) {
109  // If we are an admin, we should test all the streams
110  // to see if some need workflow and others don't
111  $needs_workflow = FALSE;
112  $wf_complete = FALSE;
113 
114  foreach ($schemas as $schemaid) {
115  $streams = $wfm->getStreams($schemaid);
116 
117  foreach ($streams as $streamid => $stream_name) {
118  $publish_result = $wfm->testPublish($this->owner->id, $GLOBALS['SQ_SYSTEM']->currentUserId(), $stream_name);
119  if ($publish_result === TRUE) {
120  $wf_complete = TRUE;
121  } else {
122  $needs_workflow = TRUE;
123  }
124  }
125  }
126  } else {
127  // If we are a normal user, we can only use the default
128  // stream, so only test that instead
129  $wf_complete = $wfm->testPublish($this->owner->id, $GLOBALS['SQ_SYSTEM']->currentUserId(), '');
130  $needs_workflow = !$wf_complete;
131  }
132 
133  // We could have BOTH Approve and Apply for Approval,
134  // if some streams can be approved straight off and
135  // some must go through workflow
136  if ($wf_complete === TRUE) {
137  $statii[SQ_STATUS_EDITING_APPROVED] = translate('status_change_approve');
138  }
139  if ($needs_workflow === TRUE) {
140  $statii[SQ_STATUS_EDITING_APPROVAL] = translate('status_change_apply_for_approval');
141  }
142  }
143  } else {
144  // can't grant approval yet because owner is a independent asset with incomplete metadata
145  $statii[SQ_STATUS_EDITING_APPROVAL] = translate('status_change_apply_for_approval');
146  }
147 
148  } else {
149  // this asset needs to allow both statuses because it cant check metadata requirements
150  // due to it not allowing metadata
151  $statii[SQ_STATUS_EDITING_APPROVED] = translate('status_change_approve');
152  $statii[SQ_STATUS_EDITING_APPROVAL] = translate('status_change_apply_for_approval');
153  }
154  }//end if workflow running
155 
156  return $statii;
157 
158  }//end getAvailableStatii()
159 
160 
168  {
169  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
170  if ($mm->allowsMetadata($this->owner->id) && !$mm->requiredFieldsComplete($this->owner->id)) {
171  return translate('cannot_approve_metadata_incomplete', '<a href="'.htmlspecialchars(replace_query_string_vars(Array('asset_ei_screen' => 'metadata'))).'">'.strtolower(translate('metadata')).'</a>');
172  }
173  return '';
174 
175  }//end getStatusChangeMessage()
176 
177 
188  function processStatusChange($new_status, $update_parent=true)
189  {
190  if (!parent::processStatusChange($new_status, $update_parent)) {
191  return false;
192  }
193 
194  switch ($new_status) {
195 
196  case SQ_STATUS_LIVE :
197  // cancelling a safe edit
198  if (!$this->owner->revertToSystemVersion()) {
199  return false;
200  }
201 
202  // send an internal message
203  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
204  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
205  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($GLOBALS['SQ_SYSTEM']->currentUserId());
206 
207  if (!$wfm->silentWorkflowParty($this->owner->id)) {
208  $log = $ms->newMessage();
209  $msg_reps = Array(
210  'user_name' => $user->name,
211  'workflow_user' => $user->name,
212  'type_code' => $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->owner->type(), 'name'),
213  'asset_name' => $this->owner->name,
214  'status' => get_status_description($new_status),
215  );
216  $log->replacements = $msg_reps;
217  $log->type = 'asset.workflow.log.safeedit.cancel';
218  $log->parameters['assetid'] = $this->owner->id;
219  $log->parameters['version'] = substr($this->owner->version, 0, strrpos($this->owner->version, '.'));
220  $log->send();
221  }
222 
223  return true;
224 
225  break;
226 
227  case SQ_STATUS_EDITING_APPROVAL :
228  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
229 
230  // we are going to make this asset live by starting the workflow process
231  $current_userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
232 
233  if (!$wfm->startWorkflow($this->owner->id)) {
234  trigger_localised_error('SYS0262', E_USER_WARNING, $this->owner->name);
235  return false;
236  }
237 
238  // send an internal message
239  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
240  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($current_userid);
241  $msg_reps = Array();
242 
243  if (!$wfm->silentWorkflowParty($this->owner->id)) {
244  $log = $ms->newMessage();
245  $msg_reps = Array(
246  'user_name' => $user->name,
247  'workflow_user' => $user->name,
248  'type_code' => $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->owner->type(), 'name'),
249  'asset_name' => $this->owner->name,
250  'status' => $this->getDescription(),
251  'workflow_url' => current_url().$this->owner->getBackendHref('workflow', FALSE),
252  );
253  $log->replacements = $msg_reps;
254  $asset_edt_fns = $this->owner->getEditFns();
255  if (isset($asset_edt_fns->static_screens['preview'])) {
256  $log->replacements['preview_url'] = current_url().$this->owner->getBackendHref('preview', FALSE);
257  } else {
258  $log->replacements['preview_url'] = current_url().$this->owner->getBackendHref('details', FALSE);
259  }
260 
261  $log->type = 'asset.workflow.log.started';
262  $log->parameters['assetid'] = $this->owner->id;
263  $log->parameters['version'] = substr($this->owner->version, 0, strrpos($this->owner->version, '.'));
264  $ms->enqueueMessage($log);
265 
266  $running_schemas = $wfm->getSchemas($this->owner->id, TRUE, TRUE);
267  foreach ($running_schemas as $schemaid) {
268  $publishers = $wfm->whoCanPublish($this->owner->id, $schemaid);
269  if (count($publishers) > 0) {
270  $msg = $ms->newMessage();
271  $msg->type = 'asset.workflow.announce.started';
272 
273  // see if we have the 'from' field set
274  $schema = $GLOBALS['SQ_SYSTEM']->am->getAsset($schemaid);
275  if ($schema->attr('schema_reply_to_email_address') != '') {
276  $msg->parameters['reply_to'] = $schema->attr('schema_reply_to_email_address');
277  } else {
278  $msg->parameters['reply_to'] = $GLOBALS['SQ_SYSTEM']->currentUserId();
279  }
280  if ($schema->attr('schema_from_email_address') != '') {
281  $msg->from = $schema->attr('schema_from_email_address');
282  }
283 
284  $msg->to = array_merge($msg->to, $publishers);
285  $msg->replacements = $msg_reps;
286  $msg->parameters['assetid'] = $this->owner->id;
287  $ms->enqueueMessage($msg);
288  }
289  }
290  }
291 
292  break;
293 
294  case SQ_STATUS_EDITING_APPROVED :
295 
296  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
297 
298  // we are going approve this asset instead of initiating the workflow
299  $current_userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
300  $wfm->setCurrentUserAsLastStarted($current_userid, $this->owner->id);
301 
302  break;
303  }
304 
305  return true;
306 
307  }//end processStatusChange()
308 
309 
310 }//end class
311 
312 ?>