Example #1
0
 public function compareWith(Ficha $ficha)
 {
     $comparacion = NULL;
     $left = $this->toArray(false);
     $right = $ficha->toArray(false);
     $exclude = array('id', 'genero_id', 'convenio', 'updated_at', 'created_at', 'publicado', 'publicado_at', 'servicio_codigo', 'comentarios', 'alias', 'rating', 'estado', 'estado_justificacion', 'actualizable', 'diagramacion', 'locked', 'metaficha', 'metaficha_servicios', 'metaficha_campos', 'metaficha_opciones', 'votos_positivos', 'votos_negativos');
     $labels = array('tipo' => array(1 => 'Personas', 2 => 'Empresas', 3 => 'Ambos', 0 => 'No asignado'));
     foreach ($left as $key => $val) {
         if (!in_array($key, $exclude)) {
             if ($right[$key] != $left[$key]) {
                 if (array_key_exists($key, $labels)) {
                     $diff = htmlDiff(strip_tags($labels[$key][$right[$key]]), strip_tags($labels[$key][$left[$key]]));
                 } else {
                     $diff = htmlDiff(strip_tags($right[$key]), strip_tags($left[$key]));
                 }
                 $diff = trim($diff);
                 if ($diff) {
                     $comparacion[$key]->left[] = $diff;
                     $comparacion[$key]->right[] = $right[$key];
                 }
             }
         }
     }
     //Comparamos las relaciones one
     //Nombre => Valor_Para_Label
     $relacionesAComparar = array('Servicio' => 'nombre', 'Genero' => 'nombre');
     foreach ($relacionesAComparar as $r => $label) {
         $left = $this->get($r);
         $right = $ficha->get($r);
         //debug($left->toArray());
         //debug($right->toArray());
         if ($left->toArray() !== $right->toArray()) {
             $comparacion[$r]->left[] = '<del>' . $right->{$label} . '</del> <ins>' . $left->{$label} . '</ins>';
             // El htmldiff tenĂ­a problemas
             $comparacion[$r]->right[] = $right->{$label};
         }
     }
     //Comparamos las relaciones many
     //[caso un valor] : Nombre => Valor_Para_Label
     //[caso multiple] : Nombre => Array(Array(label1,label2,...,labelN),separador)
     $relacionesAComparar = array('Tags' => 'nombre', 'Temas' => 'nombre', 'HechosVida' => 'nombre', 'RangosEdad' => array(array('edad_minima', 'edad_maxima'), '-'));
     //Caso multiple
     foreach ($relacionesAComparar as $r => $label) {
         $left = $this->get($r);
         $right = $ficha->get($r);
         if (!$this->bidimensional_array_equals($left->toArray(false), $right->toArray(false), array('id', 'ficha_id'))) {
             $tmp = array();
             foreach ($right as $rig) {
                 if (!is_array($label)) {
                     $tmp[] = $rig->{$label};
                 } else {
                     list($mlabels, $sep) = $label;
                     $values = array();
                     foreach ($mlabels as $mlabel) {
                         $values[] = $rig->{$mlabel};
                     }
                     $val = implode($sep, $values);
                     $tmp[] = $val;
                 }
             }
             $right_label = implode(" ", $tmp);
             $tmp = array();
             foreach ($left as $l) {
                 if (!is_array($label)) {
                     $tmp[] = $l->{$label};
                 } else {
                     list($mlabels, $sep) = $label;
                     $values = array();
                     foreach ($mlabels as $mlabel) {
                         $values[] = $l->{$mlabel};
                     }
                     $val = implode($sep, $values);
                     $tmp[] = $val;
                 }
             }
             $left_label = implode(" ", $tmp);
             $diff = trim(htmlDiff($right_label, $left_label));
             if ($diff) {
                 $comparacion[$r]->left[0] = $diff;
                 $comparacion[$r]->right[0] = $right_label;
             }
         }
     }
     return $comparacion;
 }