getName(); } /** * Set project id * * @access public * @return Base */ public function setProjectId($project_id) { $this->projectId = $project_id; return $this; } /** * Get project id * * @access public * @return integer */ public function getProjectId() { return $this->projectId; } /** * Set an user defined parameter * * @access public * @param string $name Parameter name * @param mixed $value Value * @param Base */ public function setParam($name, $value) { $this->params[$name] = $value; return $this; } /** * Get an user defined parameter * * @access public * @param string $name Parameter name * @param mixed $default Default value * @return mixed */ public function getParam($name, $default = null) { return isset($this->params[$name]) ? $this->params[$name] : $default; } /** * Check if an action is executable (right project and required parameters) * * @access public * @param array $data * @param string $eventName * @return bool */ public function isExecutable(array $data, $eventName) { return $this->hasCompatibleEvent($eventName) && $this->hasRequiredProject($data) && $this->hasRequiredParameters($data) && $this->hasRequiredCondition($data); } /** * Check if the event is compatible with the action * * @access public * @param string $eventName * @return bool */ public function hasCompatibleEvent($eventName) { return in_array($eventName, $this->getEvents()); } /** * Check if the event data has the required project * * @access public * @param array $data Event data dictionary * @return bool */ public function hasRequiredProject(array $data) { return isset($data['project_id']) && $data['project_id'] == $this->getProjectId(); } /** * Check if the event data has required parameters to execute the action * * @access public * @param array $data Event data dictionary * @return bool True if all keys are there */ public function hasRequiredParameters(array $data) { foreach ($this->getEventRequiredParameters() as $parameter) { if (! isset($data[$parameter])) { return false; } } return true; } /** * Execute the action * * @access public * @param \Kanboard\Event\GenericEvent $event * @param string $eventName * @return bool */ public function execute(GenericEvent $event, $eventName) { // Avoid infinite loop, a listener instance can be called only one time if ($this->called) { return false; } $data = $event->getAll(); $result = false; if ($this->isExecutable($data, $eventName)) { $this->called = true; $result = $this->doAction($data); } $this->logger->debug('AutomaticAction '.$this->getName().' => '.($result ? 'true' : 'false')); return $result; } /** * Register a new event for the automatic action * * @access public * @param string $event * @param string $description */ public function addEvent($event, $description = '') { if ($description !== '') { $this->eventManager->register($event, $description); } $this->compatibleEvents[] = $event; return $this; } /** * Get all compatible events of an automatic action * * @access public * @return array */ public function getEvents() { return array_unique(array_merge($this->getCompatibleEvents(), $this->compatibleEvents)); } }