Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cbovers committed Dec 12, 2023
0 parents commit b0566a5
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Copyright © Brik

Permission is hereby granted to any person obtaining a copy of this software
(the “Software”) to use, copy, modify, merge, publish and/or distribute copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

1. **Don’t plagiarize.** The above copyright notice and this license shall be
included in all copies or substantial portions of the Software.

2. **Don’t use the same license on more than one project.** Each licensed copy
of the Software shall be actively installed in no more than one production
environment at a time.

3. **Don’t mess with the licensing features.** Software features related to
licensing shall not be altered or circumvented in any way, including (but
not limited to) license validation, payment prompts, feature restrictions,
and update eligibility.

4. **Pay up.** Payment shall be made immediately upon receipt of any notice,
prompt, reminder, or other message indicating that a payment is owed.

5. **Follow the law.** All use of the Software shall not violate any applicable
law or regulation, nor infringe the rights of any other person or entity.

Failure to comply with the foregoing conditions will automatically and
immediately result in termination of the permission granted hereby. This
license does not include any right to receive updates to the Software or
technical support. Licensees bear all risk related to the quality and
performance of the Software and any modifications made or obtained to it,
including liability for actual and consequential harm, such as loss or
corruption of data, and any necessary service, repair, or correction.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, INCLUDING SPECIAL, INCIDENTAL AND CONSEQUENTIAL DAMAGES, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "brikdigital/craft-hubspot",
"description": "Hubspot",
"type": "craft-plugin",
"license": "proprietary",
"support": {
"email": "plugins@brik.digital",
"issues": "https://github.com/brikdigital/craft-hubspot/issues?state=open",
"source": "https://github.com/brikdigital/craft-hubspot",
"docs": "https://github.com/brikdigital/craft-hubspot",
"rss": "https://github.com/brikdigital/craft-hubspot/releases.atom"
},
"require": {
"php": ">=8.0.2",
"craftcms/cms": "^4.5.0",
"hubspot/api-client": "^10.0.0"
},
"require-dev": {
"craftcms/ecs": "dev-main",
"craftcms/phpstan": "dev-main"
},
"autoload": {
"psr-4": {
"brikdigital\\hubspot\\": "src/"
}
},
"extra": {
"handle": "hubspot",
"name": "Hubspot",
"developer": "Brik",
"documentationUrl": "https://github.com/brikdigital/craft-hubspot",
"class": "brikdigital\\hubspot\\Hubspot"
},
"scripts": {
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --ansi --fix",
"phpstan": "phpstan --memory-limit=1G"
},
"config": {
"sort-packages": true,
"platform": {
"php": "8.0.2"
},
"allow-plugins": {
"yiisoft/yii2-composer": true,
"craftcms/plugin-installer": true
}
}
}

75 changes: 75 additions & 0 deletions src/Hubspot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace brikdigital\hubspot;

use Craft;
use brikdigital\hubspot\fields\HubspotLandingPageField;
use brikdigital\hubspot\links\HubspotLandingPageLinkType;
use brikdigital\hubspot\models\Settings;
use craft\base\Model;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use verbb\hyper\services\Links;
use yii\base\Event;

/**
* Hubspot plugin
*
* @method static Hubspot getInstance()
* @method Settings getSettings()
* @author Brik <plugins@brik.digital>
* @copyright Brik
* @license https://craftcms.github.io/license/ Craft License
*/
class Hubspot extends Plugin
{
public string $schemaVersion = '1.0.0';
public bool $hasCpSettings = true;

public static function config(): array
{
return [
'components' => [
// Define component configs here...
],
];
}

public function init(): void
{
parent::init();

// Defer most setup tasks until Craft is fully initialized
Craft::$app->onInit(function () {
$this->attachEventHandlers();
// ...
});
}

protected function createSettingsModel(): ?Model
{
return Craft::createObject(Settings::class);
}

protected function settingsHtml(): ?string
{
return Craft::$app->view->renderTemplate('hubspot/_settings.twig', [
'plugin' => $this,
'settings' => $this->getSettings(),
]);
}

private function attachEventHandlers(): void
{
// Register event handlers here ...
// (see https://craftcms.com/docs/4.x/extend/events.html to get started)
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function (RegisterComponentTypesEvent $event) {
$event->types[] = HubspotLandingPageField::class;
});

Event::on(Links::class, Links::EVENT_REGISTER_LINK_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = HubspotLandingPageLinkType::class;
});
}
}
71 changes: 71 additions & 0 deletions src/console/controllers/HubspotController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace brikdigital\hubspot\console\controllers;

use brikdigital\hubspot\Hubspot as Plugin;
use brikdigital\hubspot\models\LandingPage;
use brikdigital\hubspot\records\LandingPageRecord;
use Craft;
use craft\console\Controller;
use yii\console\ExitCode;

/**
* Hubspot controller
*/
class HubspotController extends Controller
{
public $defaultAction = 'index';

public function options($actionID): array
{
$options = parent::options($actionID);
switch ($actionID) {
case 'index':
// $options[] = '...';
break;
}
return $options;
}

/**
* hubspot/hubspot command
*/
public function actionSync(): int
{
$this->_syncLandingPages();

return ExitCode::OK;
}

private function _syncLandingPages()
{
$accessToken = \craft\helpers\App::parseEnv(Plugin::getInstance()->getSettings()->accessToken);
$hubspot = \HubSpot\Factory::createWithAccessToken($accessToken);
$response = $hubspot->apiRequest([
'path' => '/cms/v3/pages/landing-pages',
]);

$contents = $response->getBody()->getContents();
$data = json_decode($contents);

foreach ($data->results as $object) {

$landingPage = LandingPageRecord::findOne(['hubspot_key' => $object->id]);
if ($landingPage == null) {
$landingPage = new LandingPageRecord();
}

$landingPage->hubspot_key = (int)$object->id;
$landingPage->name = $object->name;
$landingPage->url = $object->url;
$landingPage->language = isset($object->language) ? $object->language : null;
$landingPage->authorName = $object->authorName;
$landingPage->state = $object->state;
$landingPage->dateCreated = date("Y-m-d h:i:s", strtotime($object->createdAt));
$landingPage->dateUpdated = date("Y-m-d h:i:s", strtotime($object->updatedAt));
$landingPage->dateArchived = date("Y-m-d h:i:s", strtotime($object->archivedAt));

$landingPage->save();
}
}
}
103 changes: 103 additions & 0 deletions src/fields/HubspotLandingPageField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace brikdigital\hubspot\fields;

use brikdigital\hubspot\records\LandingPageRecord;
use Craft;
use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\elements\db\ElementQueryInterface;
use craft\fields\Dropdown;
use craft\helpers\Html;
use craft\helpers\Cp;
use craft\helpers\StringHelper;
use yii\db\Schema;

/**
* Hubspot Landing Page field type
*/
class HubspotLandingPageField extends Dropdown
{
public function init(): void
{

$this->options = [
[
'label' => Craft::t('hubspot', 'Choose a Landing Page'),
'value' => '',
'disabled' => true,
]
];

$landingPages = LandingPageRecord::find()->all();
foreach ($landingPages as $page) {
$this->options[] = [
'label' => $page->name,
'value' => $page->url,
];
}

}

public static function displayName(): string
{
return Craft::t('hubspot', 'Hubspot Landing Page');
}

public function getConfig(): array
{
return array_merge(parent::getConfig(), [
'value' => $this->value,
]);
}

public function getSettingsHtml(): ?string
{
return null;
}

protected function inputHtml(mixed $value, ElementInterface $element = null): string
{
return Cp::selectHtml([
'id' => $this->getInputId(),
'describedBy' => '',
'name' => $this->handle,
'value' => $value->value,
'options' => $this->options,
'disabled' => false,
]);
}

public function getElementValidationRules(): array
{
return [];
}

protected function searchKeywords(mixed $value, ElementInterface $element): string
{
return StringHelper::toString($value, ' ');
}

public function getElementConditionRuleType(): array|string|null
{
return null;
}

public function modifyElementsQuery(ElementQueryInterface $query, mixed $value): void
{
parent::modifyElementsQuery($query, $value);
}

public function getStatus(ElementInterface $element): ?array
{
if ($element->isFieldModified($this->handle)) {
return [
Element::ATTR_STATUS_MODIFIED,
Craft::t('app', 'This field has been modified.'),
];
}

return null;
}
}
Loading

0 comments on commit b0566a5

Please sign in to comment.