| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 13:52:53 +02:00
										 |  |  | function create_custom_field_transaction($task, $user, $name, $value, $template, $date) | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     $field_list = PhabricatorCustomField::getObjectFields($task, PhabricatorCustomField::ROLE_EDIT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach ($field_list->getFields() as $field) { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |     $field->setObject($task); | 
					
						
							|  |  |  |     $field->setViewer($user); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $field_list->readFieldsFromStorage($task); | 
					
						
							|  |  |  |     $aux_fields = $field_list->getFields(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   $old_values = array(); | 
					
						
							|  |  |  |   foreach ($aux_fields as $aux_arr_key => $aux_field) { | 
					
						
							|  |  |  |     if($aux_arr_key == "std:maniphest:" . $name) { | 
					
						
							|  |  |  |       $aux_old_value = $aux_field->getOldValueForApplicationTransactions(); | 
					
						
							|  |  |  |       $aux_field->setValueFromStorage($value); | 
					
						
							|  |  |  |       $aux_new_value = $aux_field->getNewValueForApplicationTransactions(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $placeholder_editor = new PhabricatorUserProfileEditor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $field_errors = $aux_field->validateApplicationTransactions( | 
					
						
							|  |  |  |         $placeholder_editor, | 
					
						
							|  |  |  |         PhabricatorTransactions::TYPE_CUSTOMFIELD, | 
					
						
							|  |  |  |         array( | 
					
						
							|  |  |  |         id(new ManiphestTransaction()) | 
					
						
							|  |  |  |           ->setOldValue($aux_old_value) | 
					
						
							|  |  |  |           ->setNewValue($aux_new_value), | 
					
						
							|  |  |  |         )); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       foreach ($field_errors as $error) { | 
					
						
							|  |  |  |         $errors[] = $error->getMessage(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $old_values[$aux_field->getFieldKey()] = $aux_old_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $transactions = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach ($aux_fields as $aux_arr_key => $aux_field) { | 
					
						
							|  |  |  |     if($aux_arr_key == "std:maniphest:" . $name) { | 
					
						
							|  |  |  |       $transaction = clone $template; | 
					
						
							|  |  |  |       $transaction->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD); | 
					
						
							|  |  |  |       $aux_key = $aux_field->getFieldKey(); | 
					
						
							|  |  |  |       $transaction->setMetadataValue('customfield:key', $aux_key); | 
					
						
							|  |  |  |       $old = idx($old_values, $aux_key); | 
					
						
							|  |  |  |       $new = $aux_field->getNewValueForApplicationTransactions(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $transaction->setOldValue($old); | 
					
						
							|  |  |  |       $transaction->setNewValue($new); | 
					
						
							|  |  |  |       $transaction->setOverrideDate($date); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $transactions[] = $transaction; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $transactions; | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  | function create_task($user, $id, $title, $projects, $description, $assign_user, $date, $ccs, $priority, $type_field) | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* create task */ | 
					
						
							|  |  |  |   $task = ManiphestTask::initializeNewTask($user); | 
					
						
							|  |  |  |   $task->setTitle($title); | 
					
						
							|  |  |  |   $task->setProjectPHIDs($projects); | 
					
						
							|  |  |  |   $task->setCCPHIDs($ccs); | 
					
						
							|  |  |  |   $task->setDescription($description); | 
					
						
							|  |  |  |   $task->setPriority($priority); | 
					
						
							|  |  |  |   $task->setOverrideDate($date); | 
					
						
							|  |  |  |   $task->setOverrideID($id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ($assign_user) | 
					
						
							|  |  |  |     $task->setOwnerPHID($assign_user->getPHID()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* content source */ | 
					
						
							|  |  |  |   $content_source = PhabricatorContentSource::newForSource( | 
					
						
							|  |  |  |     PhabricatorContentSource::SOURCE_UNKNOWN, | 
					
						
							|  |  |  |     array()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* transactions */ | 
					
						
							|  |  |  |   $changes = array(); | 
					
						
							|  |  |  |   $changes[ManiphestTransaction::TYPE_STATUS] = ManiphestTaskStatus::STATUS_OPEN; | 
					
						
							|  |  |  |   $changes[PhabricatorTransactions::TYPE_VIEW_POLICY] = 'public'; | 
					
						
							| 
									
										
										
										
											2013-11-03 03:12:56 +01:00
										 |  |  |   $changes[PhabricatorTransactions::TYPE_EDIT_POLICY] = lookup_project("Moderators")->getPHID(); | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $template = new ManiphestTransaction(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $transactions = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach ($changes as $type => $value) { | 
					
						
							|  |  |  |     $transaction = clone $template; | 
					
						
							|  |  |  |     $transaction->setTransactionType($type); | 
					
						
							|  |  |  |     $transaction->setNewValue($value); | 
					
						
							|  |  |  |     $transaction->setOverrideDate($date); | 
					
						
							|  |  |  |     $transactions[] = $transaction; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* type */ | 
					
						
							|  |  |  |   $transactions = array_merge($transactions, create_custom_field_transaction($task, $user, "blender:task-type", $type_field, $template, $date)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $editor = id(new ManiphestTransactionEditor()) | 
					
						
							|  |  |  |   ->setActor($user) | 
					
						
							|  |  |  |   ->setContentSource($content_source) | 
					
						
							|  |  |  |   ->setContinueOnNoEffect(true) | 
					
						
							|  |  |  |   ->applyTransactions($task, $transactions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return $task; | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function create_comment($task, $user, $description, $date) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* content source */ | 
					
						
							|  |  |  |   $content_source = PhabricatorContentSource::newForSource( | 
					
						
							|  |  |  |     PhabricatorContentSource::SOURCE_UNKNOWN, | 
					
						
							|  |  |  |     array()); | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* transactions */ | 
					
						
							|  |  |  |   $comment = id(new ManiphestTransactionComment()); | 
					
						
							|  |  |  |   $comment->setContent($description); | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   $transaction = id(new ManiphestTransaction()); | 
					
						
							|  |  |  |   $transaction->setTransactionType(PhabricatorTransactions::TYPE_COMMENT); | 
					
						
							|  |  |  |   $transaction->setOverrideDate($date); | 
					
						
							|  |  |  |   $transaction->attachComment($comment); | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   $transactions = array(); | 
					
						
							|  |  |  |   $transactions[] = $transaction; | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* apply */ | 
					
						
							| 
									
										
										
										
											2013-10-24 14:48:32 +02:00
										 |  |  |     $editor = id(new ManiphestTransactionEditor()) | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  |       ->setActor($user) | 
					
						
							|  |  |  |       ->setContentSource($content_source) | 
					
						
							|  |  |  |       ->setContinueOnMissingFields(true) | 
					
						
							|  |  |  |       ->applyTransactions($task, $transactions); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function create_file($task, $user, $name, $contents, $date) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* content source */ | 
					
						
							|  |  |  |   $content_source = PhabricatorContentSource::newForSource( | 
					
						
							|  |  |  |     PhabricatorContentSource::SOURCE_UNKNOWN, | 
					
						
							|  |  |  |     array()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* files */ | 
					
						
							|  |  |  |   $files = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $file = PhabricatorFile::newFromFileData( | 
					
						
							|  |  |  |     $contents, | 
					
						
							|  |  |  |     array( | 
					
						
							|  |  |  |       'authorPHID' => $user->getPHID(), | 
					
						
							|  |  |  |         'name' => $name, | 
					
						
							|  |  |  |     )); | 
					
						
							|  |  |  |   $files[] = $file; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $files = mpull($files, 'getPHID', 'getPHID'); | 
					
						
							|  |  |  |   $new = $task->getAttached(); | 
					
						
							|  |  |  |   foreach ($files as $phid) { | 
					
						
							|  |  |  |     if (empty($new[PhabricatorFilePHIDTypeFile::TYPECONST])) { | 
					
						
							|  |  |  |       $new[PhabricatorFilePHIDTypeFile::TYPECONST] = array(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $new[PhabricatorFilePHIDTypeFile::TYPECONST][$phid] = array(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* transaction */ | 
					
						
							|  |  |  |   $transaction = new ManiphestTransaction(); | 
					
						
							|  |  |  |   $transaction->setTransactionType(ManiphestTransaction::TYPE_ATTACH); | 
					
						
							|  |  |  |   $transaction->setNewValue($new); | 
					
						
							|  |  |  |   $transaction->setOverrideDate($date); | 
					
						
							|  |  |  |   $transactions[] = $transaction; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* apply */ | 
					
						
							| 
									
										
										
										
											2013-10-24 14:48:32 +02:00
										 |  |  |     $editor = id(new ManiphestTransactionEditor()) | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  |       ->setActor($user) | 
					
						
							|  |  |  |       ->setContentSource($content_source) | 
					
						
							|  |  |  |       ->setContinueOnMissingFields(true) | 
					
						
							|  |  |  |       ->applyTransactions($task, $transactions); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function set_status($task, $user, $status, $date) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* content source */ | 
					
						
							|  |  |  |   $content_source = PhabricatorContentSource::newForSource( | 
					
						
							|  |  |  |     PhabricatorContentSource::SOURCE_UNKNOWN, | 
					
						
							|  |  |  |     array()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* transactions */ | 
					
						
							|  |  |  |   $changes = array(); | 
					
						
							|  |  |  |   $changes[ManiphestTransaction::TYPE_STATUS] = $status; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $template = new ManiphestTransaction(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $transactions = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   foreach ($changes as $type => $value) { | 
					
						
							|  |  |  |     $transaction = clone $template; | 
					
						
							|  |  |  |     $transaction->setTransactionType($type); | 
					
						
							|  |  |  |     $transaction->setNewValue($value); | 
					
						
							|  |  |  |     $transaction->setOverrideDate($date); | 
					
						
							|  |  |  |     $transactions[] = $transaction; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $editor = id(new ManiphestTransactionEditor()) | 
					
						
							|  |  |  |   ->setActor($user) | 
					
						
							|  |  |  |   ->setContentSource($content_source) | 
					
						
							|  |  |  |   ->setContinueOnNoEffect(true) | 
					
						
							|  |  |  |   ->applyTransactions($task, $transactions); | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-03 03:12:56 +01:00
										 |  |  | function create_project($name, $username, $active, $membernames, $blurb = '', $admincontrol = false) | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   $user = lookup_user($username); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* create project */ | 
					
						
							|  |  |  |   $project = new PhabricatorProject(); | 
					
						
							|  |  |  |   $project->setAuthorPHID($user->getPHID()); | 
					
						
							|  |  |  |   $project->attachMemberPHIDs(array()); | 
					
						
							|  |  |  |   $profile = new PhabricatorProjectProfile(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $xactions = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-03 03:12:56 +01:00
										 |  |  |   if($admincontrol) | 
					
						
							|  |  |  |     $editpolicy = "admin"; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     $editpolicy = lookup_project("Moderators")->getPHID(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  |   /* policy */ | 
					
						
							|  |  |  |   $project->setViewPolicy("public"); | 
					
						
							| 
									
										
										
										
											2013-11-03 03:12:56 +01:00
										 |  |  |   $project->setEditPolicy($editpolicy); | 
					
						
							|  |  |  |   $project->setJoinPolicy($editpolicy); | 
					
						
							| 
									
										
										
										
											2013-11-02 20:18:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* archived */ | 
					
						
							|  |  |  |   if(!$active) | 
					
						
							|  |  |  |     $project->setStatus(PhabricatorProjectStatus::STATUS_ARCHIVED); | 
					
						
							|  |  |  |   /* name transaction */ | 
					
						
							|  |  |  |   $xaction = new PhabricatorProjectTransaction(); | 
					
						
							|  |  |  |   $xaction->setTransactionType( | 
					
						
							|  |  |  |     PhabricatorProjectTransaction::TYPE_NAME); | 
					
						
							|  |  |  |   $xaction->setNewValue($name); | 
					
						
							|  |  |  |   $xactions[] = $xaction; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* members transaction */ | 
					
						
							|  |  |  |   $member_phids = array(); | 
					
						
							|  |  |  |   foreach($membernames as $membername) { | 
					
						
							|  |  |  |     $member_phids[] = lookup_user($membername)->getPHID(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $xaction = new PhabricatorProjectTransaction(); | 
					
						
							|  |  |  |   $xaction->setTransactionType( | 
					
						
							|  |  |  |     PhabricatorProjectTransaction::TYPE_MEMBERS); | 
					
						
							|  |  |  |   $xaction->setNewValue($member_phids); | 
					
						
							|  |  |  |   $xactions[] = $xaction; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* apply transaction */ | 
					
						
							|  |  |  |   $editor = new PhabricatorProjectEditor($project); | 
					
						
							|  |  |  |   $editor->setActor($user); | 
					
						
							|  |  |  |   $editor->applyTransactions($xactions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* set blurb */ | 
					
						
							|  |  |  |   $profile->setBlurb($blurb); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $project->save(); | 
					
						
							|  |  |  |   $profile->setProjectPHID($project->getPHID()); | 
					
						
							|  |  |  |   $profile->save(); | 
					
						
							| 
									
										
										
										
											2013-10-22 02:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |