Skip to content

Commit

Permalink
add php docs #18
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Nov 7, 2018
1 parent 899066c commit d56632e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/ngrest/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,20 @@ public function setRelation(NgRestRelation $relation)

private $_activeButtons = [];

/**
* Setter method for the active button array from the model
*
* @param array
* @since 1.2.3
*/
public function setActiveButtons(array $buttons)
{
$this->_activeButtons = $buttons;
}

/**
* @inheritDoc
*/
public function getActiveButtons()
{
$btns = [];
Expand Down
8 changes: 8 additions & 0 deletions src/ngrest/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public function getApiEndpoint();
public function getRelations();

public function setAttributeLabels(array $labels);

/**
* Get an array with the button configuration like hash, label and icon.
*
* @return array An array with hash, label and icon key.
* @since 1.2.3
*/
public function getActiveButtons();
}
60 changes: 57 additions & 3 deletions src/ngrest/base/ActiveButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,80 @@
*
* An active button is a trigger option for the current model.
*
* Example integration:
*
* ```php
* class CreateCampaignActiveButton extends ActiveButton
* {
* public $label = 'Campaign Button';
* public $icon = 'extension';
*
* public function handle(\luya\admin\ngrest\base\NgRestModel $model)
* {
* // do something with the $model
* $model->udpateAttributes(['campagin' => 123]);
*
* // maybe you change value which should be visible in the list, then you can trigger a reload event.
* $this->sendReloadEvent();
*
* // let the crud know everything was good and inform user with a message.
* return $this->sendSuccess('Campaign done for ' . $model->title);
* }
* }
* ```
*
* @author Basil Suter <basil@nadar.io>
* @since 1.2.3
*/
abstract class ActiveButton extends BaseObject
{
/**
* @var string The loadList event name
*/
const EVENT_RELOAD_LIST = 'loadList';

/**
* A label value. You can also access different angular list fields when using brackets:
* @var string A label value. You can also access different angular list fields when using brackets:
*
* 'label' => '{fieldname}',
*
* @var [type]
*/
public $label;

/**
* @var string The icon from material icons list
*/
public $icon = 'extension';

private $_events = [];

/**
* The handler which implements the function of the button.
*
* The model is passed as arugment and is refereing to the current model the active button has been pushed.
*
* @param NgRestModel $model
* @return array See sendError() or sendSuccess().
*/
abstract public function handle(NgRestModel $model);

/**
* Send a crud reload event.
*
* @return void
*/
protected function sendReloadEvent()
{
$this->_events[] = self::EVENT_RELOAD_LIST;
}

/**
* Send an error message as response.
*
* Events are only triggered on success messages {{sendSuccess()}}.
*
* @param string $message The error message.
* @return array
*/
public function sendError($message)
{
return [
Expand All @@ -44,6 +92,12 @@ public function sendError($message)
];
}

/**
* Send a success message.
*
* @param string $message The sucess message.
* @return array
*/
public function sendSuccess($message)
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/ngrest/base/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public function actionExport()
}

/**
* Run active button
* Trigger an Active Button handler.
*
* @param string $hash The hash from the class name.
* @param string|integer $id
Expand Down
7 changes: 4 additions & 3 deletions src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,11 @@ public function ngRestActiveButtons()
}

/**
* find and call an acitve button
* Handle a given active button based on the hash (classname in sha1).
*
* @param [type] $hash
* @return void
* @param string $hash The hash name, equals to the class name of the button
* @return array|boolean Returns the button array response or false if not found.
* @since 1.2.3
*/
public function handleNgRestActiveButton($hash)
{
Expand Down
1 change: 1 addition & 0 deletions src/ngrest/render/RenderCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public function getButtons()
];
}

// add active buttons.
foreach ($this->config->getActiveButtons() as $btn) {
$buttons[] = [
'ngClick' => "callActiveButton('{$btn['hash']}', ".$this->getCompositionKeysForButtonActions('item').", \$event)",
Expand Down

0 comments on commit d56632e

Please sign in to comment.