255], [['text'], 'string'], [['trigger_ids'], 'each', 'rule' => ['integer']], [['type'], 'in', 'range' => self::getTypesArray()], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'type' => 'Тип', 'item_id' => 'Элемент', 'item_value' => 'Значение Элемента', 'trigger_ids' => 'Triggers', 'name' => 'Название', 'text' => 'Текст', ]; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => LinkerBehavior::className(), 'relations' => [ 'trigger_ids' => 'triggers', ], ], ]; } /** * @return array */ public static function getList() { return ArrayHelper::map(self::find()->all(), 'id', 'name'); } /** * @return array */ public static function getTypes() { return [ self::TYPE_ITEM_VALUE => 'Изменить значение Элемента', self::TYPE_NOTIFICATION_TELEGRAM => 'Сообщение Telegram', ]; } /** * @return array */ public static function getTypesArray() { return array_keys(self::getTypes()); } /** * @return string */ public function getTypeLabel() { return self::getTypes()[$this->type]; } /** * @return \yii\db\ActiveQuery */ public function getTriggers() { return $this->hasMany(Trigger::className(), ['id' => 'trigger_id']) ->viaTable('trigger_task', ['task_id' => 'id']); } /** * @return \yii\db\ActiveQuery */ public function getItem() { return $this->hasOne(Item::className(), ['id' => 'item_id']); } /** * @return bool */ public function sendNotificationTelegram() { $url = 'https://api.telegram.org/bot' . Yii::$app->params['telegramBotApiKey'] . '/sendMessage?chat_id=' . Yii::$app->params['telegramBotChatId'] . '&text=' . $this->text; $result = file_get_contents($url); $data = Json::decode($result); if (isset($data['ok']) and $data['ok']) { return true; } return false; } }