Skip to content

Commit 53df23d

Browse files
committed
General bugfixes / optimizations
- Doc - Code cleanup / General optimizations - API Key as env var - Added demo prompts
1 parent 4a456c5 commit 53df23d

File tree

15 files changed

+212
-87
lines changed

15 files changed

+212
-87
lines changed

src/Plugin.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@
3333
*/
3434
class Plugin extends BasePlugin
3535
{
36+
/**
37+
* @var string
38+
*/
3639
public string $schemaVersion = '1.0.0';
40+
41+
/**
42+
* @var bool
43+
*/
3744
public bool $hasCpSettings = true;
45+
46+
/**
47+
* @var bool
48+
*/
3849
public bool $hasCpSection = true;
3950

51+
/**
52+
* @return array[]
53+
*/
4054
public static function config(): array
4155
{
4256
return [
@@ -45,6 +59,9 @@ public static function config(): array
4559
];
4660
}
4761

62+
/**
63+
* @return void
64+
*/
4865
public function init(): void
4966
{
5067
parent::init();
@@ -55,21 +72,32 @@ public function init(): void
5572
$this->attachFunctions();
5673
$this->registerEvents();
5774
});
58-
5975
}
6076

77+
/**
78+
* @return void
79+
*/
6180
protected function setup(): void {
6281
$this->setComponents([
6382
'promptService' => PromptService::class,
6483
]);
6584
}
6685

86+
/**
87+
* @return Model|null
88+
*/
6789
protected function createSettingsModel(): ?Model
6890
{
6991
return new SettingsModel();
70-
7192
}
7293

94+
/**
95+
* @return string|null
96+
* @throws \Twig\Error\LoaderError
97+
* @throws \Twig\Error\RuntimeError
98+
* @throws \Twig\Error\SyntaxError
99+
* @throws \yii\base\Exception
100+
*/
73101
protected function settingsHtml(): ?string
74102
{
75103
return \Craft::$app->getView()->renderTemplate(
@@ -78,6 +106,9 @@ protected function settingsHtml(): ?string
78106
);
79107
}
80108

109+
/**
110+
* @return void
111+
*/
81112
private function attachFunctions(): void
82113
{
83114
Event::on(
@@ -96,6 +127,9 @@ static function (DefineHtmlEvent $event) {
96127
);
97128
}
98129

130+
/**
131+
* @return void
132+
*/
99133
protected function registerEvents(): void
100134
{
101135
Event::on(
@@ -121,12 +155,17 @@ function (Event $event) {
121155
);
122156
}
123157

124-
158+
/**
159+
* @return string
160+
*/
125161
public function getPluginName(): string
126162
{
127163
return $this->getSettings()->pluginName;
128164
}
129165

166+
/**
167+
* @return array|null
168+
*/
130169
public function getCpNavItem(): ?array
131170
{
132171
$nav = parent::getCpNavItem();
@@ -149,6 +188,9 @@ public function getCpNavItem(): ?array
149188
return $nav;
150189
}
151190

191+
/**
192+
* @return mixed
193+
*/
152194
public function getSettingsResponse(): mixed
153195
{
154196
return Craft::$app->controller->redirect(UrlHelper::cpUrl('chatgpt-integration/settings'));

src/controllers/PromptController.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace publishing\chatgptintegration\controllers;
44

5+
use craft\web\Controller;
56
use publishing\chatgptintegration\models\PromptModel;
67
use publishing\chatgptintegration\Plugin;
7-
use publishing\chatgptintegration\records\ChatgptIntegration_PromptRecord;
88
use yii\web\Response;
99

10-
class PromptController extends \craft\web\Controller
10+
class PromptController extends Controller
1111
{
12+
/**
13+
* @return Response
14+
*/
1215
public function actionCreatePrompt(): Response
1316
{
1417
return $this->renderTemplate('chatgpt-integration/prompts/_new', ['prompt' => new PromptModel()]);
@@ -29,16 +32,14 @@ public function actionSavePrompt(): Response
2932
$promptModel->enabled = $request->getRequiredParam('enabled');
3033

3134
if (!$promptModel->validate()) {
32-
return $this->renderTemplate('chatgpt-integration/prompts/_edit', ['prompt' => $promptModel]);
35+
return $this->renderTemplate('chatgpt-integration/prompts/_new', ['prompt' => $promptModel]);
3336
}
3437

3538
if (Plugin::getInstance()->promptService->savePrompt($promptModel)) {
3639
return $this->redirect('chatgpt-integration/prompts');
3740
}
3841

39-
return $this->renderTemplate('chatgpt-integration/prompts/_edit', ['prompt' => $promptModel]);
40-
41-
42+
return $this->renderTemplate('chatgpt-integration/prompts/_new', ['prompt' => $promptModel]);
4243
}
4344

4445
/**
@@ -54,39 +55,33 @@ public function actionEditPrompt(int $id): Response
5455
/**
5556
* @return void
5657
* @throws \Throwable
57-
* @throws \yii\db\StaleObjectException
5858
* @throws \yii\web\BadRequestHttpException
5959
*/
6060
public function actionUpdatePrompt()
6161
{
6262
$request = \Craft::$app->getRequest();
63+
$model = new PromptModel;
6364

64-
/** @var ChatgptIntegration_PromptRecord $record */
65-
$record = ChatgptIntegration_PromptRecord::find()->where(['id' => $request->getRequiredParam('id')])->one();
65+
$model->id = $request->getRequiredParam('id');
66+
$model->label = $request->getRequiredParam('label');
67+
$model->promptTemplate = $request->getRequiredParam('promptTemplate');
68+
$model->enabled = $request->getRequiredParam('enabled');
6669

67-
$record->label = $request->getRequiredParam('label');
68-
$record->promptTemplate = $request->getRequiredParam('promptTemplate');
69-
$record->enabled = $request->getRequiredParam('enabled');
70+
if (!$model->validate()) {
71+
return $this->renderTemplate('chatgpt-integration/prompts/_edit', ['prompt' => $model]);
72+
}
7073

71-
$record->update();
74+
Plugin::getInstance()->promptService->updatePrompt($model);
7275

76+
return $this->redirect('chatgpt-integration/prompts');
7377
}
7478

7579
/**
7680
* @return mixed
77-
* @throws \yii\web\BadRequestHttpException
7881
*/
7982
public function actionDeletePrompt($id)
8083
{
8184
$record = Plugin::getInstance()->promptService->deletePrompt($id);
8285
return $this->redirect('chatgpt-integration/prompts');
8386
}
84-
85-
86-
protected function mapModelToRecord(PromptModel $model, ChatgptIntegration_PromptRecord $record): void
87-
{
88-
$record->label = $model->label;
89-
$record->promptTemplate = $model->promptTemplate;
90-
$record->enabled = $model->enabled;
91-
}
9287
}

src/controllers/SettingsController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
class SettingsController extends \craft\web\Controller
99
{
10+
/**
11+
* @return Response
12+
*/
1013
public function actionSettings(): Response
1114
{
1215
$settings = Plugin::getInstance()->getSettings();

src/migrations/Install.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22
namespace publishing\chatgptintegration\migrations;
33

4-
use Craft;
54
use craft\db\Migration;
6-
use craft\helpers\StringHelper;
75
use publishing\chatgptintegration\records\ChatgptIntegration_PromptRecord;
86

97
/**
@@ -31,6 +29,9 @@ public function safeDown(): bool
3129
return true;
3230
}
3331

32+
/**
33+
* @return void
34+
*/
3435
protected function createTables(): void
3536
{
3637

@@ -46,7 +47,10 @@ protected function createTables(): void
4647
]);
4748
}
4849

49-
protected function insertDefaultRows() {
50+
/**
51+
* @return void
52+
*/
53+
protected function insertDefaultRows(): void {
5054
$this->insert(ChatgptIntegration_PromptRecord::tableName(), array(
5155
'label' => 'Shorten',
5256
'promptTemplate' => 'Shorten the following text:'
@@ -63,6 +67,9 @@ protected function insertDefaultRows() {
6367
));
6468
}
6569

70+
/**
71+
* @return void
72+
*/
6673
protected function removeTables()
6774
{
6875
$tables = [

src/models/PromptModel.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,70 @@
22
namespace publishing\chatgptintegration\models;
33

44
use craft\base\Model;
5+
use DateTime;
56

67
class PromptModel extends Model
78
{
9+
/**
10+
* @var int
11+
*/
812
public int $id = 0;
13+
14+
/**
15+
* @var string
16+
*/
917
public string $label = '';
18+
19+
/**
20+
* @var string
21+
*/
1022
public string $promptTemplate = '';
23+
24+
/**
25+
* @var bool
26+
*/
1127
public bool $enabled = true;
12-
public \DateTime $dateCreated;
13-
public \DateTime $dateUpdated;
28+
29+
/**
30+
* @var DateTime
31+
*/
32+
public DateTime $dateCreated;
33+
34+
/**
35+
* @var DateTime
36+
*/
37+
public DateTime $dateUpdated;
38+
39+
/**
40+
* @var string
41+
*/
1442
public string $uid;
1543

16-
public function getDateCreated()
44+
/**
45+
* @return DateTime
46+
*/
47+
public function getDateCreated(): DateTime
1748
{
18-
if (!isset($this->dateCreated))
19-
$this->dateCreated = new \DateTime('now');
49+
if (!isset($this->dateCreated)) {
50+
$this->dateCreated = new DateTime('now');
51+
}
2052
return $this->dateCreated;
2153
}
2254

23-
public function getDateUpdated()
55+
/**
56+
* @return DateTime
57+
*/
58+
public function getDateUpdated(): DateTime
2459
{
25-
if (!isset($this->dateUpdated))
26-
$this->dateUpdated = new \DateTime('now');
60+
if (!isset($this->dateUpdated)) {
61+
$this->dateUpdated = new DateTime('now');
62+
}
2763
return $this->dateUpdated;
2864
}
2965

66+
/**
67+
* @return array
68+
*/
3069
public function defineRules(): array
3170
{
3271
$rules = parent::defineRules();

src/models/SettingsModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace publishing\chatgptintegration\models;
44

55
use craft\base\Model;
6+
use craft\helpers\App;
67

78
/**
89
* @var string $pluginName
@@ -26,4 +27,11 @@ class SettingsModel extends Model
2627
*/
2728
public bool $usePageLang = true;
2829

30+
/**
31+
* @return string
32+
*/
33+
public function getAccessToken(): string
34+
{
35+
return App::parseEnv($this->accessToken);
36+
}
2937
}

src/records/ChatgptIntegration_PromptRecord.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
class ChatgptIntegration_PromptRecord extends ActiveRecord
1515
{
16-
16+
/**
17+
* @return string
18+
*/
1719
public static function tableName()
1820
{
1921
return '{{%chatgptintegration_prompt}}';

0 commit comments

Comments
 (0)