|
@@ -0,0 +1,92 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace DeviceBundle\Twig;
|
|
|
+
|
|
|
+use WebserviceBundle\Services\Webservice;
|
|
|
+
|
|
|
+class DeviceLogExtension extends \Twig_Extension
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var Webservice
|
|
|
+ */
|
|
|
+ protected $webservice;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $deviceGetUrl;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $deviceLogGetUrl;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Webservice $webservice
|
|
|
+ * @param string $deviceGetUrl
|
|
|
+ * @param string $deviceLogGetUrl
|
|
|
+ */
|
|
|
+ public function __construct(Webservice $webservice, $deviceGetUrl, $deviceLogGetUrl)
|
|
|
+ {
|
|
|
+ $this->webservice = $webservice;
|
|
|
+ $this->deviceGetUrl = $deviceGetUrl;
|
|
|
+ $this->deviceLogGetUrl = $deviceLogGetUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getFunctions()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ new \Twig_SimpleFunction('device_log', array($this, 'getDeviceLog')),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Entity $object
|
|
|
+ * @param int $limit
|
|
|
+ * @param int $offset
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDeviceLog($object, $limit = 50, $offset = 0)
|
|
|
+ {
|
|
|
+ $filters = array(
|
|
|
+ 'deviceType' => get_class($object),
|
|
|
+ 'deviceId' => $object->getId(),
|
|
|
+ );
|
|
|
+
|
|
|
+ $device = $this->webservice->get($this->deviceGetUrl, $filters);
|
|
|
+
|
|
|
+ $log = '';
|
|
|
+ if (count($device)) {
|
|
|
+ $filters = array(
|
|
|
+ 'device' => $device[0]['id'],
|
|
|
+ );
|
|
|
+// $order = array(
|
|
|
+// 'created' => 'DESC'
|
|
|
+// );
|
|
|
+ $deviceLogs = $this->webservice->get($this->deviceLogGetUrl, $filters, array(), $limit, $offset);
|
|
|
+
|
|
|
+ foreach ($deviceLogs as $deviceLog) {
|
|
|
+ unset($deviceLog['device']);
|
|
|
+ $created = new \DateTime($deviceLog['created']);
|
|
|
+ $log .= $created->format('d/m/Y H:i') . ' - ' . $deviceLog['message'] . PHP_EOL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $log;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function getName()
|
|
|
+ {
|
|
|
+ return 'device_log_extension';
|
|
|
+ }
|
|
|
+
|
|
|
+}
|