Beispiel #1
0
 /**
  * Function to allow overruling of transform for certain models
  *
  * @param \MUtil_Model_ModelAbstract $model Parent model
  * @param \MUtil_Model_ModelAbstract $sub Sub model
  * @param array $data The nested data rows
  * @param array $join The join array
  * @param string $name Name of sub model
  * @param boolean $new True when loading a new item
  * @param boolean $isPostData With post data, unselected multiOptions values are not set so should be added
  */
 protected function transformLoadSubModel(\MUtil_Model_ModelAbstract $model, \MUtil_Model_ModelAbstract $sub, array &$data, array $join, $name, $new, $isPostData)
 {
     if (1 === count($join)) {
         // Suimple implementation
         $mkey = key($join);
         $skey = reset($join);
         $mfor = \MUtil_Ra::column($mkey, $data);
         // \MUtil_Echo::track($mfor);
         if ($new) {
             $sdata = $sub->loadNew(1);
         } else {
             $sdata = $sub->load(array($skey => $mfor));
         }
         // \MUtil_Echo::track($sdata);
         if ($sdata) {
             $skeys = array_flip(\MUtil_Ra::column($skey, $sdata));
             $empty = array_fill_keys(array_keys(reset($sdata)), null);
             foreach ($data as &$mrow) {
                 $mfind = $mrow[$mkey];
                 if (isset($skeys[$mfind])) {
                     $mrow += $sdata[$skeys[$mfind]];
                 } else {
                     $mrow += $empty;
                 }
             }
         } else {
             $empty = array_fill_keys($sub->getItemNames(), null);
             foreach ($data as &$mrow) {
                 $mrow += $empty;
             }
         }
         // \MUtil_Echo::track($mrow);
     } else {
         // Multi column implementation
         $empty = array_fill_keys($sub->getItemNames(), null);
         foreach ($data as &$mrow) {
             $filter = $sub->getFilter();
             foreach ($join as $from => $to) {
                 if (isset($mrow[$from])) {
                     $filter[$to] = $mrow[$from];
                 }
             }
             if ($new) {
                 $sdata = $sub->loadNew();
             } else {
                 $sdata = $sub->loadFirst($filter);
             }
             if ($sdata) {
                 $mrow += $sdata;
             } else {
                 $mrow += $empty;
             }
             // \MUtil_Echo::track($sdata, $mrow);
         }
     }
 }
Beispiel #2
0
 /**
  * Returns true if and only if $value meets the validation requirements
  *
  * If $value fails validation, then this method returns false, and
  * getMessages() will return an array of messages that explain why the
  * validation failed.
  *
  * @param  mixed $value
  * @param  array $context
  * @return boolean
  * @throws \Zend_Validate_Exception If validation of $value is impossible
  */
 public function isValid($value, $context = array())
 {
     $this->_setValue($value);
     // Make sure the (optionally filtered) value is in the context
     $context[reset($this->_fields)] = $value;
     $filter = array();
     foreach ($this->_fields as $name) {
         // Return valid when not all the fields to check for are in the context
         if (!isset($context[$name])) {
             return true;
         }
         $filter[$name] = $context[$name];
     }
     $check = array();
     $doGet = $this->_model->hasItemsUsed();
     $keys = $this->_model->getKeys();
     foreach ($keys as $id => $key) {
         if ($doGet) {
             // Make sure the item is used
             $this->_model->get($key);
         }
         if (isset($context[$id])) {
             $check[$key] = $context[$id];
         } elseif (isset($context[$key])) {
             $check[$key] = $context[$key];
         } else {
             // Not all keys are in => therefore this is a new item
             $check = false;
             break;
         }
     }
     $rows = $this->_model->load($filter);
     if (!$rows) {
         return true;
     }
     if (!$check) {
         // Rows where found while it is a new item
         $this->_error(self::ERROR_RECORD_FOUND);
         return false;
     }
     $count = count($check);
     foreach ($rows as $row) {
         // Check for return of the whole check
         if (count(array_intersect_assoc($check, $row)) !== $count) {
             // There exists a row with the same values but not the same keys
             $this->_error(self::ERROR_RECORD_FOUND);
             return false;
         }
     }
     return true;
 }
Beispiel #3
0
 /**
  * Switch to multi rows mode and set those rows.
  *
  * @param array $rows Or load from model
  * @return \MUtil_Model_Format_DisplayFormatter (continuation pattern)
  * @throws \MUtil_Model_ModelException
  */
 public function setRows(array $rows = null)
 {
     $this->setMode(self::MODE_ROWS);
     if (null === $rows) {
         if ($this->_repeater) {
             $rows = $this->_repeater->__getRepeatable();
         } else {
             $rows = $this->model->load();
         }
         if (!$rows) {
             $rows = array();
         }
     }
     $this->_data = $rows;
     if ($this->_chainedBridge) {
         $this->_chainedBridge->_data = $this->_data;
     }
     $this->setRepeater($this->_data);
     return $this;
 }
 /**
  * Function to allow overruling of transform for certain models
  *
  * @param \MUtil_Model_ModelAbstract $model Parent model
  * @param \MUtil_Model_ModelAbstract $sub Sub model
  * @param array $data The nested data rows
  * @param array $join The join array
  * @param string $name Name of sub model
  * @param boolean $new True when loading a new item
  * @param boolean $isPostData With post data, unselected multiOptions values are not set so should be added
  */
 protected function transformLoadSubModel(\MUtil_Model_ModelAbstract $model, \MUtil_Model_ModelAbstract $sub, array &$data, array $join, $name, $new, $isPostData)
 {
     foreach ($data as $key => $row) {
         // E.g. if loaded from a post
         if (isset($row[$name])) {
             $rows = $sub->processAfterLoad($row[$name], $new, $isPostData);
         } elseif ($new) {
             $rows = $sub->loadAllNew();
         } else {
             $filter = $sub->getFilter();
             foreach ($join as $parent => $child) {
                 if (isset($row[$parent])) {
                     $filter[$child] = $row[$parent];
                 }
             }
             // If $filter is empty, treat as new
             if (empty($filter)) {
                 $rows = $sub->loadAllNew();
             } else {
                 $rows = $sub->load($filter);
             }
         }
         $data[$key][$name] = $rows;
     }
 }