set('exampleRequestMethod', 'POST'); $requestUrl = $this->getConfig()->getSiteUrl() . '/api/v1/LeadCapture/' . $entity->get('apiKey'); $entity->set('exampleRequestUrl', $requestUrl); $fieldUtil = $this->fieldUtil; $requestPayload = "```{\n"; $attributeList = []; $attributeIgnoreList = [ 'emailAddressIsOptedOut', 'phoneNumberIsOptedOut', 'emailAddressData', 'phoneNumberData', ]; $fieldList = $entity->get('fieldList'); if (is_array($fieldList)) { foreach ($fieldList as $field) { foreach ($fieldUtil->getActualAttributeList('Lead', $field) as $attribute) { if (!in_array($attribute, $attributeIgnoreList)) { $attributeList[] = $attribute; } } } } $seed = $this->getEntityManager()->getEntity('Lead'); foreach ($attributeList as $i => $attribute) { $value = strtoupper(Util::camelCaseToUnderscore($attribute)); if ( in_array( $seed->getAttributeType($attribute), [Entity::VARCHAR, Entity::TEXT] ) ) { $value = '"' . $value . '"'; } $requestPayload .= " \"" . $attribute . "\": " . $value; if ($i < count($attributeList) - 1) { $requestPayload .= ","; } $requestPayload .= "\n"; } $requestPayload .= '}```'; $entity->set('exampleRequestPayload', $requestPayload); } protected function beforeCreateEntity(Entity $entity, $data) { $apiKey = $this->generateApiKey(); $entity->set('apiKey', $apiKey); } public function generateNewApiKeyForEntity(string $id): Entity { $entity = $this->getEntity($id); if (!$entity) { throw new NotFound(); } $apiKey = $this->generateApiKey(); $entity->set('apiKey', $apiKey); $this->getEntityManager()->saveEntity($entity); $this->prepareEntityForOutput($entity); return $entity; } public function generateApiKey(): string { return Util::generateApiKey(); } public function isApiKeyValid(string $apiKey): bool { $leadCapture = $this->getEntityManager() ->getRDBRepository('LeadCapture') ->where([ 'apiKey' => $apiKey, 'isActive' => true, ]) ->findOne(); if ($leadCapture) { return true; } return false; } protected function createTool(): Tool { return $this->injectableFactory->create(Tool::class); } public function leadCapture(string $apiKey, stdClass $data) { $this->createTool()->capture($apiKey, $data); } public function jobOptInConfirmation(stdClass $data) { if (empty($data->id)) { throw new Error(); } $this->createTool()->sendOptInConfirmation($data->id); } public function confirmOptIn(string $id): stdClass { return $this->createTool()->confirmOptIn($id); } public function getSmtpAccountDataList(): array { if (!$this->getUser()->isAdmin()) { throw new Forbidden(); } $dataList = []; $inboundEmailList = $this->getEntityManager() ->getRDBRepository('InboundEmail') ->where([ 'useSmtp' => true, 'status' => 'Active', ['emailAddress!=' => ''], ['emailAddress!=' => null], ]) ->find(); foreach ($inboundEmailList as $inboundEmail) { $item = (object) []; $key = 'inboundEmail:' . $inboundEmail->getId(); $item->key = $key; $item->emailAddress = $inboundEmail->get('emailAddress'); $item->fromName = $inboundEmail->get('fromName'); $dataList[] = $item; } return $dataList; } }