View source
<?php
trait SchemaActionTrait {
use SchemaPersonOrgTrait, SchemaOfferTrait, SchemaThingTrait, SchemaPlaceTrait, SchemaEventTrait, SchemaEntryPointTrait, SchemaPivotTrait {
SchemaPlaceTrait::placeForm insteadof SchemaEventTrait;
SchemaPlaceTrait::postalAddressForm insteadof SchemaEventTrait;
SchemaPlaceTrait::geoForm insteadof SchemaEventTrait;
SchemaPivotTrait::pivotForm insteadof SchemaPersonOrgTrait;
SchemaPivotTrait::pivotForm insteadof SchemaOfferTrait;
SchemaPivotTrait::pivotForm insteadof SchemaThingTrait;
SchemaPivotTrait::pivotForm insteadof SchemaPlaceTrait;
SchemaPivotTrait::pivotForm insteadof SchemaEventTrait;
SchemaPivotTrait::pivotForm insteadof SchemaEntryPointTrait;
}
protected abstract function schemaMetatagManager();
public function actionForm(array $input_values) {
$all_types = static::getAllActions(TRUE);
$all_options = array_combine($all_types, $all_types);
$input_values += $this
->SchemaMetatagManager()
->defaultInputValues();
$value = $input_values['value'];
$form['#type'] = 'fieldset';
$form['#title'] = $input_values['title'];
$form['#description'] = $input_values['description'];
$form['#tree'] = TRUE;
$actions = $input_values['actions'];
if (empty($actions)) {
return [];
}
$selector = ':input[name="' . $input_values['visibility_selector'] . '[@type]"]';
$visibility = [
'invisible' => [
$selector => [
'value' => '',
],
],
];
$selector2 = $this
->SchemaMetatagManager()
->altSelector($selector);
$visibility2 = [
'invisible' => [
$selector2 => [
'value' => '',
],
],
];
$visibility['invisible'] = [
$visibility['invisible'],
$visibility2['invisible'],
];
$form['#type'] = 'fieldset';
$form['#title'] = $input_values['title'];
$form['#description'] = $input_values['description'];
$form['#tree'] = TRUE;
$form['pivot'] = $this
->pivotForm($value);
$form['pivot']['#states'] = $visibility;
$options = [];
foreach ($all_options as $type => $label) {
if (in_array($type, $actions)) {
$options[$type] = $all_options[$type];
}
}
$form['@type'] = [
'#type' => 'select',
'#title' => $this
->t('@type'),
'#default_value' => !empty($value['@type']) ? $value['@type'] : '',
'#empty_option' => t('- None -'),
'#empty_value' => '',
'#options' => $options,
'#required' => $input_values['#required'],
'#weight' => -10,
];
foreach ($options as $type => $label) {
$parent_type = static::getActionType($type);
$properties = static::actionProperties($parent_type);
foreach ($properties as $key => $property) {
if (empty($property['form'])) {
$form[$key] = [
'#type' => 'textfield',
'#title' => $key,
'#default_value' => !empty($value[$key]) ? $value[$key] : '',
'#empty_option' => t('- None -'),
'#empty_value' => '',
'#required' => $input_values['#required'],
'#description' => $property['description'],
'#states' => $visibility,
'#weight' => 0,
];
}
else {
$sub_values = [
'title' => $key,
'description' => $property['description'],
'value' => !empty($value[$key]) ? $value[$key] : [],
'#required' => $input_values['#required'],
'visibility_selector' => $input_values['visibility_selector'] . '[' . $key . ']',
];
$method = $property['form'];
$form[$key] = self::$method($sub_values);
$form[$key]['#states'] = $visibility;
$form[$key]['#weight'] = 0;
}
}
}
$properties = static::actionProperties('All');
foreach ($properties as $key => $property) {
if (empty($property['form'])) {
$form[$key] = [
'#type' => 'textfield',
'#title' => $key,
'#default_value' => !empty($value[$key]) ? $value[$key] : '',
'#empty_option' => t('- None -'),
'#empty_value' => '',
'#required' => $input_values['#required'],
'#description' => $property['description'],
'#states' => $visibility,
'#weight' => 5,
];
}
else {
$sub_values = [
'title' => $key,
'description' => $property['description'],
'value' => !empty($value[$key]) ? $value[$key] : [],
'#required' => $input_values['#required'],
'visibility_selector' => $input_values['visibility_selector'] . '[' . $key . ']',
];
$method = $property['form'];
$form[$key] = self::$method($sub_values);
$form[$key]['#states'] = $visibility;
$form[$key]['#weight'] = 5;
}
}
return $form;
}
public static function getAllActions($flattened = FALSE) {
$list = $flattened ? [
'Action',
] : [];
$types = static::actionTypes();
foreach ($types as $type) {
if ($flattened) {
$list = array_merge($list, static::actionObjects($type));
}
else {
$list[$type] = static::actionObjects($type);
}
}
return $list;
}
public static function getActionType($action) {
$actions = static::getAllActions();
foreach ($actions as $type => $values) {
if (in_array($action, $values)) {
return $type;
}
}
return FALSE;
}
public static function actionTypes() {
return [
'MoveAction',
'TransferAction',
'TradeAction',
'ControlAction',
'AchieveAction',
'OrganizeAction',
'AssessAction',
'InteractAction',
'ConsumeAction',
'CreateAction',
'PlayAction',
'SearchAction',
'FindAction',
'UpdateAction',
];
}
public static function actionObjects($action_type) {
switch ($action_type) {
case 'MoveAction':
return [
'MoveAction',
'TravelAction',
'DepartAction',
'ArriveAction',
];
case 'TransferAction':
return [
'TransferAction',
'DownloadAction',
'LendAction',
'GiveAction',
'ReceiveAction',
'SendAction',
'BorrowAction',
'ReturnAction',
'TakeAction',
];
case 'TradeAction':
return [
'TradeAction',
'BuyAction',
'QuoteAction',
'SellAction',
'PayAction',
'RentAction',
'DonateAction',
'OrderAction',
'TipAction',
];
case 'ControlAction':
return [
'ControlAction',
'ResumeAction',
'DeactivateAction',
'ActivateAction',
'SuspendAction',
];
case 'AchieveAction':
return [
'AchieveAction',
'WinAction',
'LoseAction',
'TieAction',
];
case 'OrganizeAction':
return [
'OrganizeAction',
'PlanAction',
'CancelAction',
'ReserveAction',
'ScheduleAction',
'ApplyAction',
'AllocateAction',
'AuthorizeAction',
'AssignAction',
'RejectAction',
'AcceptAction',
'BookmarkAction',
];
case 'AssessAction':
return [
'AssessAction',
'IgnoreAction',
'ChooseAction',
'VoteAction',
'ReactAction',
'LikeAction',
'DisagreeAction',
'EndorseAction',
'AgreeAction',
'DislikeAction',
'WantAction',
'ReviewAction',
];
case 'InteractAction':
return [
'InteractAction',
'BefriendAction',
'SubscribeAction',
'LeaveAction',
'UnRegisterAction',
'MarryAction',
'RegisterAction',
'JoinAction',
'CommunicateAction',
'CheckOutAction',
'InviteAction',
'CommentAction',
'ReplyAction',
'ShareAction',
'InformAction',
'RsvpAction',
'ConfirmAction',
'AskAction',
'CheckInAction',
'FollowAction',
];
case 'ConsumeAction':
return [
'ConsumeAction',
'ViewAction',
'DrinkAction',
'ListenAction',
'WatchAction',
'InstallAction',
'UseAction',
'WearAction',
'ReadAction',
'EatAction',
];
case 'CreateAction':
return [
'CreateAction',
'DrawAction',
'FilmAction',
'CookAction',
'PhotographAction',
'PaintAction',
'WriteAction',
];
case 'PlayAction':
return [
'PlayAction',
'ExerciseAction',
'PerformAction',
];
case 'SearchAction':
return [
'SearchAction',
];
case 'FindAction':
return [
'FindAction',
'CheckAction',
'DiscoverAction',
'TrackAction',
];
case 'UpdateAction':
return [
'UpdateAction',
'AddAction',
'InsertAction',
'AppendAction',
'PrependAction',
'DeleteAction',
'ReplaceAction',
];
default:
return [
'Action',
];
}
}
public static function actionProperties($action_type) {
switch ($action_type) {
case 'MoveAction':
case 'TransferAction':
return [
'fromLocation' => [
'class' => 'SchemaPlaceBase',
'form' => 'placeForm',
'description' => "A sub property of location. The original location of the object or the agent before the action.",
],
'toLocation' => [
'class' => 'SchemaPlaceBase',
'form' => 'placeForm',
'description' => "A sub property of location. The final location of the object or the agent after the action.",
],
];
case 'TradeAction':
return [
'target' => [
'class' => 'SchemaEntryPointBase',
'form' => 'entryPointForm',
'description' => "Indicates a target EntryPoint for an Action.",
],
];
case 'ConsumeAction':
return [
'target' => [
'class' => 'SchemaEntryPointBase',
'form' => 'entryPointForm',
'description' => "Indicates a target EntryPoint for an Action.",
],
'expectsAcceptanceOf' => [
'class' => 'SchemaOfferBase',
'form' => 'offerForm',
'description' => "An Offer which must be accepted before the user can perform the Action. For example, the user may need to buy a movie before being able to watch it.",
],
];
case 'OrganizeAction':
return [
'target' => [
'class' => 'SchemaEntryPointBase',
'form' => 'entryPointForm',
'description' => "Indicates a target EntryPoint for an Action.",
],
'result' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "The result produced in the action. e.g. John wrote a book.",
],
];
case 'InteractAction':
case 'PlayAction':
return [
'event' => [
'class' => 'SchemaEventBase',
'form' => 'eventForm',
'description' => "Upcoming or past event associated with this place, organization, or action.",
],
];
case 'SearchAction':
return [
'query' => [
'class' => 'SchemaNameBase',
'form' => '',
'description' => "The query used on this action, i.e. https://query.example.com/search?q={search_term_string}.",
],
'query-input' => [
'class' => 'SchemaNameBase',
'form' => '',
'description' => "The placeholder for the query, i.e. required name=search_term_string.",
],
'target' => [
'class' => 'SchemaEntryPointBase',
'form' => 'entryPointForm',
'description' => "Indicates a target EntryPoint for an Action.",
],
];
case 'UpdateAction':
return [
'targetCollection' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "The collection target of the action.",
],
];
case 'All':
return [];
case 'Other':
return [
'price' => [
'class' => 'SchemaNameBase',
'form' => '',
'description' => "The offer price of a product, or of a price component.",
],
'buyer' => [
'class' => 'SchemaPersonOrgBase',
'form' => 'personOrgForm',
'description' => "The participant/person/organization that bought the object.",
],
'seller' => [
'class' => 'SchemaPersonOrgBase',
'form' => 'personOrgForm',
'description' => "An entity which offers (sells / leases / lends / loans) the services / goods. A seller may also be a provider.",
],
'recipient' => [
'class' => 'SchemaPersonOrgBase',
'form' => 'personOrgForm',
'description' => "The participant who is at the receiving end of the action.",
],
'target' => [
'class' => 'SchemaEntryPointBase',
'form' => 'entryPointForm',
'description' => "Indicates a target EntryPoint for an Action.",
],
'result' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "The result produced in the action. e.g. John wrote a book.",
],
'agent' => [
'class' => 'SchemaPersonOrgBase',
'form' => 'personOrgForm',
'description' => "The direct performer or driver of the action (animate or inanimate). e.g. John wrote a book.",
],
'instrument' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "The object that helped the agent perform the action. e.g. John wrote a book with a pen.",
],
'participant' => [
'class' => 'SchemaPersonOrgBase',
'form' => 'personOrgForm',
'description' => "Other co-agents that participated in the action indirectly. e.g. John wrote a book with Steve.",
],
'object' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "The object upon which the action is carried out, whose state is kept intact or changed. Also known as the semantic roles patient, affected or undergoer (which change their state) or theme (which doesn't). e.g. John read a book.",
],
'error' => [
'class' => 'SchemaThingBase',
'form' => 'thingForm',
'description' => "For failed actions, more information on the cause of the failure.",
],
'location' => [
'class' => 'SchemaPlaceBase',
'form' => 'placeForm',
'description' => "The location of for example where the event is happening, an organization is located, or where an action takes place.",
],
'startTime' => [
'class' => 'SchemaNameBase',
'form' => '',
'description' => "The startTime of something. For a reserved event or service (e.g. FoodEstablishmentReservation), the time that it is expected to start. For actions that span a period of time, when the action was performed. e.g. John wrote a book from January to December.",
],
'endTime' => [
'class' => 'SchemaNameBase',
'form' => '',
'description' => "The endTime of something. For a reserved event or service (e.g. FoodEstablishmentReservation), the time that it is expected to end. For actions that span a period of time, when the action was performed. e.g. John wrote a book from January to December.",
],
];
default:
return [];
}
}
}