/**
  * Creates an empty instance of the DmBehavior
  * 
  * @param string $key The behavior key
  * @param string $attachedTo On which container is attached, page, area, zone or widget?
  * @param int $attachedToId The id of the container
  * @param string $attachedToSelector If it is attached to the content
  * @return type 
  */
 public function createEmptyInstance($key, $attachedTo, $attachedToId, $attachedToSelector = null)
 {
     $formClass = $this->getBehaviorFormClass($key);
     $behavior = new DmBehavior();
     $behavior->setDmBehaviorKey($key);
     $form = new $formClass($behavior);
     $form->removeCsrfProtection();
     $position = dmDb::query('DmBehavior b')->orderBy('b.position desc')->limit(1)->select('MAX(b.position) as position')->fetchOneArray();
     $saveData = array('position' => $position['position'] + 1, 'dm_behavior_key' => $key, 'dm_behavior_attached_to' => $attachedTo, 'dm_' . $attachedTo . '_id' => (int) $attachedToId, 'dm_behavior_value' => json_encode($form->getDefaults()));
     if (!is_null($attachedToSelector)) {
         $saveData['dm_behavior_attached_to_selector'] = $attachedToSelector;
     }
     return dmDb::create('DmBehavior', $saveData)->saveGet();
 }
 /**
  * Paste behavior from clipboard
  * returns behavior settings, css, js and action which caused a paste
  */
 public function executePaste(dmWebRequest $request)
 {
     if (!$this->getUser()->can('behavior_add')) {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('You do not have privileges to paste behaviors'));
     }
     if (!$this->getUser()->can('behavior_delete') && $request->getParameter('clipboard_action') == 'cut') {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('You do not have privileges to cut behaviors'));
     }
     $clipboard = $this->getUser()->getAttribute('dm_behavior_clipboard', null, 'dm.front_user_behavior_clipboard');
     if (is_null($clipboard)) {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('The clipboard is empty'));
     }
     if ($clipboard['dm_behavior_clipboard_action'] != $request->getParameter('dm_behavior_clipboard_action')) {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('Unknown error occured - cliboard action missmatch'));
     }
     $behaviorsManager = $this->getService('behaviors_manager');
     $behavior = DmBehaviorTable::getInstance()->findOneById($clipboard['dm_behavior_id']);
     if (!$behavior) {
         $this->getUser()->setAttribute('dm_behavior_clipboard', null, 'dm.front_user_behavior_clipboard');
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('The behavior from the clipboard does not exist anymore'));
     }
     $clipboardBehavior = $behavior;
     if ($clipboard['dm_behavior_clipboard_action'] == 'copy') {
         $clipboardBehavior = new DmBehavior();
         try {
             $position = dmDb::query('DmBehavior b')->orderBy('b.position desc')->limit(1)->select('MAX(b.position) as position')->fetchOneArray();
             $clipboardBehavior->setPosition($position['position'] + 1);
         } catch (Exception $e) {
             return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('The behavior from clipboard could not be created'));
         }
     }
     $clipboardBehavior->setDmBehaviorKey($behavior->getDmBehaviorKey());
     $clipboardBehavior->setDmBehaviorAttachedTo($request->getParameter('dm_behavior_attached_to'));
     if ($request->getParameter('dm_behavior_attached_to_content') == 'true') {
         $clipboardBehavior->setDmBehaviorAttachedToSelector($request->getParameter('dm_behavior_attached_to_selector'));
     } else {
         $clipboardBehavior->setDmBehaviorAttachedToSelector(null);
     }
     $clipboardBehavior->setDmPageId(null);
     $clipboardBehavior->setDmAreaId(null);
     $clipboardBehavior->setDmZoneId(null);
     $clipboardBehavior->setDmWidgetId(null);
     switch ($request->getParameter('dm_behavior_attached_to')) {
         case 'page':
             $clipboardBehavior->setDmPageId($request->getParameter('dm_behavior_attached_to_id'));
             break;
         case 'area':
             $clipboardBehavior->setDmAreaId($request->getParameter('dm_behavior_attached_to_id'));
             break;
         case 'zone':
             $clipboardBehavior->setDmZoneId($request->getParameter('dm_behavior_attached_to_id'));
             break;
         case 'widget':
             $clipboardBehavior->setDmWidgetId($request->getParameter('dm_behavior_attached_to_id'));
             break;
         default:
             return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('The behavior can not be attached to unknown container'));
             break;
     }
     $clipboardBehavior->setDmBehaviorValue($behavior->getDmBehaviorValue());
     $clipboardBehavior->setDmBehaviorEnabled($behavior->getDmBehaviorEnabled());
     try {
         $clipboardBehavior = $clipboardBehavior->saveGet();
     } catch (Exception $e) {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('The behavior from clipboard could not be created'));
     }
     try {
         $viewClass = $behaviorsManager->getBehaviorViewClass($clipboardBehavior->getDmBehaviorKey());
         $behaviorView = new $viewClass($this->context, $clipboardBehavior);
     } catch (Exception $e) {
         return $this->renderError($this->getI18n()->__('Error'), $this->getI18n()->__('Behavior created, but could not initialize behavior view class'));
     }
     return $this->renderJson(array('error' => false, 'dm_behavior_clipboard_action' => $clipboard['dm_behavior_clipboard_action'], 'dm_behavior_data' => $behaviorView->renderArray(), 'js' => $this->parseJavascripts($behaviorView->getJavascripts()), 'css' => $this->parseStylesheets($behaviorView->getStylesheets())));
 }