linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.scale.php

Documentation is available at class.scale.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage scale
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Scale Management
  10.  */
  11. class scale {
  12.   /* @param
  13.    * */
  14.   var $TDB_SCALE = T_SCALE; // nom de la table.
  15.   var $ID;
  16.   var $DENOMINATION;
  17.   var $ROOT;
  18.   var $SURFACE;
  19.   var $INHABITANTSNUM;
  20.   var $COMMENT;
  21.   var $DEPENDENCIES;
  22.   var $DATE_CREA;
  23.   var $STATUT;
  24.   var $LAST_MODIFY;
  25.  
  26.   var $DEPTH_TABLE = array (); // tableau contenant les profondeurs d'échelles
  27.   var $SON_TABLE = array (); // tableau contenant les fils d'échelles
  28.   var $ALL_REC = array (); // tableau contenant les fils d'échelles
  29.   protected $dispatcher = null;
  30.  
  31.   public function __construct()
  32.   {
  33.     $this->dispatcher = $GLOBALS['dispatcher'];
  34.   }
  35.  
  36.   public function __call($method, $arguments)
  37.   {
  38.     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'scale.extensible_function', array(
  39.       'method'    => $method,
  40.       'arguments' => $arguments
  41.     )));
  42.     if (!$event->isProcessed())
  43.     {
  44.       throw new Exception(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  45.     }
  46.  
  47.     return $event->getReturnValue();
  48.   }
  49.  
  50.   /**
  51.    * scale::CheckDataIntegrity()
  52.    * Vérification des données d'une echelle
  53.    *
  54.    * @access public
  55.    * @param array $table_scale : contient les composants d'une echelle
  56.    * @return boolean si vrai renvoie true sinon message d'erreurs (string)
  57.    */
  58.   function CheckDataIntegrity($table_scale)
  59.   {
  60.       // Filter data event + return value
  61.       $r = $this->dispatcher->filter(new sfEvent($this, 'scale.before_datacheck', array('data' => $table_scale)), $table_scale);
  62.       $table_scale = $r->getReturnValue();
  63.       
  64.     $notdenomination = _t('scale','object_notdenomination');
  65.     $notnumsurface = _t('scale','object_notnumsurface');
  66.     $notnuminhabitants = _t('scale','object_notnuminhabitants');
  67.     $notroot = _t('scale','object_notroot');
  68.  
  69.     if (strlen($table_scale[0]) < 2) return $notdenomination;
  70.     if (trim($table_scale[1]) != '' && !is_numeric($table_scale[1])) return $notnumsurface;
  71.     if (trim($table_scale[2]) != '' && !is_numeric($table_scale[2])) return $notnuminhabitants;
  72.     if (!is_numeric($table_scale[3]) || $table_scale[3] == -1) return $notroot;
  73.     
  74.     // Notify the beginning of the current method
  75.     $this->dispatcher->notify(new sfEvent($this, 'scale.after_datacheck', array('data' => $table_scale)));
  76.     
  77.     return true;
  78.   }
  79.  
  80.   /**
  81.    * scale::getMaxDepth()
  82.    * obtention de la profondeur max d'une échelle ( par rapport a ses filles
  83.    *
  84.    * @access public
  85.    * @param  $id : identifiant de l'échelle courante
  86.    * @param  $sql_object 
  87.    * @return array $return_table contennat profondeur et échelle d'exclusion
  88.    */
  89.   function getMaxDepth($id, $sql_object)
  90.   {
  91.       // Notify the beginning of the current method
  92.       $this->dispatcher->notify(new sfEvent($this, 'scale.get_max_depth', array('id' => $id)));
  93.       
  94.     $this->ID = $id;
  95.  
  96.     $q = "SELECT scale_dependencies FROM " . $this->TDB_SCALE . " WHERE scale_id='" . $this->ID . "';";
  97.     $result = $sql_object->DBSelect($q);
  98.     $this->DEPENDENCIES = $result[0]['scale_dependencies'];
  99.     array_push($this->DEPTH_TABLE, $this->DEPENDENCIES);
  100.     array_push($this->SON_TABLE, $this->ID);
  101.  
  102.     $this->getAllRecord($sql_object);
  103.     $this->getScaleDepthAndSon($this->ALL_REC, $this->ID);
  104.     rsort($this->DEPTH_TABLE);
  105.  
  106.     $return_table["depth"] = MAX_SCALE_LEVEL - ($this->DEPTH_TABLE[0] - ($this->DEPTH_TABLE[(count($this->DEPTH_TABLE)-1)])) - 1;
  107.     $return_table["exclusion"] = $this->SON_TABLE;
  108.  
  109.     return $return_table;
  110.   }
  111.  
  112.   /**
  113.    * scale::getScaleDepthAndSon()
  114.    * obtention des fils et profondeur ( récursif ) par rapport à une échelle données
  115.    * stockage dans des tableaux respectifs.
  116.    *
  117.    * @param  $table Ensemble des échelles
  118.    * @param  $id échelle courante
  119.    * @return void 
  120.    */
  121.   function getScaleDepthAndSon($table, $id)
  122.   {
  123.       // Notify the beginning of the current method
  124.       $this->dispatcher->notify(new sfEvent($this, 'scale.get_scale_depth_and_son', array('data' => $table, 'id' => $id)));
  125.       
  126.     for($i = 0; $i < count($table); $i++) {
  127.       $current_dep = $table[$i]['scale_dependencies'];
  128.       $current_root = $table[$i]['scale_root'];
  129.       $current_id = $table[$i]['scale_id'];
  130.  
  131.       if ($id == $current_root) {
  132.         if (!in_array($current_dep, $this->DEPTH_TABLE)) array_push($this->DEPTH_TABLE, $current_dep);
  133.         if (!in_array($current_id, $this->SON_TABLE)) array_push($this->SON_TABLE, $current_id);
  134.         $this->getScaleDepthAndSon($table, $current_id);
  135.       }
  136.     }
  137.   }
  138.  
  139.   /**
  140.    * scale::getAllRecord()
  141.    * Obtention de toutes les échelles.
  142.    *
  143.    * @param  $sql_object 
  144.    * @return void 
  145.    */
  146.   function getAllRecord($sql_object)
  147.   {
  148.       
  149.       // Notify the beginning of the current method
  150.       $this->dispatcher->notify(new sfEvent($this, 'scale.get_all_record'));
  151.       
  152.     $q = "SELECT scale_dependencies, scale_id, scale_root FROM " . $this->TDB_SCALE . ";";
  153.     $this->ALL_REC = $sql_object->DBSelect($q);
  154.   }
  155.  
  156.   /**
  157.    * scale::AddScale()
  158.    * Ajout d'une echelle
  159.    *
  160.    * @access public
  161.    * @param array $table_scale contient les composants d'une echelle
  162.    * @param object $sql_object 
  163.    * @return integer $last_id
  164.    */
  165.   function AddScale($table_scale, $sql_object)
  166.   {
  167.       
  168.       // Filter data event + return value
  169.       $r = $this->dispatcher->filter(new sfEvent($this, 'scale.before_add', array('data' => $table_scale)), $table_scale);
  170.       $table_scale = $r->getReturnValue();
  171.       
  172.     $table_scale=$sql_object->DBescape($table_scale);
  173.  
  174.     if (strlen($table_scale[0]) > 1) {
  175.       $this->DENOMINATION = strip_input(trim($table_scale[0]));
  176.     } else return $notdenomination;
  177.     if (trim($table_scale[1]) == '') {
  178.       $this->SURFACE = 0;
  179.     } else {
  180.       $this->SURFACE = trim($table_scale[1]);
  181.     }
  182.     if (trim($table_scale[2]) == '') {
  183.       $this->INHABITANTSNUM = 0;
  184.     } else {
  185.       $this->INHABITANTSNUM = trim($table_scale[2]);
  186.     }
  187.     if (is_numeric($table_scale[3])) {
  188.       $this->ROOT = $table_scale[3];
  189.     }
  190.  
  191.     $this->COMMENT = strip_input(trim($table_scale[4], true));
  192.  
  193.     if ($table_scale[5] != '') {
  194.       strtoupper($table_scale[5]);
  195.       switch ($table_scale[5]) {
  196.         case 'P':
  197.           $this->STATUT = $table_scale[5];
  198.           break;
  199.         case 'D':
  200.           $this->STATUT = $table_scale[5];
  201.           break;
  202.         default:
  203.           $this->STATUT = 'P';
  204.           break;
  205.       }
  206.     } else $this->STATUT = 'P';
  207.     // On Determine le rang de l'échelle
  208.     $q = "SELECT scale_dependencies FROM " . $this->TDB_SCALE . " WHERE scale_id=" . $this->ROOT . ";";
  209.     $result = $sql_object->DBSelect($q);
  210.     $this->DEPENDENCIES = $result[0]['scale_dependencies'] + 1;
  211.  
  212.     $q = "INSERT INTO " . $this->TDB_SCALE . " (scale_denomination, scale_surface, scale_inhabitantsnumber, scale_root, scale_comment, " . "scale_dependencies, scale_statut, scale_date_crea) " . "VALUES('" . $this->DENOMINATION . "', " . $this->SURFACE . ", " . $this->INHABITANTSNUM . ", " . $this->ROOT . ", '" . $this->COMMENT . "', " . $this->DEPENDENCIES . ", '" . $this->STATUT . "', NOW());";
  213.     $last_id = $sql_object->DBInsert ($q, 1);
  214.     
  215.     // Notify the end of the current method
  216.     $this->dispatcher->notify(new sfEvent($this, 'scale.after_add', array('data' => $table_scale, 'id' => $last_id)));
  217.     
  218.     return $last_id;
  219.   }
  220.  
  221.   /**
  222.    * scale::ModifyScale()
  223.    * modification d'une echelle
  224.    *
  225.    * @access public
  226.    * @param int $ID identifiant de l'echelle
  227.    * @param object $sql_object 
  228.    * @param array $table_scale contient les composants d'une echelle
  229.    * @return bool $result
  230.    */
  231.   function ModifyScale($ID, $table_scale, $sql_object)
  232.   {
  233.       // Filter data event + return value
  234.       $r = $this->dispatcher->filter(new sfEvent($this, 'scale.before_modify', array('data' => $table_scale, 'id' => $ID)), $table_scale);
  235.       $table_scale = $r->getReturnValue();
  236.       
  237.     $table_scale=$sql_object->DBescape($table_scale);
  238.  
  239.     if (is_numeric($ID)) {
  240.       $this->ID = $ID;
  241.     }
  242.     if ($table_scale[0] != '') {
  243.       $this->DENOMINATION = strip_input(trim($table_scale[0]));
  244.     }
  245.     if (trim($table_scale[1]) == '') {
  246.       $this->SURFACE = 0;
  247.     } else {
  248.       $this->SURFACE = trim($table_scale[1]);
  249.     }
  250.     if (trim($table_scale[2]) == '') {
  251.       $this->INHABITANTSNUM = 0;
  252.     } else {
  253.       $this->INHABITANTSNUM = trim($table_scale[2]);
  254.     }
  255.     if (is_numeric($table_scale[3])) {
  256.       $this->ROOT = $table_scale[3];
  257.     }
  258.  
  259.     $this->COMMENT = strip_input(trim($table_scale[4], true));
  260.  
  261.     if ($table_scale[5] != '') {
  262.       strtoupper($table_scale[5]);
  263.       switch ($table_scale[5]) {
  264.         case 'P':
  265.           $this->STATUT = $table_scale[5];
  266.           break;
  267.         case 'D':
  268.           $this->STATUT = $table_scale[5];
  269.           break;
  270.         default:
  271.           $this->STATUT = 'P';
  272.           break;
  273.       }
  274.     }
  275.     // On Determine le rang de l'échelle
  276.     $q = "SELECT scale_dependencies FROM " . $this->TDB_SCALE . " WHERE scale_id=" . $this->ROOT . ";";
  277.     $result = $sql_object->DBSelect($q);
  278.     // Si ce n'est pas l'échelle racine on assigne sinon =-1
  279.     $current_depth= (empty($result)) ? -1 : $result[0]['scale_dependencies'];
  280.     $this->DEPENDENCIES = $current_depth + 1;
  281.     // mise à jour du niveau des informations relatives à l'échelle
  282.     $q = "UPDATE  " . $this->TDB_SCALE . " set scale_denomination='" . $this->DENOMINATION . "', scale_surface=" . $this->SURFACE . ", scale_inhabitantsnumber=" . $this->INHABITANTSNUM . ", scale_root=" . $this->ROOT . ", scale_comment='" . $this->COMMENT . "', scale_dependencies=" . $this->DEPENDENCIES . ", scale_statut='" . $this->STATUT . "', scale_last_modify=NOW() WHERE scale_id=" . $this->ID . ";";
  283.     $result = $sql_object->DBQuery($q);
  284.     // si la nouvelle échelle root diffère de la précédente, update récursif des dépendances des échelles filles
  285.     if ($table_scale[6] != $this->ROOT) {
  286.       $this->getAllRecord($sql_object);
  287.       $tabledepth = $this->update_all_dependencies($this->ALL_REC, $this->DEPENDENCIES, $this->ID, $sql_object);
  288.     }
  289.     
  290.     // Notify the end of the current method
  291.     $this->dispatcher->notify(new sfEvent($this, 'scale.after_modify', array('data' => $table_scale, 'id' => $this->ID)));
  292.  
  293.     return true;
  294.   }
  295.  
  296.   /**
  297.    * scale::update_all_dependencies()
  298.    * Mise à jour de l'ensemble des échelles filles par rapport à ul'échelle courante.
  299.    *
  300.    * @param  $table 
  301.    * @param  $depth 
  302.    * @param  $id 
  303.    * @param  $sql_object 
  304.    * @return void 
  305.    */
  306.   function update_all_dependencies($table, $depth, $id, $sql_object)
  307.   {
  308.       // Notify the end of the current method
  309.       $this->dispatcher->notify(new sfEvent($this, 'scale.update_all_dependencies', array('data' => $table, 'id' => $id, 'depth' => $depth)));
  310.       
  311.     $table_depth = array();
  312.     for($i = 0; $i < count($table); $i++) {
  313.       $current_dep = $table[$i]['scale_dependencies'];
  314.       $current_root = $table[$i]['scale_root'];
  315.       $current_id = $table[$i]['scale_id'];
  316.  
  317.       if ($id == $current_root) {
  318.         $q = "UPDATE  " . $this->TDB_SCALE . " SET scale_dependencies='" . ($depth + 1) . "' WHERE scale_id='" . $current_id . "';";
  319.         $result = $sql_object->DBQuery($q);
  320.         $this->update_all_dependencies($table, $depth + 1, $current_id, $sql_object);
  321.       }
  322.     }
  323.   }
  324.  
  325.   /**
  326.    * scale::StateScale()
  327.    * modification du statut d'une échelle
  328.    *
  329.    * @access public
  330.    * @param int $id identifiant de l'echelle
  331.    * @param string $state (facultatif) 'P' Public/'D' Draft
  332.    * @param object $sql_object 
  333.    * @return bool $result
  334.    */
  335.   function StateScale($ID, $state, $sql_object)
  336.   {
  337.       // Notify the end of the current method
  338.       $this->dispatcher->notify(new sfEvent($this, 'scale.change_state', array('id' => $ID, 'state' => $state)));
  339.       
  340.     if (is_numeric($ID)) {
  341.       $this->ID = $ID;
  342.     }
  343.     if ($state != '') {
  344.       strtoupper($state);
  345.       switch ($state) {
  346.         case 'P':
  347.           $this->STATUT = $state;
  348.           break;
  349.         case 'D':
  350.           $this->STATUT = $state;
  351.           break;
  352.         default:
  353.           $this->STATUT = $state;
  354.           break;
  355.       }
  356.     } else $this->STATUT = 'P';
  357.     $q = "UPDATE  " . $this->TDB_SCALE . " set scale_statut='" . $this->STATUT . "', scale_last_modify=NOW() WHERE scale_id=" . $this->ID . ";";
  358.     $result = $sql_object->DBQuery($q);
  359.     return $result;
  360.   }
  361.  
  362.   /**
  363.    * scale::DeleteScale()
  364.    * suppression d'une echelle
  365.    *
  366.    * @access public
  367.    * @param int $ID identifiant de l'echelle
  368.    * @param string $type 'MASS_DELETE'
  369.    *  'MASS_DELETE' toutes les ressources associées à l'échelle sont rendues inactives
  370.    *  'MASS_MODIFY' remplacement de l'ancienne échelle par $ID_new
  371.    * @param int $ID_new nouvel id de rattachement
  372.    * @param object $sql_object 
  373.    * @return integer $affected // nombre de ressources affectées par les changements
  374.    */
  375.   function DeleteScale($ID, $sql_object, $type, $ID_new = -1)
  376.   {
  377.       // Notify the end of the current method
  378.       $this->dispatcher->notify(new sfEvent($this, 'scale.delete', array('id' => $ID, 'type' => $type, 'new_id' => $ID_new)));
  379.       
  380.     $affected = 0;
  381.     $this->ID = $ID;
  382.     if ($type == 'MASS_MODIFY') {
  383.       if ($ID_new != -1 && is_numeric($ID_new)) {
  384.         $q = "UPDATE  " . $this->TDB_SCALE . " set scale_statut='E', scale_last_modify=NOW() WHERE scale_id=" . $this->ID . ";";
  385.         $result = $sql_object->DBQuery($q);
  386.         if ($result) {
  387.           $q = "SELECT scale_dependencies FROM " . $this->TDB_SCALE . " WHERE scale_id=" . $ID_new . ";";
  388.           $result = $sql_object->DBSelect($q);
  389.           $dependencies = $result[0]['scale_dependencies'];
  390.           $q = "UPDATE  " . $this->TDB_SCALE . " set scale_root=" . $ID_new . " WHERE scale_root=" . $this->ID . ";";
  391.           $result = $sql_object->DBQuery($q);
  392.           // modification récursive des dépendances.
  393.           $this->getAllRecord($sql_object);
  394.           $this->update_all_dependencies($this->ALL_REC, $dependencies, $ID_new, $sql_object);
  395.           // mise à jour des ressources.
  396.           $q_news = "UPDATE  " . T_NEWS . " set news_scale=" . $ID_new . " WHERE  news_scale=" . $this->ID . ";";
  397.           $result = $sql_object->DBQuery($q_news);
  398.           $affected += $sql_object->DBaffectedRow();
  399.           $q_project = "UPDATE  " . T_PROJECT . " set project_scale_id=" . $ID_new . " WHERE  project_scale_id=" . $this->ID . ";";
  400.           $result = $sql_object->DBQuery($q_project);
  401.           $affected += $sql_object->DBaffectedRow();
  402.           $q_publication = "UPDATE  " . T_PUBLI . " set publi_scale=" . $ID_new . " WHERE  publi_scale=" . $this->ID . ";";
  403.           $result = $sql_object->DBQuery($q_publication);
  404.           $affected += $sql_object->DBaffectedRow();
  405.           $q_sdi = "UPDATE  " . T_SDI_VALUE . " set sdiv_scale=" . $ID_new . " WHERE  sdiv_scale=" . $this->ID . ";";
  406.           $result = $sql_object->DBQuery($q_sdi);
  407.           $affected += $sql_object->DBaffectedRow();
  408.         }
  409.       }
  410.     }
  411.     if ($type == 'MASS_DELETE') {
  412.       $this->getAllRecord($sql_object);
  413.       $this->getScaleDepthAndSon($this->ALL_REC, $this->ID);
  414.  
  415.       $update_bdd_scale = '';
  416.       $update_bdd_news = '';
  417.       $update_bdd_publi = '';
  418.       $update_bdd_sdi = '';
  419.       for($i = 0; $i < count($this->SON_TABLE); $i++) {
  420.         $update_bdd_scale .= " OR scale_id='" . $this->SON_TABLE[$i] . "'";
  421.         $update_bdd_news .= " OR news_scale='" . $this->SON_TABLE[$i] . "'";
  422.         $update_bdd_publi .= " OR publi_scale='" . $this->SON_TABLE[$i] . "'";
  423.         $update_bdd_sdi .= " OR sdiv_scale='" . $this->SON_TABLE[$i] . "'";
  424.         $update_bdd_project .= " OR project_scale_id='" . $this->SON_TABLE[$i] . "'";
  425.       }
  426.       // Effacement de l'échelle et ses fils
  427.       $q = "UPDATE  " . $this->TDB_SCALE . " set scale_statut='E', scale_last_modify=NOW() WHERE scale_id='" . $this->ID . "'";
  428.       $q .= $update_bdd_scale;
  429.       $q .= ";";
  430.  
  431.       $result = $sql_object->DBQuery($q);
  432.  
  433.       if ($result == 1) {
  434.         $q_news = "UPDATE  " . T_NEWS . " SET news_statut='E' WHERE  news_scale='" . $this->ID . "'";
  435.         $q_news .= $update_bdd_news;
  436.         $q_news .= ";";
  437.  
  438.         $result = $sql_object->DBQuery($q_news);
  439.         $affected += $sql_object->DBaffectedRow();
  440.  
  441.         $q_project = "UPDATE  " . T_PROJECT . " SET project_statut='E' WHERE  project_scale_id='" . $this->ID . "'";
  442.         $q_project .= $update_bdd_project;
  443.         $q_project .= ";";
  444.  
  445.         $result = $sql_object->DBQuery($q_project);
  446.         $affected += $sql_object->DBaffectedRow();
  447.  
  448.         $q_publication = "UPDATE  " . T_PUBLI . " SET publi_statut='E' WHERE  publi_scale='" . $this->ID . "'";
  449.         $q_publication .= $update_bdd_publi;
  450.         $q_publication .= ";";
  451.  
  452.         $result = $sql_object->DBQuery($q_publication);
  453.         $affected += $sql_object->DBaffectedRow();
  454.  
  455.         $q_sdi = "UPDATE  " . T_SDI_VALUE . " SET sdiv_statut='E' WHERE  sdiv_scale='" . $this->ID . "'";
  456.         $q_sdi .= $update_bdd_sdi;
  457.         $q_sdi .= ";";
  458.  
  459.         $result = $sql_object->DBQuery($q_sdi);
  460.         $affected += $sql_object->DBaffectedRow();
  461.       }
  462.     }
  463.     return $affected;
  464.   }
  465. }
  466.  
  467. ?>

Documentation generated on Thu, 20 Mar 2014 16:46:47 +0100 by phpDocumentor 1.4.1