/** * 保存数据 * @param array $attr * @param object $o * @return array */ static function dataSave($attr, $o = false) { $return = array('status' => false, 'message' => null); $columnMap = null; if (!$o) { // 插入 $o = new self(); if ($o->create($attr) == false) { $return['message'] = join($o->getMessages(), '<br>'); } } else { // 更新 if ($o->update($attr) == false) { $return['message'] = join($o->getMessages(), '<br>'); } } if (is_null($return['message'])) { $return['status'] = true; } return $return; }
public static function login($login, $senha) { $model = new self(); // Estancia a conexão com o banco de dados $db = Zend_Db_Table::getDefaultAdapter(); // Estancia o Zend_Auth para indica em qual tabela e quais campos fazer a verificação $adapter = new Zend_Auth_Adapter_DbTable($db); $adapter->setTableName($model->name)->setIdentityColumn('email')->setCredentialColumn('password')->setCredentialTreatment('SHA1(CONCAT(?,salt))'); // Atribuindo campo extra para a verificação $select = $adapter->getDbSelect(); $select->where('acesso = 1'); $adapter->setIdentity($login); $adapter->setCredential($senha); $auth = Zend_Auth::getInstance(); $result = $auth->authenticate($adapter); if ($result->isValid()) { // Gravando dados na sessão $contents = $adapter->getResultRowObject(null, 'password'); $contents->childrens_ids = array(); $db->setFetchMode(Zend_Db::FETCH_OBJ); $result = $db->fetchRow('SELECT role FROM ' . $model->perfilName . ' WHERE id = ?', $contents->id_perfil); $userchildrens = $db->fetchCol('SELECT id FROM ' . $model->name . ' WHERE parent_id = ?', $contents->id); if ($userchildrens) { $contents->childrens_ids = $userchildrens; foreach ($userchildrens as $children) { $childrens = $db->fetchCol('SELECT id FROM ' . $model->name . ' WHERE parent_id = ?', $children); if ($childrens) { $contents->childrens_ids = array_merge($contents->childrens_ids, $childrens); } } } $contents = (object) array_merge((array) $contents, (array) $result); $auth->getStorage()->write($contents); return true; } else { return $model->getMessages($result); } }
public static function createTrace($user, $status, $operation, $msg, $date, $ip) { try { $trace = new self(); $trace->idUser = !$user ? 0 : $user; $trace->result = $status; $trace->operation = $operation; $trace->description = $msg; $trace->location = 'Empty'; $trace->date = $date; $trace->ip = ip2long($ip); if (!$trace->save()) { $message = 'Error while saving trace' . PHP_EOL; foreach ($trace->getMessages() as $msg) { $message .= " - {$msg}" . PHP_EOL; } self::saveInLog($user, $status, $operation, $msg, $date, $ip, $msg); } } catch (Exception $e) { $msg = "Exception while saving trace" . PHP_EOL; $msg .= $e; self::saveInLog($user, $status, $operation, $msg, $date, $ip, $msg); } }
/** * Add Vote up or down for the post * * @param integer $objectId this is id of posts * @param string $object there are some object comments, posts, postsReplay see constant the above * @param string $way positive or negative * @return boolen|array */ public static function vote($objectId, $object, $way) { $auth = \Phalcon\DI::getDefault()->getAuth(); $conditions = ['usersId = :usersId: AND objectId = :objectId: AND object = :object:', 'bind' => ['usersId' => $auth->getAuth()['id'], 'objectId' => $objectId, 'object' => $object]]; if (!($vote = Vote::findFirst($conditions))) { $vote = new self(); } // When the posts have vote then user is existing if ($vote->getUsersId()) { switch ($object) { case Vote::OBJECT_POSTS: return ['type' => 'error', 'content' => t('You have already voted this post')]; break; default: return ['type' => 'error', 'content' => t('You have already voted this post reply')]; break; } } if ($way == 'positive') { $vote->voteUp($objectId, $object); } if ($way == 'negative') { $vote->voteDown($objectId, $object); } if (!$vote->save()) { foreach ($vote->getMessages() as $m) { error_log($m->getMessage()); //return ['type' => 'error','content' => $m->getMessage()]; } return false; } return true; }
public static function fail($obj, array $rules) { $val = new self($obj, $rules); return $val->exec() ? false : $val->getMessages(); }
public static function addMerchant($name, $password, $stores, $privs) { $type = self::MERCHANT; $user = new self(); $transaction = \Bootstrap::getApp()->getDi()->getTransactions()->get(); $user->setTransaction($transaction); $user->name = $name; $user->password = \Plugins\Common::generatePassword($password); $user->type = $type; if (!$user->save()) { foreach ($user->getMessages() as $message) { $transaction->rollback($message->getMessage()); } return false; } $result = $user->addMerchantStore($stores); $role = new \Model\Admin\Role(); $role->setTransaction($transaction); $role->name = $name; if (!$role->save()) { foreach ($user->getMessages() as $message) { $transaction->rollback($message->getMessage()); } return false; } $result = $role->setRolePermission($privs); if (!$result['result']) { $transaction->rollback(); return false; } $user->role_id = $role->id; if (!$user->save()) { foreach ($user->getMessages() as $message) { $transaction->rollback($message->getMessage()); } return false; } $transaction->commit(); return true; }