public function setPrivacyAction()
 {
     if (!$this->getRequest()->isPost()) {
         return $this->_helper->json(array('status' => false, 'error' => 'Invalid method'));
     }
     $viewer = Engine_Api::_()->user()->getViewer();
     $field_id = $this->getRequest()->getParam('field');
     $privacy = $this->getRequest()->getParam('privacy');
     if (!$viewer || !$viewer->user_id || !$field_id || !$privacy) {
         return $this->_helper->json(array('status' => false, 'error' => 'Invalid parameters'));
     }
     $allowedPrivacies = Fields_Api_Core::getFieldPrivacyOptions();
     if (empty($allowedPrivacies[$privacy])) {
         return $this->_helper->json(array('status' => false, 'error' => 'Invalid privacy option'));
     }
     $table = Engine_Api::_()->fields()->getTable('user', 'values');
     //$table = new Fields_Model_DbTable_Values();
     $ret = (int) $table->update(array('privacy' => (string) $privacy), array('item_id = ?' => (int) $viewer->user_id, 'field_id = ?' => (int) $field_id));
     return $this->_helper->json(array('status' => true, 'c' => $ret));
 }
 public function saveValues()
 {
     // An id must be set to save data (duh)
     if (is_null($this->getItem())) {
         throw new Exception("Cannot save data when no identity has been specified");
     }
     // Iterate over values
     $values = Engine_Api::_()->fields()->getFieldsValues($this->getItem());
     $fVals = $this->getValues();
     if ($this->_elementsBelongTo) {
         if (isset($fVals[$this->_elementsBelongTo])) {
             $fVals = $fVals[$this->_elementsBelongTo];
         }
     }
     $privacyOptions = Fields_Api_Core::getFieldPrivacyOptions();
     foreach ($fVals as $key => $value) {
         $parts = explode('_', $key);
         if (count($parts) != 3) {
             continue;
         }
         list($parent_id, $option_id, $field_id) = $parts;
         // Whoops no headings
         if ($this->getElement($key) instanceof Engine_Form_Element_Heading) {
             continue;
         }
         // Array mode
         if (is_array($value) || $this->getElement($key)->isArray()) {
             // Lookup
             $valueRows = $values->getRowsMatching(array('field_id' => $field_id, 'item_id' => $this->getItem()->getIdentity()));
             // Delete all
             $prevPrivacy = null;
             foreach ($valueRows as $valueRow) {
                 if (!empty($valueRow->privacy)) {
                     $prevPrivacy = $valueRow->privacy;
                 }
                 $valueRow->delete();
             }
             if ($this->_hasPrivacy && !empty($this->_privacyValues[$key]) && !empty($privacyOptions[$this->_privacyValues[$key]])) {
                 $prevPrivacy = $this->_privacyValues[$key];
             }
             // Insert all
             $indexIndex = 0;
             if (is_array($value) || !empty($value)) {
                 foreach ((array) $value as $singleValue) {
                     $valueRow = $values->createRow();
                     $valueRow->field_id = $field_id;
                     $valueRow->item_id = $this->getItem()->getIdentity();
                     $valueRow->index = $indexIndex++;
                     $valueRow->value = $singleValue;
                     if ($this->_hasPrivacy && $prevPrivacy) {
                         $valueRow->privacy = $prevPrivacy;
                     }
                     $valueRow->save();
                 }
             } else {
                 $valueRow = $values->createRow();
                 $valueRow->field_id = $field_id;
                 $valueRow->item_id = $this->getItem()->getIdentity();
                 $valueRow->index = 0;
                 $valueRow->value = '';
                 if ($this->_hasPrivacy && $prevPrivacy) {
                     $valueRow->privacy = $prevPrivacy;
                 }
                 $valueRow->save();
             }
         } else {
             // Lookup
             $valueRow = $values->getRowMatching(array('field_id' => $field_id, 'item_id' => $this->getItem()->getIdentity(), 'index' => 0));
             // Remove value row if empty
             //        if( empty($value) ) {
             //          if( $valueRow ) {
             //            $valueRow->delete();
             //          }
             //          continue;
             //        }
             // Create if missing
             $isNew = false;
             if (!$valueRow) {
                 $isNew = true;
                 $valueRow = $values->createRow();
                 $valueRow->field_id = $field_id;
                 $valueRow->item_id = $this->getItem()->getIdentity();
             }
             $valueRow->value = htmlspecialchars($value);
             if ($this->_hasPrivacy && !empty($this->_privacyValues[$key]) && !empty($privacyOptions[$this->_privacyValues[$key]])) {
                 $valueRow->privacy = $this->_privacyValues[$key];
             }
             $valueRow->save();
             /*
                     // Insert activity if being changed (and publish is enabled)
                     if( !$isNew ) {
              $field = Engine_Api::_()->fields()->getFieldsMeta($this->getItem())->getRowMatching('field_id', $field_id);
              if( is_object($field) && !empty($field->publish) ) {
                $helper = new Fields_View_Helper_FieldValueLoop();
                $helper->view = Zend_Registry::get('Zend_View');
                $actionTable = Engine_Api::_()->getDbtable('actions', 'activity');
             
                if( $field->type )
                $actionTable->addActivity($this->getItem(), $this->getItem(), 'fields_change_generic', null, array(
                  'label' => $field->label,
                  'value' => $helper->getFieldValueString($field, $valueRow, $this->getItem()), //$value,
                ));
              }
                     }
             * 
             */
         }
     }
     // Update search table
     Engine_Api::_()->getApi('core', 'fields')->updateSearch($this->getItem(), $values);
     // Fire on save hook
     Engine_Hooks_Dispatcher::getInstance()->callEvent('onFieldsValuesSave', array('item' => $this->getItem(), 'values' => $values));
     // Regenerate form
     $this->generate();
 }