/**
  * @param array|null $resource
  * @param Request    $request
  * @param Response   $response
  */
 public function __invoke(EventInterface $event)
 {
     $resource = $event->getParam('resource');
     $request = $event->getParam('request');
     $formattedResource = $this->formatter->format($resource, $request->params());
     $event->setParam('resource', $formattedResource);
 }
 /**
  * Add current `DateTime` value for field `last_updated_at`
  *
  * @param EventInterface $event
  */
 public function onUpdate(EventInterface $event)
 {
     $values = $event->getParam('values');
     $now = new DateTime();
     $values['last_updated_at'] = $now->format('Y-m-d h:i:s');
     $event->setParam('values', $values);
 }
Example #3
0
 /**
  * Display widget dashboard
  *
  * @param \Zend\EventManager\EventInterface $event Event
  *
  * @return void
  */
 public function dashboard(Event $event)
 {
     $commentModel = new Comment();
     $unactiveCommentList = $commentModel->getList(null, false);
     $activeCommentList = $commentModel->getList(null, true);
     $widgets = $event->getParam('widgets');
     $widgets['blog']['id'] = 'blog';
     $widgets['blog']['title'] = 'Blog information';
     $widgets['blog']['content'] = $this->addPath(__DIR__ . '/views')->render('dashboard.phtml', array('unactiveComments' => count($unactiveCommentList), 'activeComments' => count($activeCommentList)));
     $event->setParam('widgets', $widgets);
 }
 /**
  * ComposedObject target object resolver
  *
  * Resolves the interface (specification) into entity object class
  *
  * @param \Zend\EventManager\EventInterface $e
  */
 public function resolveComposedTargetObject($e)
 {
     $annotation = $e->getParam('annotation');
     if (!$annotation instanceof ComposedObject) {
         return;
     }
     $formSpec = $e->getParam('formSpec');
     if (!isset($formSpec['object'])) {
         return;
     }
     $metadata = $this->objectManager->getClassMetadata($formSpec['object']);
     $fieldName = $e->getParam('elementSpec')['spec']['name'];
     if ($metadata->hasAssociation($fieldName)) {
         $e->setParam('annotation', new ComposedObject(['value' => ['target_object' => $metadata->getAssociationTargetClass($fieldName), 'is_collection' => $annotation->isCollection(), 'options' => $annotation->getOptions()]]));
         return;
     }
     if (!empty($metadata->embeddedClasses[$fieldName])) {
         $class = $metadata->embeddedClasses[$fieldName]['class'];
         if (!is_object($class)) {
             $class = $this->objectManager->getClassMetadata($class)->getName();
         }
         $e->setParam('annotation', new ComposedObject(['value' => ['target_object' => $class, 'is_collection' => $annotation->isCollection(), 'options' => $annotation->getOptions()]]));
     }
 }
Example #5
0
 /**
  * @param \Zend\EventManager\EventInterface $e
  */
 public function handleAllowEmpty($e)
 {
     $formSpec = $e->getParam('formSpec');
     if (!(isset($formSpec['object']) && $this->objectManager->getMetadataFactory()->hasMetadataFor($formSpec['object']))) {
         return;
     }
     $metadata = $this->objectManager->getClassMetadata($formSpec['object']);
     $fieldName = $e->getParam('elementSpec')['spec']['name'];
     if (!$metadata->hasField($fieldName)) {
         return;
     }
     $fieldMapping = $metadata->getFieldMapping($fieldName);
     $inputSpec = $e->getParam('inputSpec');
     if (!isset($inputSpec['allow_empty'])) {
         $inputSpec['allow_empty'] = $fieldMapping['nullable'];
         $e->setParam('inputSpec', $inputSpec);
     }
 }
 /**
  * Allow creating fieldsets from composed entity properties
  *
  * @param  \Zend\EventManager\EventInterface $e
  * @return void
  */
 public function handleComposedObjectAnnotation($e)
 {
     $annotation = $e->getParam('annotation');
     if (!$annotation instanceof ComposedObject) {
         return;
     }
     $class = $annotation->getComposedObject();
     $annotationManager = $e->getTarget();
     $specification = $annotationManager->getFormSpecification($class);
     $name = $e->getParam('name');
     $elementSpec = $e->getParam('elementSpec');
     $filterSpec = $e->getParam('filterSpec');
     // Compose input filter into parent input filter
     $inputFilter = $specification['input_filter'];
     if (!isset($inputFilter['type'])) {
         $inputFilter['type'] = 'Zend\\InputFilter\\InputFilter';
     }
     $e->setParam('inputSpec', $inputFilter);
     unset($specification['input_filter']);
     // Compose specification as a fieldset into parent form/fieldset
     if (!isset($specification['type'])) {
         $specification['type'] = 'Zend\\Form\\Fieldset';
     }
     $elementSpec['spec'] = $specification;
     $elementSpec['spec']['name'] = $name;
 }
Example #7
0
 public function onSendingNode(EventInterface $e)
 {
     /** @var NodeInterface $node */
     $node = $e->getParam('node');
     if ($this->canInjectId($node)) {
         $this->injectNodeId($node);
     }
     if ($node->hasAttribute('t') && null == $node->getAttribute('t')) {
         $this->injectNodeTimestamp($node);
     }
     $e->setParam('node', $node);
 }
 /**
  * Allow creating fieldsets from composed entity properties
  *
  * @param  \Zend\EventManager\EventInterface $e
  * @return void
  */
 public function handleComposedObjectAnnotation($e)
 {
     $annotation = $e->getParam('annotation');
     if (!$annotation instanceof ComposedObject) {
         return;
     }
     $class = $annotation->getComposedObject();
     $annotationManager = $e->getTarget();
     $specification = $annotationManager->getFormSpecification($class);
     $name = $e->getParam('name');
     $elementSpec = $e->getParam('elementSpec');
     if ($annotation->isCollection()) {
         // Compose specification as a fieldset into parent form/fieldset
         if (!isset($specification['type'])) {
             //use input filter provider fieldset so we can compose the input filter into the fieldset
             //it is assumed that if someone uses a custom fieldset, they will take care of the input
             //filtering themselves or consume the input_filter_spec option.
             $specification['type'] = 'Zend\\Form\\InputFilterProviderFieldset';
         }
         $inputFilter = $specification['input_filter'];
         if (!isset($inputFilter['type'])) {
             $inputFilter['type'] = 'Zend\\InputFilter\\InputFilter';
         }
         unset($specification['input_filter']);
         $elementSpec['spec']['type'] = 'Zend\\Form\\Element\\Collection';
         $elementSpec['spec']['name'] = $name;
         $elementSpec['spec']['options'] = new ArrayObject($this->mergeOptions($elementSpec, $annotation));
         $elementSpec['spec']['options']['target_element'] = $specification;
         $elementSpec['spec']['options']['target_element']['options']['input_filter_spec'] = $inputFilter;
         if (isset($specification['hydrator'])) {
             $elementSpec['spec']['hydrator'] = $specification['hydrator'];
         }
     } else {
         // Compose input filter into parent input filter
         $inputFilter = $specification['input_filter'];
         if (!isset($inputFilter['type'])) {
             $inputFilter['type'] = 'Zend\\InputFilter\\InputFilter';
         }
         $e->setParam('inputSpec', $inputFilter);
         unset($specification['input_filter']);
         // Compose specification as a fieldset into parent form/fieldset
         if (!isset($specification['type'])) {
             $specification['type'] = 'Zend\\Form\\Fieldset';
         }
         if (isset($elementSpec['spec']['options'])) {
             $specification['options'] = isset($specification['options']) ? $specification['options'] : [];
             $specification['options'] = array_merge($elementSpec['spec']['options'], $specification['options']);
         }
         // Add element spec:
         $elementSpec['spec'] = $specification;
         $elementSpec['spec']['name'] = $name;
         $elementSpec['spec']['options'] = new ArrayObject($this->mergeOptions($elementSpec, $annotation));
     }
 }
Example #9
0
 public function onRenderEntity(EventInterface $e)
 {
     $payload = $e->getParam('payload');
     $newPayload = $this->parsePayload($payload);
     $e->setParam('payload', $newPayload);
 }
 /**
  * Normalizes event setting all expected parameters.
  *
  * @param EventInterface $event
  */
 protected function prepareEvent(EventInterface $event)
 {
     foreach (array('elementSpec', 'inputSpec') as $type) {
         if (!$event->getParam($type)) {
             $event->setParam($type, new ArrayObject());
         }
     }
     $elementSpec = $event->getParam('elementSpec');
     $inputSpec = $event->getParam('inputSpec');
     if (!isset($elementSpec['spec'])) {
         $elementSpec['spec'] = array();
     }
     if (!isset($inputSpec['filters'])) {
         $inputSpec['filters'] = array();
     }
     if (!isset($inputSpec['validators'])) {
         $inputSpec['validators'] = array();
     }
 }
 /**
  * @param \ArrayObject $entity
  * @param EventInterface $e
  * @return void
  */
 private function normalizeInputFilterEntityName($entity, EventInterface $e)
 {
     $entity['input_filter_name'] = str_replace('\\', '-', $entity['input_filter_name']);
     $e->setParam('entity', $entity);
 }
Example #12
0
 /**
  * Dashboard widget
  *
  * @param Event $e Event
  *
  * @return false|null
  */
 public function dashboard(Event $e)
 {
     $addthis = $e->getTarget()->getServiceLocator()->get('AddThisModel');
     $options = $addthis->getConfig();
     if (empty($options['show_stats'])) {
         return false;
     }
     if (isset($options['username'])) {
         $username = $options['username'];
     } else {
         return false;
     }
     if (isset($options['password'])) {
         $password = $options['password'];
     } else {
         return false;
     }
     if (isset($options['profile_id'])) {
         $profile = $options['profile_id'];
     } else {
         return false;
     }
     $document = DocumentModel::fromUrlKey('');
     $domain = parse_url($document->getUrl(true), PHP_URL_HOST);
     $requests = array(array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'day'), array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'week'), array('metric' => 'shares', 'dimension' => '', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'day'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'week'), array('metric' => 'clickbacks', 'dimension' => '', 'domain' => $domain, 'period' => 'month'), array('metric' => 'shares', 'dimension' => 'service', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => 'service', 'domain' => $domain, 'period' => 'month'), array('metric' => 'shares', 'dimension' => 'url', 'domain' => $domain, 'period' => 'month'), array('metric' => 'clickbacks', 'dimension' => 'url', 'domain' => $domain, 'period' => 'month'));
     $stats = array();
     foreach ($requests as $request) {
         $dimension = $metric = $domain = $period = '';
         extract($request);
         $dimension = $dimension != '' ? '/' . $dimension : '';
         $stats[$metric . $dimension . $period] = $this->executeQuery($metric, $dimension, $domain, $period, $username, $password, $profile);
         if (!$stats[$metric . $dimension . $period]) {
             $data = array('error' => true);
             break;
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 401) {
             $data = array('unauthorized' => true);
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 500) {
             $data = array('error' => true);
             break;
         } elseif ($stats[$metric . $dimension . $period]->getStatusCode() == 501) {
             $data = array('error' => true);
             break;
         }
     }
     if (empty($data) and !empty($stats['sharesday']) and $stats['sharesday']->getStatusCode() == 200) {
         if ($stats['sharesmonth']->getBody() == '[]') {
             $data = array('noData' => true);
         } else {
             $yesterday = array();
             $shareurls = json_decode($stats['shares/urlmonth']->getBody());
             $clickbackurls = json_decode($stats['clickbacks/urlmonth']->getBody());
             $yesterday['shares'] = json_decode($stats['sharesday']->getBody());
             $yesterday['shares'] = isset($yesterday['shares'][0]->shares) ? $yesterday['shares'][0]->shares : '';
             $yesterday['clickbacks'] = json_decode($stats['clickbacksday']->getBody());
             $yesterday['clickbacks'] = isset($yesterday['clickbacks'][0]->clickbacks) ? $yesterday['clickbacks'][0]->clickbacks : '';
             $yesterday['viral'] = $yesterday['shares'] > 0 && $yesterday['clickbacks'] > 0 ? $yesterday['clickbacks'] / $yesterday['shares'] * 100 . '%' : 'n/a';
             if (!$yesterday['clickbacks']) {
                 $yesterday['clickbacks'] = 0;
             }
             if (!$yesterday['shares']) {
                 $yesterday['shares'] = 0;
             }
             $lastweek = array();
             $decodedLastWeek = json_decode($stats['sharesweek']->getBody());
             $lastweek['shares'] = 0;
             foreach ($decodedLastWeek as $share) {
                 $lastweek['shares'] += $share->shares;
             }
             $decodedLastWeek = json_decode($stats['clickbacksweek']->getBody());
             $lastweek['clickbacks'] = 0;
             foreach ($decodedLastWeek as $clickback) {
                 $lastweek['clickbacks'] += $clickback->clickbacks;
             }
             $lastweek['viral'] = $lastweek['shares'] > 0 && $lastweek['clickbacks'] > 0 ? $lastweek['clickbacks'] / $lastweek['shares'] * 100 . '%' : 'n/a';
             $decodedLastMonth = json_decode($stats['sharesmonth']->getBody());
             $lastmonth = array();
             $lastmonth['shares'] = 0;
             foreach ($decodedLastMonth as $share) {
                 $lastmonth['shares'] += $share->shares;
             }
             $decodedLastMonth = json_decode($stats['clickbacksmonth']->getBody());
             $lastmonth['clickbacks'] = 0;
             foreach ($decodedLastMonth as $clickback) {
                 $lastmonth['clickbacks'] += $clickback->clickbacks;
             }
             $lastmonth['viral'] = $lastmonth['shares'] > 0 && $lastmonth['clickbacks'] ? $lastmonth['clickbacks'] / $lastmonth['shares'] * 100 . '%' : 'n/a';
             $services = array();
             $services['shares'] = json_decode($stats['shares/servicemonth']->getBody());
             if (is_null($services['shares'])) {
                 $services['shares'] = array();
             }
             $services['clickbacks'] = json_decode($stats['clickbacks/servicemonth']->getBody());
             if (is_null($services['clickbacks'])) {
                 $services['clickbacks'] = array();
             }
             $data = array('yesterday' => $yesterday, 'lastweek' => $lastweek, 'lastmonth' => $lastmonth, 'shareurls' => $shareurls, 'clickbackurls' => $clickbackurls, 'domain' => $domain);
         }
     }
     if (!empty($data)) {
         $widgets = $e->getParam('widgets');
         $widgets['addthis']['id'] = 'addthis';
         $widgets['addthis']['title'] = 'AddThis';
         $widgets['addthis']['content'] = $this->addPath(__DIR__ . '/views')->render('dashboard.phtml', $data);
         $e->setParam('widgets', $widgets);
     }
 }
Example #13
0
 public function preUpdate(Event $e, $moduleName, $name)
 {
     /** @var EntityInterface $entity */
     $entity = $e->getParam('entity');
     $entityName = ucfirst($name);
     /** @var \Zend\Mvc\I18n\Translator $translator */
     $translator = $this->getServiceLocator()->get('MvcTranslator');
     $defaultLocale = 'ru_UA';
     $finder = $this->getServiceLocator()->get("T4webTranslate\\{$entityName}\\Service\\Finder");
     /** @var EntityInterface $defaultEntity */
     $defaultEntity = $finder->find(['Translate' => ['Translate' => ['EntityId' => $entity->getId()], 'Languages' => ['Locale' => $translator->getLocale()]]]);
     $update = $this->getServiceLocator()->get("T4webTranslate\\{$entityName}\\Service\\Update");
     $dataEntity = $entity->extract();
     unset($dataEntity['id']);
     $data = $defaultEntity->populate($dataEntity)->extract();
     $update->update($defaultEntity->getId(), $data);
     if ($translator->getLocale() != $defaultLocale) {
         /** @var EntityInterface $translateEntity */
         $defaultEntity = $finder->find(['Translate' => ['Translate' => ['EntityId' => $entity->getId()], 'Languages' => ['Locale' => $defaultLocale]]]);
         $dataEntity = $defaultEntity->extract();
         unset($dataEntity['id']);
         if ($defaultEntity) {
             $entity->populate($dataEntity);
         }
         $e->setParam('entity', $entity);
     }
 }