public static function createOrUpdateState($config)
 {
     $afPortalStateObj = self::retrieveByIdXml($config->idXml);
     if (!$afPortalStateObj) {
         $afPortalStateObj = new afPortalState();
         $afPortalStateObj->setIdXml($config->idXml);
         $afPortalStateObj->setLayoutType($config->layoutType);
         $afPortalStateObj->setContent($config->content);
         $afPortalStateObj->save();
     } else {
         $content = $afPortalStateObj->getContent();
         unset($content[$config->layoutItem]);
         $content[$config->layoutItem] = $config->content[$config->layoutItem];
         $afPortalStateObj->setLayoutType($config->layoutType);
         $afPortalStateObj->setContent($content);
         $afPortalStateObj->save();
         unset($content);
     }
     if (isset($config->layoutItem) && isset($config->content[$config->layoutItem]['portalLayoutNewType']) && $config->content[$config->layoutItem]['portalLayoutNewType'] != null && $config->content[$config->layoutItem]['portalLayoutNewType'] != false) {
         $oldLayout = json_decode($config->content[$config->layoutItem]['portalLayoutType']);
         $newLayout = json_decode($config->content[$config->layoutItem]['portalLayoutNewType']);
         $countOldLayout = count($oldLayout);
         $countNewLayout = count($newLayout);
         $columns = $config->content[$config->layoutItem]['portalColumns'];
         $afPortalStateObj->setPortalLayoutType($config->layoutItem, $config->content[$config->layoutItem]['portalLayoutNewType']);
         if ($countOldLayout < $countNewLayout) {
             //add diff columns from old layout to new layout
             for ($i = $countOldLayout; $i < $countNewLayout; $i++) {
                 $columns[$i] = array();
             }
             $afPortalStateObj->setColumns($config->layoutItem, $columns);
         } elseif ($countOldLayout > $countNewLayout) {
             //remove diff columns from old layout to new layout & copy the widgets from removed columns to the last existing column
             $widgets = array();
             for ($i = $countNewLayout; $i < $countOldLayout; $i++) {
                 if (isset($columns[$i])) {
                     $widgets = array_merge($widgets, $columns[$i]);
                     unset($columns[$i]);
                 }
             }
             $columns[$countNewLayout - 1] = array_merge($columns[$countNewLayout - 1], $widgets);
             $afPortalStateObj->setColumns($config->layoutItem, $columns);
         }
         $afPortalStateObj->save();
     }
     return $afPortalStateObj;
 }
 /**
  * Exclude object from result
  *
  * @param     afPortalState $afPortalState Object to remove from the list of results
  *
  * @return    afPortalStateQuery The current query, for fluid interface
  */
 public function prune($afPortalState = null)
 {
     if ($afPortalState) {
         $this->addUsingAlias(afPortalStatePeer::ID, $afPortalState->getId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }