<?php

/**
 * menu actions.
 *
 * @package    contab2
 * @subpackage menu
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class menuActions extends sfActions {

    /**
     *
     * @param sfWebRequest $request
     */
    public function executeRoot(sfWebRequest $request) {

        //Revisar que sea post
        //$this->forward404Unless($request->isMethod('post'));

        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("---END Logging request");

        $records = array();
        $usuario = $request['username'];

        $hoja = false;

        // inicio leer archivo *************************************************
        // archivo txt
        $file = "usuario.txt";
        $handle = fopen($file, "r");

        $fileMenu = "menus.txt";
        $handleMenu = fopen($fileMenu, "r");


        if (!filesize($file) > 0 && !filesize($fileMenu) > 0) {
            echo"File is empty!";
        } else if ($usuario != null) {

            $fcontent = fread($handle, filesize($file));
            $fcontentMenus = fread($handleMenu, filesize($fileMenu));

            $users = explode("\n", $fcontent);
            $usersMenus = explode("\n", $fcontentMenus);

            foreach ($users as $user) {

                $sql = explode(";", $user);

                $id_credencial = $sql[0];
                $username = $sql[1];
                $activo = $sql[2];

                if ($username == $usuario && $activo == 'true') {

                    foreach ($usersMenus as $userMenu) {

                        $sqlMenu = explode(";", $userMenu);

                        if ((int) $sqlMenu[0] == (int) $id_credencial && (int) $sqlMenu[3] == 0) {

                            $records[] = array('id_menu' => (int) $sqlMenu[0], 'rotulo' => $sqlMenu[1], 'hoja' => $hoja);
                            break;
                        }
                    }
                }
            }
        }

        $this->list_data = $records;

        $this->logMessage('record_count = ' . count($records));
        $this->logMessage('total_record_count = ' . count($records));
        $this->totalRows = count($records);
        $this->startRow = $request['_startRow'];
        $this->endRow = count($records);
    }

    /**
     *
     * @param sfWebRequest $request
     */
    public function executeList(sfWebRequest $request) {

        //Revisar que sea post
        //$this->forward404Unless($request->isMethod('post'));

        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("---END Logging request");

        $records = array();
        $id_credencial = $request['id_menu'];
        $usuario = $request['username'];


        // inicio leer archivo *************************************************
        // archivo txt
        $file = "usuario.txt";
        $handle = fopen($file, "r");

        $fileMenu = "menus.txt";
        $handleMenu = fopen($fileMenu, "r");


        if (!filesize($file) > 0 && !filesize($fileMenu) > 0) {
            echo"File is empty!";
        } else if ($usuario != null) {

            $fcontent = fread($handle, filesize($file));
            $fcontentMenus = fread($handleMenu, filesize($fileMenu));

            $users = explode("\n", $fcontent);
            $usersMenus = explode("\n", $fcontentMenus);

            foreach ($usersMenus as $userMenu) {

                $sqlMenu = explode(";", $userMenu);

                if ((int) $sqlMenu[3] == (int) $id_credencial) {

                    //echo $sqlMenu[3] . '<br>';

                    foreach ($users as $user) {

                        $sql = explode(";", $user);

                        $id_menu = $sql[0];
                        $username = $sql[1];
                        $activo = $sql[2];

                        if ($username == $usuario && $id_menu == $sqlMenu[0] && $activo == 'true') {
                            //echo $id_credencial . '-' . $username . '<br>';
                            //si es hoja true
                            if ($sqlMenu[2] == '2') {
                                $hoja = true;
                            } else {
                                $hoja = false;
                            }

                            $records[] = array('id_menu' => (int) $sqlMenu[0], 'rotulo' => $sqlMenu[1], 'hoja' => $hoja);
                            break;
                        }
                    }
                }
            }
        }
        /*

          // inicio leer archivo *************************************************
          // archivo txt
          $file = "/home/lavila/NetBeansProjects/contab/apps/api/modules/menu/actions/menus.txt";
          $handle = fopen($file, "r");

          if (!filesize($file) > 0) {
          echo"File is empty!";
          die();
          } else {
          $fcontent = fread($handle, filesize($file));
          $users = explode("\n", $fcontent);

          foreach ($users as $user) {

          $sql = explode(";", $user);

          if ((int) $sql[3] == (int) $id_credencial) {

          //si es hoja true
          if ($sql[2] == '2') {
          $hoja = true;
          } else {
          $hoja = false;
          }

          $records[] = array('id_menu' => (int) $sql[0], 'rotulo' => $sql[1], 'hoja' => $hoja);
          }

          // fin leer archivo ****************************************************
          }
          }
         */

        $this->list_data = $records;

        $this->logMessage('record_count = ' . count($records));
        $this->logMessage('total_record_count = ' . count($records));
        $this->totalRows = count($records);
        $this->startRow = $request['_startRow'];
        $this->endRow = count($records);
    }

}
