/**
  * Get registry.
  *
  * @return \yii\db\ActiveRelation
  */
 public function getRegistry()
 {
     return $this->hasOne(Registry::className(), ['id' => 'id']);
 }
 /**
  * Get object.
  *
  * @return \yii\db\ActiveRelation
  */
 public function getObject()
 {
     return $this->hasOne(Registry::className(), ['id' => 'object_id']);
 }
Beispiel #3
0
 /**
  * [[@doctodo method_description:actionWatch]].
  *
  * @throws HttpException [[@doctodo exception_description:HttpException]]
  */
 public function actionWatch()
 {
     if (empty($_GET['id']) or !($object = $this->params['object'] = Registry::getObject($_GET['id'], false)) or !($typeItem = $this->params['typeItem'] = $object->objectTypeItem)) {
         throw new HttpException(404, "Unknown object.");
     }
     if (!$object->can('read')) {
         throw new HttpException(403, "Unable to access object.");
     }
     Yii::$app->request->object = $object;
     $watching = empty($_GET['stop']);
     if ($object->watch($watching)) {
         Yii::$app->response->task = 'trigger';
         if ($watching) {
             Yii::$app->response->success = 'You are now watching ' . $object->descriptor . '.';
             Yii::$app->response->trigger = [['startedWatching']];
         } else {
             Yii::$app->response->success = 'You stopped watching ' . $object->descriptor . '.';
             Yii::$app->response->trigger = [['stoppedWatching']];
         }
     } else {
         Yii::$app->response->error = 'Unable update the watching status of this object.';
     }
 }
Beispiel #4
0
 /**
  * [[@doctodo method_description:searchLocal]].
  *
  * @param cascade\components\dataInterface\DataItem $item         [[@doctodo param_description:item]]
  * @param array                                     $searchParams [[@doctodo param_description:searchParams]] [optional]
  *
  * @return [[@doctodo return_type:searchLocal]] [[@doctodo return_description:searchLocal]]
  */
 public function searchLocal(DataItem $item, $searchParams = [])
 {
     if (empty($searchParams['searchFields'])) {
         $searchParams['searchFields'] = $this->localFields;
     }
     if (empty($searchParams['searchFields'])) {
         return;
     }
     if (!isset($searchParams['limit'])) {
         $searchParams['limit'] = 5;
     }
     $searchParams['skipForeign'] = true;
     $query = [];
     foreach ($this->foreignFields as $field) {
         if (!empty($item->foreignObject->{$field})) {
             $value = $item->foreignObject->{$field};
             if (isset($this->foreignFilter)) {
                 $value = call_user_func($this->foreignFilter, $value);
             }
             $query[] = $value;
         }
     }
     if (empty($query)) {
         return;
     }
     $localClass = $this->dataSource->localModel;
     $searchResults = $localClass::searchTerm(implode(' ', $query), $searchParams);
     $resultsLeft = $totalResults = count($searchResults);
     foreach ($searchResults as $k => $r) {
         // if ($r->descriptor === $query[0]) { continue; }
         $score = $resultsLeft / $totalResults * 100 * 0.2 + StringHelper::compareStrings($r->descriptor, implode(' ', $query)) * 0.8;
         $r->score = $score;
         if ($score < $this->threshold) {
             unset($searchResults[$k]);
         } else {
             $reverseKey = $this->dataSource->getReverseKeyTranslation($r->id);
             if (!empty($reverseKey)) {
                 unset($searchResults[$k]);
             }
         }
         $resultsLeft--;
     }
     ArrayHelper::multisort($searchResults, 'scoreSort', SORT_DESC);
     $searchResults = array_values($searchResults);
     if (empty($searchResults)) {
         return;
     } elseif ($safeResult = $this->getSafeResult($searchResults)) {
         $this->dataSource->task->addInfo('Auto-matched "' . implode(' ', $query) . '" to "' . $safeResult->descriptor . '" (Score: ' . round($safeResult->score, 2) . ')', ['query' => $query, 'foreignObject' => $item->foreignObject->attributes, 'localObject' => ['id' => $safeResult->id, 'descriptor' => $safeResult->descriptor]]);
         return $safeResult->object;
     } else {
         $options = [];
         $resultsNice = [];
         $optionNumber = 1;
         $defaultOption = 'new';
         if ($searchResults[0]->score > $this->defaultOptionThreshold) {
             $defaultOption = '1';
         }
         foreach ($searchResults as $result) {
             $resultsNice['' . $optionNumber] = $result;
             $options['' . $optionNumber] = $result->descriptor . ' (' . round($result->score) . '%)';
             $optionNumber++;
         }
         $options['new'] = 'Create New Object';
         $options['selectObject'] = 'Select Object';
         $select = false;
         $interactionOptions = ['inputType' => 'select', 'options' => $options];
         $interactionOptions['details'] = $item->foreignObject->attributes;
         $interactionOptions['default'] = $defaultOption;
         $callback = ['callback' => function ($response) use(&$select, $options) {
             if (empty($response)) {
                 return false;
             }
             if (substr($response, 0, 2) !== '*-' && !isset($options[$response])) {
                 return false;
             }
             $select = $response;
             return true;
         }];
         if (!$this->dataSource->action->createInteraction('Match Object (' . implode(' ', $query) . ')', $interactionOptions, $callback)) {
             return false;
         }
         if ($select === 'new') {
             $this->dataSource->task->addInfo('For "' . implode(' ', $query) . '" user chose to to create new object', ['query' => $query, 'foreignObject' => $item->foreignObject->attributes]);
             return;
         } elseif (substr($select, 0, 2) === '*-') {
             $matchedObjectId = substr($select, 2);
             $matchedObject = Registry::getObject($matchedObjectId, false);
             if (!$matchedObject) {
                 $this->dataSource->task->addWarning('For "' . implode(' ', $query) . '" user tried to match it to a different object, but object wasn\'t found! Created new object.', ['query' => $query, 'foreignObject' => $item->foreignObject->attributes, 'matchedObject' => $matchedObjectId]);
                 return;
             }
             $this->dataSource->task->addInfo('For "' . implode(' ', $query) . '" matched to an existing object "' . $matchedObject->descriptor . '"', ['query' => $query, 'foreignObject' => $item->foreignObject->attributes, 'matchedObject' => $matchedObject->attributes]);
             return $matchedObject;
         } else {
             $this->dataSource->task->addInfo('User matched "' . implode(' ', $query) . '" to "' . $resultsNice[$select]->descriptor . '"', ['query' => $query, 'foreignObject' => $item->foreignObject->attributes, 'localObject' => $resultsNice[$select]->id]);
             return $resultsNice[$select]->object;
         }
     }
 }
Beispiel #5
0
 /**
  * Get top contributors.
  *
  * @param [[@doctodo param_type:parentObject]] $parentObject [[@doctodo param_description:parentObject]]
  * @param array                                $options      [[@doctodo param_description:options]] [optional]
  *
  * @return [[@doctodo return_type:getTopContributors]] [[@doctodo return_description:getTopContributors]]
  */
 public function getTopContributors($parentObject, $options = [])
 {
     $individualType = Yii::$app->collectors['types']->getOne('Individual')->object;
     $individualModelClass = $individualType->primaryModel;
     $individualModelAlias = $individualModelClass::modelAlias();
     $taxonomyType = Yii::$app->collectors['taxonomies']->getOne('ic_time_individual_role');
     $taxonomy = $taxonomyType->getTaxonomy('contributor');
     $limit = isset($options['limit_contributors']) ? $options['limit_contributors'] : 5;
     $query = $this->getBaseStatsQuery($parentObject);
     $query->join('LEFT JOIN', Relation::tableName() . ' r2', 'r2.child_object_id=innerQuery.id');
     $query->join('LEFT JOIN', Registry::tableName() . ' reg', 'r2.parent_object_id=reg.id');
     $query->join('LEFT JOIN', RelationTaxonomy::tableName() . ' tax', 'r2.id=tax.relation_id');
     $query->select(['r2.parent_object_id', 'SUM(`hours`) as sum']);
     $query->groupBy(['r2.parent_object_id']);
     $query->andWhere(['reg.object_model' => $individualModelAlias]);
     $query->andWhere(['tax.taxonomy_id' => $taxonomy->primaryKey]);
     $query->addOrderBy(['SUM(`hours`)' => SORT_DESC]);
     $query->limit($limit);
     $c = [];
     foreach ($query->each() as $row) {
         $individual = Registry::getObject($row['parent_object_id']);
         if (!$individual) {
             continue;
         }
         $c[$row['parent_object_id']] = ['label' => $individual->descriptor, 'sum' => $row['sum']];
     }
     return $c;
 }
Beispiel #6
0
 /**
  * @inheritdoc
  */
 public function getRequestors($accessingObject, $firstLevel = true)
 {
     $individual = false;
     if (empty($accessingObject)) {
         return false;
     }
     if ($accessingObject->modelAlias === 'cascade\\models\\User' && isset($accessingObject->object_individual_id)) {
         $individual = Registry::getObject($accessingObject->object_individual_id, false);
     } elseif ($accessingObject->modelAlias === ':Individual\\ObjectIndividual') {
         $individual = $accessingObject;
     }
     if ($individual) {
         $requestors = [$individual->primaryKey];
         foreach ($this->collectorItem->parents as $parentType) {
             if ($parentType->parent->getBehavior('Authority') !== null) {
                 if (($parentRequestors = $parentType->parent->getRequestors($individual, false)) && !empty($parentRequestors)) {
                     $requestors = array_merge($requestors, $parentRequestors);
                 }
             }
         }
         return $requestors;
     }
     return false;
 }
 public static function getRegistryClass()
 {
     return Registry::className();
 }
Beispiel #8
0
 /**
  * [[@doctodo method_description:startRequest]].
  */
 public function startRequest()
 {
     if (isset($_GET['p'])) {
         $this->_previousObject = Registry::getObject($_GET['p']);
     }
 }
Beispiel #9
0
 /**
  * [[@doctodo method_description:actionStream]].
  *
  * @return [[@doctodo return_type:actionStream]] [[@doctodo return_description:actionStream]]
  */
 public function actionStream()
 {
     header("Content-type: text/plain");
     header("Access-Control-Allow-Origin: *");
     ob_implicit_flush(1);
     Yii::$app->response->task = false;
     $refreshed = [];
     $source = $_POST;
     $stream = false;
     if (!empty($source['__TODO_FIGURE_OUT_VIBE_STREAM_HINT__'])) {
         // @todo figure out this vibe hint thing
         $stream = true;
     }
     if (isset($source['when']) && $source['when'] === 'handshake') {
         echo json_encode(['id' => md5(microtime(true)), 'transports' => ['stream']]);
         return;
     }
     if (empty($source['requests'])) {
         echo json_encode([]);
         return;
     }
     $nonStreamResult = [];
     $baseInstrictions = isset($source['baseInstructions']) ? $source['baseInstructions'] : [];
     foreach ($source['requests'] as $requestId => $request) {
         $refreshed[$requestId] = false;
         $instructions = $baseInstrictions;
         if (isset($request['instructions'])) {
             $instructions = array_merge($instructions, $request['instructions']);
         }
         if (empty($instructions['type']) || empty($instructions['type'])) {
             continue;
         }
         if (isset($request['state'])) {
             foreach ($request['state'] as $key => $value) {
                 Yii::$app->webState->set($key, $value);
             }
         }
         if (isset($instructions['objectId'])) {
             $object = Yii::$app->request->object = Registry::getObject($instructions['objectId']);
             if (!$object) {
                 $refreshed[$requestId] = ['error' => 'Invalid object ' . $instructions['objectId'] . ''];
                 continue;
             }
             $type = $object->objectType;
         }
         $settings = isset($instructions['settings']) ? $instructions['settings'] : [];
         switch ($instructions['type']) {
             case 'widget':
                 $widget = false;
                 if (isset($object)) {
                     $widgets = $object->objectTypeItem->widgets;
                     if (isset($widgets[$instructions['systemId']])) {
                         $widget = $widgets[$instructions['systemId']]->object;
                     }
                 } else {
                     $widget = Yii::$app->collectors['widgets']->getOne($instructions['systemId']);
                 }
                 if (!$widget) {
                     $refreshed[$requestId] = ['error' => 'Unknown widget'];
                     return;
                 }
                 $widgetObject = $widget->object;
                 if (isset($instructions['section']) && ($sectionItem = Yii::$app->collectors['sections']->getOne($instructions['section'])) && ($section = $sectionItem->object)) {
                     $widgetObject->attachDecorator($section->widgetDecoratorClass);
                     $widgetObject->section = $section;
                 }
                 $widgetObject->owner = $widget->owner;
                 $refreshed[$requestId] = ['content' => $widgetObject->generate()];
                 break;
         }
         if ($refreshed[$requestId]) {
             if ($stream) {
                 echo "data: " . json_encode(['type' => 'handleRequests', 'data' => [$requestId => $refreshed[$requestId]], 'id' => round(microtime(true) * 100)]);
                 echo "\n\n";
             } else {
                 $nonStreamResult[$requestId] = $refreshed[$requestId];
             }
             //echo str_repeat("\n\n",1024*4);
         }
     }
     if (!$stream) {
         echo json_encode(['requests' => $nonStreamResult]);
     }
     ob_implicit_flush(0);
     ob_start();
     Yii::$app->end();
     ob_end_clean();
 }