public function offsetSet($index, $newval) { if ($index != NULL) { CodeGuard::checkTypeAndThrow($index, 'integer'); } parent::offsetSet($index, $newval); }
/** * @param string $key * @param ReferenceList $model * @return array */ public function encodeReferenceList($key, $model) { if ($key != 'projects') { return parent::encodeReferenceList($key, $model); } $domain = $this->_website->domain; $result = array_map(function ($id) use($domain) { CodeGuard::checkTypeAndThrow($id, 'Api\\Model\\Mapper\\Id'); $projectDto = null; try { $projectModel = new ProjectModel($id->asString()); // Filter for active projects on the same domain. // Also exclude projects that don't have things to modify on User Profile // userProfilePropertiesEnabled is type ArrayOf, so testing for empty() didn't work if (!$projectModel->isArchived && $projectModel->siteName == $domain && count($projectModel->userProperties->userProfilePropertiesEnabled) > 0) { $projectDto = array(); $projectDto['id'] = $id->asString(); $projectDto['name'] = $projectModel->projectName; $projectDto['userProperties'] = self::encode($projectModel->userProperties); } } catch (\Exception $e) { } return $projectDto; }, $model->refs); // Filter out empty entries in the project list return array_values(array_filter($result)); }
/** * Updates the given LexEntry in $projectId * @param string $projectId * @param array $params * @param string $userId * @param string $mergeQueuePath * @param string $pidFilePath * @param string $command * @return bool|array<encoded LexEntryModel> if the project is syncing (or on hold) return false (no save)FixSe */ public static function updateEntry($projectId, $params, $userId, $mergeQueuePath = null, $pidFilePath = null, $command = null) { CodeGuard::checkTypeAndThrow($params, 'array'); $project = new LexProjectModel($projectId); ProjectCommands::checkIfArchivedAndThrow($project); $now = UniversalTimestamp::now(); if (array_key_exists('id', $params) && $params['id'] != '') { $entry = new LexEntryModel($project, $params['id']); $action = 'update'; } else { $entry = new LexEntryModel($project); $entry->authorInfo->createdByUserRef->id = $userId; $entry->authorInfo->createdDate = $now; $entry->guid = Guid::create(); $action = 'create'; // TODO: Consider adding more specific activity entry: which fields were modified? 2014-09-03 RM // E.g., "User _____ updated entry _____ by adding a new sense with definition ______" } $entry->authorInfo->modifiedDate = $now; $entry->authorInfo->modifiedByUserRef->id = $userId; if ($project->hasSendReceive()) { // $entry->dirtySR++; $entry->dirtySR = 0; $status = SendReceiveCommands::getProjectStatus($projectId); if ($status && $status['SRState'] != 'IDLE') { return false; } } LexEntryDecoder::decode($entry, $params); $entry->write(); ActivityCommands::writeEntry($project, $userId, $entry, $action); // SendReceiveCommands::queueProjectForUpdate($project, $mergeQueuePath); // SendReceiveCommands::startLFMergeIfRequired($projectId, 'merge', $pidFilePath, $command); return JsonEncoder::encode($entry); }
public static function updateReply($projectId, $userId, $website, $commentId, $params) { CodeGuard::checkTypeAndThrow($params, 'array'); CodeGuard::checkEmptyAndThrow($commentId, 'commentId in updateReply()'); $project = new LexiconProjectModel($projectId); $comment = new LexCommentModel($project, $commentId); $rightsHelper = new RightsHelper($userId, $project, $website); $replyId = $params['id']; if (array_key_exists('id', $params) && $replyId != '') { $reply = $comment->getReply($replyId); if ($reply->authorInfo->createdByUserRef->asString() != $userId && !$rightsHelper->userHasProjectRight(Domain::COMMENTS + Operation::EDIT)) { throw new \Exception("No permission to update other people's lex comment replies!"); } if ($reply->content != $params['content']) { $reply->authorInfo->modifiedDate = new \DateTime(); } $reply->content = $params['content']; $comment->setReply($replyId, $reply); } else { $reply = new LexCommentReply(); $reply->content = $params['content']; $reply->authorInfo->createdByUserRef->id = $userId; $reply->authorInfo->modifiedByUserRef->id = $userId; $reply->authorInfo->createdDate = new \DateTime(); $comment->replies->append($reply); $replyId = $reply->id; } $comment->write(); return $replyId; }
/** * Updates the given LexEntry in $projectId * @param string $projectId * @param array $params * @param string $userId * @return LexEntryModel */ public static function updateEntry($projectId, $params, $userId) { CodeGuard::checkTypeAndThrow($params, 'array'); $project = new LexiconProjectModel($projectId); if (array_key_exists('id', $params) && $params['id'] != '') { $entry = new LexEntryModel($project, $params['id']); $action = 'update'; } else { $entry = new LexEntryModel($project); $entry->authorInfo->createdByUserRef->id = $userId; $entry->authorInfo->createdDate = new \DateTime(); $action = 'create'; // TODO: Consider adding more specific activity entry: which fields were modified? 2014-09-03 RM // E.g., "User _____ updated entry _____ by adding a new sense with definition ______" } // set authorInfo $entry->authorInfo->modifiedDate = new \DateTime(); $entry->authorInfo->modifiedByUserRef->id = $userId; $params = self::recursiveRemoveEmptyFieldValues($params); //$params = self::recursiveAlignCustomFieldsWithModel($params); JsonDecoder::decode($entry, $params); $entry->write(); ActivityCommands::writeEntry($project, $userId, $entry, $action); return JsonEncoder::encode($entry); }
/** * @param string $key * @param object $model * @param MongoDate $data */ public function decodeDateTime($key, $model, $data) { CodeGuard::checkTypeAndThrow($data, 'MongoDate', CodeGuard::CHECK_NULL_OK); if ($data !== null) { $model->setTimeStamp($data->sec); } }
/** * @see addRef - this should only be called by the addRef method of other ReferenceLists * @param string $id */ public function _addRef($id) { CodeGuard::checkTypeAndThrow($id, 'string'); // CARRY ON HERE CP :-) $idModel = new Id($id); if (!in_array($idModel, $this->refs)) { $this->refs[] = $idModel; } // TODO log if ref already exists? }
/** * Writes the model to the mongo collection * @return string The unique id of the object written * @see MongoMapper::write() */ public function write() { CodeGuard::checkTypeAndThrow($this->id, 'Api\\Model\\Shared\\Mapper\\Id'); $now = UniversalTimestamp::now(); $this->dateModified = $now; if (Id::isEmpty($this->id)) { $this->dateCreated = $now; } $this->id->id = $this->_mapper->write($this, $this->id->id); return $this->id->id; }
function _createCustomField($data) { CodeGuard::checkTypeAndThrow($data, 'array'); if (array_key_exists('value', $data)) { return new LexiconField(); } elseif (array_key_exists('values', $data)) { return new LexiconMultiValueField(); } else { return new MultiText(); } }
/** * Update the optionlist with params * @param $projectId * @param LexOptionListModel $params */ public static function updateList($projectId, $params) { CodeGuard::checkTypeAndThrow($params, 'array'); $project = new LexiconProjectModel($projectId); if (array_key_exists('id', $params) && $params['id'] != '') { $optionlist = new LexOptionListModel($project, $params['id']); } else { $optionlist = new LexOptionListModel($project); } JsonDecoder::decode($optionlist, $params); $optionlist->write(); }
/** * @param array $userIds * @return int Total number of users removed. */ public static function deleteUsers($userIds) { CodeGuard::checkTypeAndThrow($userIds, 'array'); $count = 0; foreach ($userIds as $userId) { CodeGuard::checkTypeAndThrow($userId, 'string'); $userModel = new \Api\Model\UserModel($userId); $userModel->remove(); $count++; } return $count; }
/** * @param array $questionTemplateIds * @return int Total number of questionTemplate questions removed. */ public static function deleteQuestionTemplates($projectId, $questionTemplateIds) { CodeGuard::checkTypeAndThrow($questionTemplateIds, 'array'); $count = 0; $projectModel = new ProjectModel($projectId); foreach ($questionTemplateIds as $questionTemplateId) { CodeGuard::checkTypeAndThrow($questionTemplateId, 'string'); $questionTemplate = new QuestionTemplateModel($projectModel, $questionTemplateId); $questionTemplate->remove(); $count++; } return $count; }
/** * @param SmsModel $smsModel */ public static function queue($smsModel) { CodeGuard::checkTypeAndThrow($smsModel, 'Api\\Library\\Shared\\Communicate\\Sms\\SmsModel'); // Check if the smsModel is valid if (!$smsModel->to || !$smsModel->from || $smsModel->userRef == null || !$smsModel->userRef->asString()) { error_log("Error: Attempting to queue invalid sms"); error_log(" Dump: " . var_export($smsModel, true)); return; } $id = $smsModel->write(); $databaseName = $smsModel->databaseName(); error_log("wrote sms: {$id} to {$databaseName}"); }
function generateCustomField($data) { CodeGuard::checkTypeAndThrow($data, 'array'); if (array_key_exists('type', $data)) { switch ($data['type']) { case LexConfig::MULTIPARAGRAPH: return new LexMultiParagraph(); default: $type = $data['type']; throw new \Exception("Cannot generate unknown custom field type: {$type}"); } } elseif (array_key_exists('value', $data)) { return new LexValue(); } elseif (array_key_exists('values', $data)) { return new LexMultiValue(); } elseif (array_key_exists('paragraphs', $data)) { return new LexMultiParagraph(); } else { return new LexMultiText(); } }
/** * @param UniversalTimestamp $model * @param UTCDateTime $data */ public function decodeUniversalTimestamp(&$model, $data) { CodeGuard::checkTypeAndThrow($data, 'MongoDB\\BSON\\UTCDateTime', CodeGuard::CHECK_NULL_OK); // account for difference between .NET and Linux epoch // (which produces negative milliseconds in UTCDateTime then causes an exception in UniversalTimestamp) if ((int) (string) $data < 0) { $data = new UTCDateTime(0); } parent::decodeUniversalTimestamp($model, $data); }
/** * Reads a MultiText from the XmlNode $sxeNode given by the element 'gloss' * * @param SimpleXMLElement $sxeNode * @param MultiText $multiText * @param ArrayOf $inputSystems * @return MultiText */ public function readMultiTextGloss($sxeNode, $multiText, $inputSystems = null) { CodeGuard::checkTypeAndThrow($multiText, 'Api\\Model\\Languageforge\\Lexicon\\MultiText'); if ($sxeNode->getName() != 'gloss') { throw new \Exception("'" . $sxeNode->getName() . "' is not a gloss"); } $inputSystemTag = (string) $sxeNode['lang']; $multiText->form($inputSystemTag, (string) $sxeNode->text); $this->projectModel->addInputSystem($inputSystemTag); // TODO InputSystems should extend ArrayOf (or Map) and become more useful. CP 2014-10 if (isset($inputSystems)) { // i.e. $inputSystems->ensureFieldHasInputSystem($inputSystemTag); $inputSystems->value($inputSystemTag); } }
/** * List users in the project * @param string $projectId * @return array - the DTO array */ public static function usersDto($projectId) { CodeGuard::checkTypeAndThrow($projectId, 'string'); CodeGuard::checkNotFalseAndThrow($projectId, '$projectId'); $usersDto = ManageUsersDto::encode($projectId); return $usersDto; }
/** * Decodes the mongo array into the ReferenceList $model * @param ReferenceList $model * @param array $data * @throws \Exception */ public function decodeReferenceList($model, $data) { $model->refs = array(); if (array_key_exists('refs', $data)) { // This likely came from an API client, who shouldn't be sending this. return; } $refsArray = $data; foreach ($refsArray as $objectId) { CodeGuard::checkTypeAndThrow($objectId, 'string'); array_push($model->refs, new Id((string) $objectId)); } }
/** * Returns the rights array for the $userId role. * @param string $userId * @return array */ public function getRightsArray($userId) { if (!method_exists($this->rolesClass, 'getRightsArray')) { throw new \Exception('getRightsArray method cannot be called directly from ProjectModel'); } CodeGuard::checkTypeAndThrow($userId, 'string'); if (!key_exists($userId, $this->users)) { $result = array(); } else { $role = $this->users[$userId]->role; $rolesClass = $this->rolesClass; $result = $rolesClass::getRightsArray($role); } return $result; }
/** * Writes the model to the mongo collection * @return string The unique id of the object written * @see MongoMapper::write() */ public function write() { CodeGuard::checkTypeAndThrow($this->id, 'Api\\Model\\Mapper\\Id'); $this->dateModified = new \DateTime(); if (Id::isEmpty($this->id)) { $this->dateCreated = new \DateTime(); } $this->id->id = $this->_mapper->write($this, $this->id->id); return $this->id->id; }
/** * @param ReferenceList $model * @return array */ public function encodeReferenceList($model) { $result = array_map(function ($id) { CodeGuard::checkTypeAndThrow($id, 'Api\\Model\\Shared\\Mapper\\Id'); /** @var Id $id */ return MongoMapper::mongoID($id->asString()); }, $model->refs); return $result; }
/** * @param string $type - this is one of * @param string $content */ public function addContent($type, $content) { CodeGuard::checkTypeAndThrow($content, 'string'); $this->actionContent[$type] = $content; }
/** * @param array $data * @param string $id * @param int $keyType * @param string $rootId * @param string $property * @return string */ protected function update($data, $id, $keyType, $rootId, $property) { CodeGuard::checkTypeAndThrow($rootId, 'string'); CodeGuard::checkTypeAndThrow($id, 'string'); if ($keyType == self::ID_IN_KEY) { if (empty($rootId)) { $mongoid = self::mongoId($id); $result = $this->_collection->update(array('_id' => $mongoid), array('$set' => $data), array('upsert' => true, 'multiple' => false, 'safe' => true)); //$id = isset($result['upserted']) ? $result['upserted'].$id : $id; $id = $mongoid->__toString(); } else { CodeGuard::checkNullAndThrow($id, 'id'); CodeGuard::checkNullAndThrow($property, 'property'); $subKey = $property . '.' . $id; $result = $this->_collection->update(array('_id' => self::mongoId($rootId)), array('$set' => array($subKey => $data)), array('upsert' => false, 'multiple' => false, 'safe' => true)); } } else { CodeGuard::checkNullAndThrow($id, 'id'); $result = $this->_collection->update(array('_id' => self::mongoId($rootId)), array('$set' => array($property . '$' . $id => $data)), array('upsert' => false, 'multiple' => false, 'safe' => true)); } return $id; }
/** * @param string $key * @param ReferenceList $model * @return array */ public function encodeReferenceList($key, $model) { // Note: $key may be used in derived methods $result = array_map(function ($id) { CodeGuard::checkTypeAndThrow($id, 'Api\\Model\\Shared\\Mapper\\Id'); return $id->id; }, $model->refs); return $result; }
public function offsetSet($index, $newval) { CodeGuard::checkTypeAndThrow($index, 'string'); parent::offsetSet($index, $newval); }
/** * Reads a MultiText from the XmlNode $sxeNode given by the element 'gloss' * * @param \SimpleXMLElement $sxeNode * @param LexMultiText $multiText * @param ArrayOf $inputSystems * @throws \Exception */ public function readMultiTextGloss($sxeNode, $multiText, $inputSystems = null) { CodeGuard::checkTypeAndThrow($multiText, 'Api\\Model\\Languageforge\\Lexicon\\LexMultiText'); if ($sxeNode->getName() != 'gloss') { throw new \Exception("'" . $sxeNode->getName() . "' is not a gloss"); } $inputSystemTag = (string) $sxeNode['lang']; $multiText->form($inputSystemTag, (string) $sxeNode->{'text'}); $this->project->addInputSystem($inputSystemTag); if (isset($inputSystems)) { $inputSystems->ensureValueExists($inputSystemTag); } }
/** * @param array $data * @param string $id * @param int $keyType * @param string $rootId * @param string $property * @return string */ protected function update($data, $id, $keyType, $rootId, $property) { CodeGuard::checkTypeAndThrow($rootId, 'string'); CodeGuard::checkTypeAndThrow($id, 'string'); if ($keyType == self::ID_IN_KEY) { if (empty($rootId)) { $mongoid = self::mongoID($id); $filter = array('_id' => $mongoid); $updateCommand = array('$set' => $data); $options = array('upsert' => true); $this->_collection->updateOne($filter, $updateCommand, $options); $id = $mongoid->__toString(); } else { CodeGuard::checkNullAndThrow($id, 'id'); CodeGuard::checkNullAndThrow($property, 'property'); $subKey = $property . '.' . $id; $filter = array('_id' => self::mongoID($rootId)); $updateCommand = array('$set' => array($subKey => $data)); $this->_collection->updateOne($filter, $updateCommand); } } else { CodeGuard::checkNullAndThrow($id, 'id'); $filter = array('_id' => self::mongoID($rootId)); $updateCommand = array('$set' => array($property . '$' . $id => $data)); $this->_collection->updateOne($filter, $updateCommand); } return $id; }