/** * This method associates a flowing item with certain workflow and creates * an active 'flow' for it, unless there's already another active 'flow' * for such combination. * * @param \TooBasic\Workflows\Item $item Flowing item to be injected. * @param string $workflowName Name of the workflow to associate it with. * @param string $error Returns an error message when something went * wrong. * @return boolean Returns TRUE if the items was injectected successfully. */ public function inject(Item $item, $workflowName, &$error = false) { // // Default values. $out = true; // // Logging operation. $this->_log->log(LGGR_LOG_LEVEL_INFO, "Injecting into workflow '{$workflowName}'. Item: " . json_encode($item->toArray())); // // Loading and checking workflow. $workflow = $this->getWorkflow($workflowName); if ($workflow) { // // Basic query prefixes. $prefixes = array(GC_DBQUERY_PREFIX_TABLE => $this->_dbprefix, GC_DBQUERY_PREFIX_COLUMN => 'ifl_'); // // Creating a query to search for active flows. $query = $this->_db->queryAdapter()->select('wkfl_item_flows', array('workflow' => $workflowName, 'type' => $item->type(), 'item' => $item->id(), 'status' => 'tmp'), $prefixes); $stmt = $this->_db->prepare($query[GC_AFIELD_QUERY]); // // Checking active flows. $injected = false; foreach (array(WKFL_ITEM_FLOW_STATUS_OK, WKFL_ITEM_FLOW_STATUS_WAIT) as $status) { $query[GC_AFIELD_PARAMS]['status'] = $status; $stmt->execute($query[GC_AFIELD_PARAMS]); if ($stmt->rowCount() > 0) { $injected = true; break; } } // // Checking if the new flow can be injected. if ($injected) { $error = 'Item already injected and running'; $out = false; } else { // // Creating a query to inject a flow. $query = $this->_db->queryAdapter()->insert('wkfl_item_flows', array('workflow' => $workflowName, 'type' => $item->type(), 'item' => $item->id()), $prefixes); $stmt = $this->_db->prepare($query[GC_AFIELD_QUERY]); $out = boolval($stmt->execute($query[GC_AFIELD_PARAMS])); } } else { $out = false; $error = "Unknown workflow '{$workflowName}'"; } // // Logging errors if any. if (!$out) { $this->_log->log(LGGR_LOG_LEVEL_ERROR, $error); } return $out; }