/** * Handles PRE_SAVE storage event * * @param StorageEvent $event */ public function onPreSave(StorageEvent $event) { $contenttype = $event->getContentType(); // record contenttype $record = $event->getContent(); // record itself $created = $event->isCreate(); // if record was created, updated or deleted, for more information see the page in the documentation // Do whatever you want with this data // See page in the documentation for a logging example }
/** * It should detect already hashed passwords. * * @dataProvider providePreSaveAlreadyHashed */ public function testOnPreSavePasswordAlreadyHashed($hash) { $this->storageEvent->getContent()->willReturn($this->user->reveal()); $this->user->getPassword()->willReturn($hash); $this->passwordFactory->createHash(Argument::cetera())->shouldNotBeCalled(); $this->user->setPassword($hash)->shouldBeCalled(); $this->listener->onUserEntityPreSave($this->storageEvent->reveal()); }
/** * Pre-save storage event. * * @param StorageEvent $event */ public function onPreSave(StorageEvent $event) { /** @var Entity\Entity $entityRecord */ $entityRecord = $event->getContent(); if ($entityRecord instanceof Entity\Users) { $this->passwordHash($entityRecord); } }
/** * Encode array fields as json string just before save it in the storage. * * @param StorageEvent $event */ public function arrayToJson(StorageEvent $event) { $content = $event->getContent(); foreach ($content->contenttype['fields'] as $key => $options) { if ($options['type'] == 'array') { $content->values[$key] = json_encode(array_values($content->values[$key])); } } }
public function testSetup() { $app = $this->getApp(); $content = new Content($app); $event = new StorageEvent($content); $this->assertEquals(null, $event->isCreate()); $this->assertEquals($content, $event->getContent()); $this->assertEquals(null, $event->getId()); $this->assertEquals(null, $event->getContentType()); }
/** * Remove sessions & authtokens when a user is deleted. * * @param StorageEvent $event */ public function onStorageEventPreDelete(StorageEvent $event) { /** @var Entity\Users $userEntity */ $userEntity = $event->getContent(); if (!$userEntity instanceof \Bolt\Storage\Entity\Users) { return; } $this->deleteAuthtokens($userEntity); $this->deleteSessions($userEntity); }
/** * Handles PRE_SAVE storage event * * @param StorageEvent $event */ public function onPreSave(StorageEvent $event) { // The ContentType of the record being saved $contenttype = $event->getContentType(); // The record being saved $record = $event->getContent(); // A flag to tell if the record was created, updated or deleted, // for more information see the page in the documentation $created = $event->isCreate(); // Do whatever you want with this data // See page in the documentation for a logging example }
/** * Post-save testing event. * * @param StorageEvent $event */ public function eventPostSave(StorageEvent $event) { $contenttype = $event->getContentType(); if ($contenttype === 'pages') { $repo = $this->app['storage']->getRepository($contenttype); $record = $event->getContent(); $values = $record->serialize(); if ($event->isCreate()) { // Add a unique paragraph to the end of the body $record->setBody($values['body'] . '<p>Snuck in to body during POST_SAVE on create: ' . date('Y-m-d H:i:s') . '</p>'); } else { // Add a unique paragraph to the end of the body $record->setBody($values['body'] . '<p>Added to body during POST_SAVE on save: ' . date('Y-m-d H:i:s') . '</p>'); } // Save the changes to the database $repo->save($record, true); } }
/** * Dispatch the event to IFTTT channel * * Only control if the content belongs to allowed content-types to broadcast. */ public function dispatchToChannel(\Bolt\Events\StorageEvent $event) { $id = $event->getId(); $contenttype = $event->getContentType(); $content = $event->getContent(); // Only allowed content types in published state. if (!in_array($contenttype, $this->config['content_types'])) { return; } // Payload $payload = array('value1' => $content->get('title'), 'value2' => $this->app['resources']->getUrl('hosturl') . $content->link()); $event_name = $this->config['event_name']; try { $this->sendRequest($event_name, $payload); $this->log("Channel notified with event '{$event_name}' for {$contenttype}#{$id}"); } catch (\Guzzle\Http\Exception\BadResponseException $e) { $code = $e->getResponse()->getStatusCode(); $message = $e->getMessage(); $this->log("Error : HTTP/{$code} - {$message}", "error"); } }
/** * Handles POST_DELETE storage event * * @param StorageEvent $event */ public function onPostDelete(StorageEvent $event) { $id = $event->getId(); // $contenttype = $event->getContentType(); $record = $event->getContent(); }
/** * Replace shortcodes with actual tweet content * * @uses getEmbed() * @param StorageEvent $event */ function preSave(StorageEvent $event) { $record = $event->getContent(); $newbody = preg_replace_callback($this->regex, array($this, 'getEmbed'), $record->get('body')); $record->setValue('body', $newbody); }
/** * preSaveCallback. * * This callback is used to store the content of translated fields * on content type update. It is called by the event dispatcher. */ public function preSaveCallback(StorageEvent $event) { $default_locale = $this->app['config']->get('general/locale', 'en_GB'); $prefix = $this->app['config']->get('general/database/prefix', 'bolt_'); $content_type = $event->getContentType(); $content_type_id = $event->getId(); $content = $event->getContent()->getValues(); $content_type_config = $this->app['config']->get('contenttypes/' . $content_type); $locale_field = null; foreach ($content_type_config['fields'] as $name => $field) { if ($field['type'] == 'locale') { $locale_field = $name; break; } } if (!$content_type_id || !$locale_field || $content[$locale_field] === $default_locale) { return; } $translatable_fields = $this->getTranslatableFields($content_type_config['fields']); $query = 'SELECT * FROM ' . $prefix . $content_type . ' WHERE id = :content_type_id'; $default_content = $this->app['db']->fetchAssoc($query, array(':content_type_id' => $content_type_id)); foreach ($translatable_fields as $translatable_field) { $fieldtype = $content_type_config['fields'][$translatable_field]['type']; if (is_a($content[$translatable_field], 'Bolt\\Content')) { $content[$translatable_field] = json_encode($content[$translatable_field]->getValues(true, true)); } if ($fieldtype === "video") { $content[$translatable_field]['html'] = (string) $content[$translatable_field]['html']; $content[$translatable_field]['responsive'] = (string) $content[$translatable_field]['responsive']; } if (in_array($fieldtype, $this->serializedFieldTypes) && !is_string($content[$translatable_field])) { $content[$translatable_field] = json_encode($content[$translatable_field]); } $content_type_config['fields'][$translatable_field]; // Create/update translation entries $query = 'REPLACE INTO ' . $prefix . 'translation (locale, field, content_type_id, content_type, value) VALUES (?, ?, ?, ?, ?)'; $this->app['db']->executeQuery($query, array($content[$locale_field], $translatable_field, $content_type_id, $content_type, (string) $content[$translatable_field])); // Reset values to english $content[$translatable_field] = $default_content[$translatable_field]; } $content[$locale_field] = $default_locale; $event->getContent()->setValues($content); }
/** * StorageEvents::PRE_SAVE event callback. * * @param StorageEvent $event */ public function preSave(StorageEvent $event) { $request = $this->requestStack->getCurrentRequest(); if ($request === null) { return; } $contentType = $this->boltConfig->get('contenttypes/' . $event->getContentType()); $translatableFields = $this->getTranslatableFields($contentType['fields']); /** @var Content $record */ $record = $event->getContent(); $values = $record->serialize(); $localeValues = []; if (empty($translatableFields)) { return; } $localeSlug = $request->get('_locale'); $record->set($localeSlug . 'slug', $values['slug']); $locales = $this->config->getLocales(); if ($values['_locale'] == reset($locales)->getSlug()) { $record->set($localeSlug . 'data', '[]'); return; } if ($values['id']) { /** @var Content $defaultContent */ $defaultContent = $this->query->getContent($event->getContentType(), ['id' => $values['id'], 'returnsingle' => true]); } if (in_array('templatefields', $translatableFields)) { $templateFields = $this->boltConfig->get('theme/templatefields/' . $values['template'] . '/fields'); foreach ($templateFields as $key => $field) { if ($field['type'] === 'repeater') { $values['templatefields'][$key] = json_encode($values['templatefields'][$key]); } } } foreach ($translatableFields as $field) { $localeValues[$field] = $values[$field]; if ($values['id']) { $record->set($field, $defaultContent->get($field)); } else { $record->set($field, ''); } } $localeJson = json_encode($localeValues); $record->set($localeSlug . 'data', $localeJson); }