Browse Source

Nuevo skeleton para el nuevo webAppMulti con multicuentas

Gabriel Badano 4 năm trước cách đây
mục cha
commit
4f96ac19ea

+ 71 - 45
db.sql

@@ -1,58 +1,84 @@
--- MySQL dump 10.13  Distrib 5.7.28, for Linux (x86_64)
+-- phpMyAdmin SQL Dump
+-- version 4.9.2
+-- https://www.phpmyadmin.net/
 --
--- Host: localhost    Database: webApp
--- ------------------------------------------------------
--- Server version	5.7.28
+-- Servidor: 127.0.0.1:3306
+-- Tiempo de generación: 28-05-2020 a las 09:37:55
+-- Versión del servidor: 10.4.10-MariaDB
+-- Versión de PHP: 7.3.12
 
-/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
-/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
-/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
-/*!40101 SET NAMES utf8 */;
-/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
-/*!40103 SET TIME_ZONE='+00:00' */;
-/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
-/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
-/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
+SET AUTOCOMMIT = 0;
+START TRANSACTION;
+SET time_zone = "+00:00";
 
 --
--- Table structure for table `usuarios`
+-- Base de datos: `webapp`
 --
 
-DROP TABLE IF EXISTS `usuarios`;
-/*!40101 SET @saved_cs_client     = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `usuarios` (
-  `id` bigint(20) NOT NULL AUTO_INCREMENT,
-  `usuario` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `pass` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `nombre` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `role` varchar(1) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `token` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
-  `ultimoLogin` datetime DEFAULT NULL,
-  PRIMARY KEY (`id`),
-  UNIQUE KEY `usuario` (`usuario`),
-  KEY `token` (`token`)
+-- --------------------------------------------------------
+
+--
+-- Estructura de tabla para la tabla `cuentas`
+--
+
+DROP TABLE IF EXISTS `cuentas`;
+CREATE TABLE IF NOT EXISTS `cuentas` (
+                                         `id` bigint(20) NOT NULL AUTO_INCREMENT,
+                                         `cuenta` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                         `dir` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                         `active` tinyint(1) DEFAULT 1,
+                                         PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-/*!40101 SET character_set_client = @saved_cs_client */;
 
 --
--- Dumping data for table `usuarios`
+-- Volcado de datos para la tabla `cuentas`
+--
+
+INSERT INTO `cuentas` (`id`, `cuenta`, `dir`, `active`) VALUES
+(1, 'Cuenta 1', 'cuenta1', 1),
+(2, 'Cuenta 2', 'cuenta2', 1);
+
+-- --------------------------------------------------------
+
+--
+-- Estructura de tabla para la tabla `usuarios`
 --
 
-LOCK TABLES `usuarios` WRITE;
-/*!40000 ALTER TABLE `usuarios` DISABLE KEYS */;
-INSERT INTO `usuarios` VALUES (1,'admin','c8837b23ff8aaa8a2dde915473ce0991','Administrador','admin@empresa.com','A','',NULL);
-/*!40000 ALTER TABLE `usuarios` ENABLE KEYS */;
-UNLOCK TABLES;
+DROP TABLE IF EXISTS `usuarios`;
+CREATE TABLE IF NOT EXISTS `usuarios` (
+                                          `id` bigint(20) NOT NULL AUTO_INCREMENT,
+                                          `usuario` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+                                          `pass` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                          `nombre` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                          `email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                          `role` varchar(1) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                          `token` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+                                          `ultimoLogin` datetime DEFAULT NULL,
+                                          PRIMARY KEY (`id`),
+                                          UNIQUE KEY `usuario` (`usuario`),
+                                          KEY `token` (`token`)
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+
+--
+-- Volcado de datos para la tabla `usuarios`
+--
 
-/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
-/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
-/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
-/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
-/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
-/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
-/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+INSERT INTO `usuarios` (`id`, `usuario`, `pass`, `nombre`, `email`, `role`, `token`, `ultimoLogin`) VALUES
+(1, 'admin', 'c8837b23ff8aaa8a2dde915473ce0991', 'Administrador', 'admin@empresa.com', 'A', '', NULL);
+
+-- --------------------------------------------------------
+
+--
+-- Estructura de tabla para la tabla `usuarioscuentas`
+--
 
--- Dump completed on 2019-10-23 22:32:31
+DROP TABLE IF EXISTS `usuarioscuentas`;
+CREATE TABLE IF NOT EXISTS `usuarioscuentas` (
+                                                 `usuario_id` bigint(20) NOT NULL,
+                                                 `cuenta_id` bigint(20) NOT NULL,
+                                                 `role` char(1) COLLATE utf8mb4_unicode_ci NOT NULL,
+                                                 PRIMARY KEY (`usuario_id`,`cuenta_id`),
+                                                 UNIQUE KEY `cuenta_id` (`cuenta_id`,`usuario_id`) USING BTREE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+COMMIT;

+ 7 - 0
protected/business/business1/cuentas/cuenta1/config/config.php

@@ -0,0 +1,7 @@
+<?php
+
+return array(
+
+    'ACCOUNT_VAR' => 'hello world!',
+
+);

+ 30 - 0
protected/business/business1/cuentas/cuenta1/controllers/ejemplo.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace cuenta\controllers;
+
+use app\models\GeneralModel;
+use business\models\BusinessModel;
+use cuenta\models\CuentaModel;
+use oxusmedia\webAppMulti\controller;
+
+class ejemplo extends controller
+{
+    public function index()
+    {
+        $this->webApp()->requireLoginRedir();
+
+        $this->titulo = 'Página de cuenta';
+
+        // invocar model de carpeta protected/models
+        new GeneralModel();
+
+        // invocar model de carpeta models del business
+        new BusinessModel();
+
+        // invocar model de carpeta models de la account
+        new CuentaModel();
+
+        $this->render('index');
+    }
+
+}

+ 8 - 0
protected/business/business1/cuentas/cuenta1/models/CuentaModel.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace cuenta\models;
+
+class CuentaModel
+{
+
+}

+ 4 - 0
protected/business/business1/cuentas/cuenta1/views/_includes/menu.php

@@ -0,0 +1,4 @@
+
+<li>
+    <a href="<?php echo $this->webApp()->getSite();?>c/ejemplo"><i class="fa fa-bar-chart fa-fw"></i> <span>Página de Cuenta</span></a>
+</li>

+ 10 - 0
protected/business/business1/cuentas/cuenta1/views/ejemplo/index.php

@@ -0,0 +1,10 @@
+
+<?php $this->renderInclude("header");?>
+
+    <p>Esta es una página de la cuenta de un negocio!</p>
+
+    <pre><?php print_r($this->webApp()->getCuenta());?></pre>
+
+    <pre><?php print_r($this->webApp()->getCuenta()->getConfig());?></pre>
+
+<?php $this->renderInclude("footer");?>

+ 7 - 0
protected/business/business1/cuentas/cuenta2/config/config.php

@@ -0,0 +1,7 @@
+<?php
+
+return array(
+
+    'ACCOUNT_VAR' => 'hello world 2!',
+
+);

+ 30 - 0
protected/business/business1/cuentas/cuenta2/controllers/ejemplo.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace cuenta\controllers;
+
+use app\models\GeneralModel;
+use business\models\BusinessModel;
+use cuenta\models\CuentaModel;
+use oxusmedia\webAppMulti\controller;
+
+class ejemplo extends controller
+{
+    public function index()
+    {
+        $this->webApp()->requireLoginRedir();
+
+        $this->titulo = 'Página de cuenta';
+
+        // invocar model de carpeta protected/models
+        new GeneralModel();
+
+        // invocar model de carpeta models del business
+        new BusinessModel();
+
+        // invocar model de carpeta models de la account
+        new CuentaModel();
+
+        $this->render('index');
+    }
+
+}

+ 8 - 0
protected/business/business1/cuentas/cuenta2/models/CuentaModel.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace cuenta\models;
+
+class CuentaModel
+{
+
+}

+ 4 - 0
protected/business/business1/cuentas/cuenta2/views/_includes/menu.php

@@ -0,0 +1,4 @@
+
+<li>
+    <a href="<?php echo $this->webApp()->getSite();?>c/ejemplo"><i class="fa fa-bar-chart fa-fw"></i> <span>Página de Cuenta</span></a>
+</li>

+ 10 - 0
protected/business/business1/cuentas/cuenta2/views/ejemplo/index.php

@@ -0,0 +1,10 @@
+
+<?php $this->renderInclude("header");?>
+
+    <p>Esta es una página de la cuenta de un negocio!</p>
+
+    <pre><?php print_r($this->webApp()->getCuenta());?></pre>
+
+    <pre><?php print_r($this->webApp()->getCuenta()->getConfig());?></pre>
+
+<?php $this->renderInclude("footer");?>

+ 8 - 2
protected/business/business1/views/_includes/menu.php

@@ -1,4 +1,10 @@
-
 <li>
-    <a href="<?php echo $this->webApp()->getSite();?>_/ejemplo"><i class="fa fa-paper-plane fa-fw"></i> <span>Página de Negocio</span></a>
+    <a href="<?php echo $this->webApp()->getSite(); ?>b/ejemplo"><i class="fa fa-paper-plane fa-fw"></i> <span>Página de Negocio</span></a>
 </li>
+
+<?php
+
+if ($this->webApp()->getCuenta())
+    $this->renderCuentaInclude('menu');
+
+?>

+ 2 - 2
protected/business/business1/views/ejemplo/index.php

@@ -1,8 +1,8 @@
 
 <?php $this->renderInclude("header");?>
 
-<p>Esta es una página de ejemplo de un negocio!</p>
+    <p>Esta es una página de ejemplo de un negocio!</p>
 
-<pre><?php print_r(\app\models\CuentaSel::getSelected());?></pre>
+    <pre><?php print_r($this->webApp()->app()->getBusiness());?></pre>
 
 <?php $this->renderInclude("footer");?>

+ 1 - 1
protected/controllers/cuenta.php

@@ -10,7 +10,7 @@ class cuenta extends controller
 
         if (isset($params['id'])) {
 
-            app\models\CuentaSel::select($params['id']);
+            $this->webApp()->setCuenta($params['id']);
 
             $this->redirect($_SERVER["HTTP_REFERER"]);
 

+ 6 - 3
protected/controllers/usuario.php

@@ -316,8 +316,9 @@ class usuario extends controller
 
             } else {
 
-                $cuentas = $this->db()->query('SELECT cuentas.*, usuarioscuentas.role FROM cuentas LEFT JOIN usuarioscuentas ON usuarioscuentas.cuenta_id = cuentas.id WHERE usuarioscuentas.usuario_id = :usuario_id', array(
-                    'usuario_id' => $usuario->id
+                $cuentas = $this->db()->query('SELECT cuentas.*, usuarioscuentas.role FROM cuentas LEFT JOIN usuarioscuentas ON usuarioscuentas.cuenta_id = cuentas.id WHERE usuarioscuentas.usuario_id = :usuario_id AND cuentas.active = :active', array(
+                    'usuario_id' => $usuario->id,
+                    'active'     => 1
                 ));
 
                 $form->setAtributes($usuario);
@@ -462,7 +463,9 @@ class usuario extends controller
 
     private function getCuentasRoleSelect(&$col2)
     {
-        $cuentas = $this->db()->query('SELECT * FROM cuentas ORDER BY cuenta');
+        $cuentas = $this->db()->query('SELECT * FROM cuentas WHERE active = :active ORDER BY cuenta', array(
+            'active' => 1
+        ));
 
         $col1 = array();
         $col2 = array();

+ 0 - 34
protected/models/Cuenta.php

@@ -1,34 +0,0 @@
-<?php
-
-namespace app\models;
-
-use oxusmedia\webAppMulti\webApp;
-
-class Cuenta
-{
-    public $id, $cuenta, $role;
-
-    public function __construct($id = null)
-    {
-        if ($id != null) {
-
-            $cuenta = webApp::app()->db()->queryRow('SELECT * FROM cuentas WHERE id = :id', array(
-                'id' => $id
-            ));
-
-            if ($cuenta) {
-
-                $this->id     = $cuenta->id;
-                $this->cuenta = $cuenta->nombre;
-                $this->role   = $cuenta->role;
-
-                return true;
-            }
-
-        }
-
-        return false;
-
-    }
-
-}

+ 0 - 60
protected/models/CuentaSel.php

@@ -1,60 +0,0 @@
-<?php
-
-namespace app\models;
-
-use oxusmedia\webAppMulti\webApp;
-
-class CuentaSel
-{
-    static public function select($id)
-    {
-        $cuenta = webApp::app()->db()->queryRow('SELECT cuentas.*, usuarioscuentas.role FROM usuarioscuentas LEFT JOIN cuentas ON cuentas.id = usuarioscuentas.cuenta_id WHERE usuarioscuentas.usuario_id = :usuario_id AND usuarioscuentas.cuenta_id = :cuenta_id', array(
-            'usuario_id' => webApp::app()->getUsuarioId(),
-            'cuenta_id'  => $id,
-        ));
-
-        if ($cuenta and $cuenta->role != CuentaRole::ROLE_NONE) {
-
-            $_SESSION['cuenta_sel_id'] = $cuenta->id;
-
-            return true;
-
-        }
-
-        return false;
-
-    }
-
-    static public function getSelected()
-    {
-        static $cuentaSel;
-
-        if ($cuentaSel)
-            return $cuentaSel;
-
-        if (isset($_SESSION['cuenta_sel_id'])) {
-
-            $cuenta = webApp::app()->db()->queryRow('SELECT cuentas.*, usuarioscuentas.role FROM usuarioscuentas LEFT JOIN cuentas ON cuentas.id = usuarioscuentas.cuenta_id WHERE usuarioscuentas.usuario_id = :usuario_id AND usuarioscuentas.cuenta_id = :cuenta_id', array(
-                'usuario_id' => webApp::app()->getUsuarioId(),
-                'cuenta_id'  => $_SESSION['cuenta_sel_id']
-            ));
-
-            if ($cuenta) {
-
-                $cuentaSel = new Cuenta();
-
-                $cuentaSel->id     = $_SESSION['cuenta_sel_id'];
-                $cuentaSel->cuenta = $cuenta->cuenta;
-                $cuentaSel->role   = webApp::app()->getRole() == webApp::ROLE_ADMIN ? webApp::ROLE_ADMIN : $cuenta->role;
-
-                return $cuentaSel;
-
-            }
-
-        }
-
-        return false;
-
-    }
-
-}

+ 15 - 4
protected/models/Usuario.php

@@ -2,6 +2,7 @@
 
 namespace app\models;
 
+use oxusmedia\webAppMulti\Cuenta;
 use oxusmedia\webAppMulti\webApp;
 
 class Usuario
@@ -35,15 +36,25 @@ class Usuario
     {
         $db = webApp::app()->db();
 
-        $cuentas = $db->query('SELECT cuentas.*, usuarioscuentas.role FROM usuarioscuentas LEFT JOIN cuentas ON usuarioscuentas.cuenta_id = cuentas.id WHERE usuarioscuentas.usuario_id = :usuario_id ORDER BY cuenta', array(
-            'usuario_id' => $this->id
-        ));
+        if ($this->role == webApp::ROLE_ADMIN)
+
+            $cuentas = $db->query('SELECT cuentas.* FROM cuentas WHERE active = :active ORDER BY cuenta', array(
+                'usuario_id' => $this->id,
+                'active'     => 1
+            ));
+
+        else
+
+            $cuentas = $db->query('SELECT cuentas.*, usuarioscuentas.role FROM usuarioscuentas LEFT JOIN cuentas ON usuarioscuentas.cuenta_id = cuentas.id WHERE usuarioscuentas.usuario_id = :usuario_id AND active = :active ORDER BY cuenta', array(
+                'usuario_id' => $this->id,
+                'active'     => 1
+            ));
 
         $ctas = array();
 
         while ($cuenta = $db->getRow($cuentas)) {
 
-            $cta = new Cuenta();
+            $cta = new Cuenta($cuenta);
 
             $cta->id     = $cuenta->id;
             $cta->cuenta = $cuenta->cuenta;

+ 9 - 4
protected/views/_includes/menu.php

@@ -1,3 +1,8 @@
+<?php
+
+$cuenta = $this->webApp()->getCuenta();
+
+?>
 
 <!-- Navigation -->
 <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
@@ -19,17 +24,17 @@
     <ul class="nav navbar-right navbar-top-links">
         <li class="dropdown">
             <a class="dropdown-toggle" data-toggle="dropdown" href="#">
-                <i class="fa fa-line-chart fa-fw"></i> <?php echo !app\models\CuentaSel::getSelected()->id ? 'Seleccionar cuenta...' : app\models\CuentaSel::getSelected()->cuenta;?> <b class="caret"></b>
+                <i class="fa fa-line-chart fa-fw"></i> <?php echo !$cuenta ? 'Seleccionar cuenta...' : $cuenta->cuenta;?> <b class="caret"></b>
             </a>
             <ul class="dropdown-menu dropdown-cuenta">
-                <?php $usu = new app\models\Usuario(); foreach ($usu->getCuentas() as $cuenta) { ?>
-                    <li><a href="cuenta/select?id=<?php echo $cuenta->id;?>"><?php echo $cuenta->cuenta;?></a></li>
+                <?php $usu = new app\models\Usuario(); foreach ($usu->getCuentas() as $c) { ?>
+                    <li><a href="cuenta/select?id=<?php echo $c->id;?>"><?php echo $c->cuenta;?></a></li>
                 <?php } ?>
             </ul>
         </li>
         <li class="dropdown">
             <a class="dropdown-toggle" data-toggle="dropdown" href="#">
-                <i class="fa fa-user fa-fw"></i> <?php echo $this->webApp()->getUsuario();?> <b class="caret"></b>
+                <i class="fa fa-user fa-fw"></i> <?php echo $this->webApp()->getUsuarioNombre();?> <b class="caret"></b>
             </a>
             <ul class="dropdown-menu dropdown-user">
                 <li class="divider"></li>