Exemplo n.º 1
0
 public function editAction()
 {
     $p = $_REQUEST;
     $pId = empty($p['id']) ? die('ID不能为空') : intval($p['id']);
     $tMO = new FoodModel();
     $tCateRow = $tMO->where('id = ' . $pId)->fRow();
     $this->assign('tId', $pId);
     $this->assign('tCateRow', $tCateRow);
     $tFT = new FoodtagModel();
     $tTagList = $tFT->field('id,concat(title,\'_tag\') title,fid,flag')->where('fid = ' . $pId)->fList();
     foreach ($tTagList as $v) {
         if (is_array($v)) {
             foreach ($v as $k => $v1) {
                 if ($k == 'title' || $k == 'flag') {
                     if ($k == 'title') {
                         $t1 = $v1;
                     } elseif ($k == 'flag') {
                         $t2 = $v1;
                     }
                 }
             }
             $tTagRow[$t1] = $t2;
         }
     }
     $this->assign('tTagRow', isset($tTagRow) ? $tTagRow : null);
     $tTP = new FoodcategoryModel();
     $pType = $tTP->field('id,title')->fList();
     $this->assign('pType', $pType);
     if ($_SERVER['REQUEST_METHOD'] == "POST") {
         $pPhoto = isset($_FILES['photo']) ? $_FILES['photo'] : null;
         if ($pPhoto) {
             $subPath = date("Ymd", time());
             $tUpload = new Tool_Upload($pPhoto, APPLICATION_PATH . '/public/upload/food/' . $subPath . '/');
             $tSavename = $tUpload->getSaveName();
             $tRes = $tUpload->upload($tSavename);
             $tExt = $tUpload->extension;
             if ($tRes == 1) {
                 $tUpload->createThumb('300', '300', APPLICATION_PATH . '/public/upload/food/' . $subPath . '_small/');
                 $tSFile = '/upload/food/' . $subPath . '_small/' . $tSavename . '.' . $tExt;
                 $tFile = '/upload/food/' . $subPath . '/' . $tSavename . '.' . $tExt;
             } else {
                 Tool_Fnc::ajaxMsg('图片上传失败');
                 die;
             }
         }
         $pTitle = empty($p['title']) ? Tool_Fnc::ajaxMsg('食物名称不能为空') : Tool_Fnc::safe_string($p['title']);
         $pProtein = empty($p['protein']) ? Tool_Fnc::ajaxMsg('蛋白质不能为空') : floatval($p['protein']);
         $pCalory = !isset($p['calory']) ? '' : floatval($p['calory']);
         $pFat = !isset($p['fat']) ? '' : floatval($p['fat']);
         $pCarbohydrate = !isset($p['carbohydrate']) ? '' : floatval($p['carbohydrate']);
         $pFiber = !isset($p['fiber_dietary']) ? '' : floatval($p['fiber_dietary']);
         $pCalcium = !isset($p['calcium']) ? '' : floatval($p['calcium']);
         $pZinc = !isset($p['zinc']) ? '' : floatval($p['zinc']);
         $pVitamin_a = !isset($p['vitamin_a']) ? '' : floatval($p['vitamin_a']);
         $pVitamin_c = !isset($p['vitamin_c']) ? '' : floatval($p['vitamin_c']);
         $pMagnesium = !isset($p['magnesium']) ? '' : floatval($p['magnesium']);
         $pSelenium = !isset($p['selenium']) ? '' : floatval($p['selenium']);
         $pCopper = !isset($p['copper']) ? '' : floatval($p['copper']);
         $pPhosphor = !isset($p['phosphor']) ? '' : floatval($p['phosphor']);
         $pNiacin = !isset($p['niacin']) ? '' : floatval($p['niacin']);
         $pVitamin_e = !isset($p['vitamin_e']) ? '' : floatval($p['vitamin_e']);
         $pManganese = !isset($p['manganese']) ? '' : floatval($p['manganese']);
         $pCholesterol = !isset($p['cholesterol']) ? '' : floatval($p['cholesterol']);
         $pIron = !isset($p['iron']) ? '' : floatval($p['iron']);
         $pKalium = !isset($p['kalium']) ? '' : floatval($p['kalium']);
         $pNatrium = !isset($p['natrium']) ? '' : floatval($p['natrium']);
         $pThiamine = !isset($p['thiamine']) ? '' : floatval($p['thiamine']);
         $pLactoflavin = !isset($p['lactoflavin']) ? '' : floatval($p['lactoflavin']);
         $pCarotene = !isset($p['carotene']) ? '' : floatval($p['carotene']);
         $pTag = array();
         empty($p['calory_tag']) ? '' : ($pTag['calory'] = Tool_Fnc::safe_string($p['calory_tag']));
         empty($p['protein_tag']) ? '' : ($pTag['protein'] = Tool_Fnc::safe_string($p['protein_tag']));
         empty($p['fat_tag']) ? '' : ($pTag['fat'] = Tool_Fnc::safe_string($p['fat_tag']));
         empty($p['carbohydrate_tag']) ? '' : ($pTag['carbohydrate'] = Tool_Fnc::safe_string($p['carbohydrate_tag']));
         empty($p['fiber_dietary_tag']) ? '' : ($pTag['fiber_dietary'] = Tool_Fnc::safe_string($p['fiber_dietary_tag']));
         $pTags = empty($pTag) ? '' : implode('|', $pTag);
         $pSuggest = $p['suggest'];
         $pUpdated = time();
         $pCid = empty($p['c_id']) ? Tool_Fnc::ajaxMsg('食物没有分类') : floatval($p['c_id']);
         $tRow = $tTP->field('id,title')->where('id= \'' . $pCid . '\'')->fRow();
         $tData = array('id' => $pId, 'title' => $pTitle, 'calory' => $pCalory, 'protein' => $pProtein, 'fat' => $pFat, 'carbohydrate' => $pCarbohydrate, 'fiber_dietary' => $pFiber, 'calcium' => $pCalcium, 'zinc' => $pZinc, 'vitamin_a' => $pVitamin_a, 'vitamin_c' => $pVitamin_c, 'magnesium' => $pMagnesium, 'selenium' => $pSelenium, 'copper' => $pCopper, 'phosphor' => $pPhosphor, 'niacin' => $pNiacin, 'vitamin_e' => $pVitamin_e, 'manganese' => $pManganese, 'cholesterol' => $pCholesterol, 'iron' => $pIron, 'kalium' => $pKalium, 'natrium' => $pNatrium, 'thiamine' => $pThiamine, 'lactoflavin' => $pLactoflavin, 'carotene' => $pCarotene, 'tag' => $pTags, 'updated' => $pUpdated, 'suggest' => $pSuggest, 'c_id' => $tRow['id'], 'c_title' => $tRow['title']);
         $tData['large_img'] = isset($tFile) ? $tFile : $p['large_img'];
         $tData['thumb_img'] = isset($tSFile) ? $tSFile : $p['thumb_img'];
         $tFF = new FoodtagModel();
         if ($tMO->update($tData)) {
             $tFF->where(' fid =' . $pId)->del();
             foreach ($pTag as $k => $v) {
                 $values['title'] = $k;
                 $values['fid'] = $pId;
                 $values['flag'] = $v;
                 $tFF->insert($values);
             }
             Tool_Fnc::ajaxMsg('修改成功', 1);
         } else {
             Tool_Fnc::ajaxMsg('修改失败');
         }
     }
 }
Exemplo n.º 2
0
 public function indexAction()
 {
     exit;
     #$db = new SQLite3('/home/zhangyueru/data/www/dakang_www/shell/foods.db');
     #$res = $db->query('select * from foods');
     #$data = $db->fetchArray($res);
     #print_r($data);
     #
     header('Content-type:text/html;charset=utf-8');
     $tFMO = new FoodModel();
     $tFUMO = new FoodunitsModel();
     $tFTMO = new FoodtagModel();
     $tSqlite = new Orm_Sqlite('/home/zhangyueru/data/www/dakang_www/shell/food.db');
     $res = $tSqlite->query('select a.*,s.name ctitle  from (select fd.thumb_image_url,fd.large_image_url,fd.ingredient,lights,cate_id,fd.name,fd.units from foods f, food_detail fd where f.id = fd.parent_id) a,categories s where a.cate_id = s.id');
     $ii = 0;
     while ($tRow = $res->fetchArray()) {
         $tTime = time();
         $ii++;
         $large_image_url = '';
         $thumb_image_url = '';
         if (!empty($tRow['thumb_image_url']) && $tRow['thumb_image_url'] != 'null') {
             $thumb_image_url = '/foodimg/small/' . $tTime . '_' . rand(1000, 99999) . '.jpg';
             $this->saveimg($tRow['thumb_image_url'], $thumb_image_url);
             echo 'thumb';
         }
         if (!empty($tRow['large_image_url']) && $tRow['large_image_url'] != 'null') {
             $large_image_url = '/foodimg/' . $tTime . '_' . rand(1000, 99999) . '.jpg';
             $this->saveimg($tRow['large_image_url'], $large_image_url);
             echo 'large';
         }
         $i = json_decode($tRow['ingredient'], true);
         $l = json_decode($tRow['lights'], true);
         $tTag = array();
         foreach ($l as $tKey => $tVal) {
             if (empty($tVal)) {
                 continue;
             }
             $tTag[$tKey] = $tVal;
         }
         $tTag = implode('|', $tTag);
         $i['calcium'] = empty($i['calcium']) ? 0 : $i['calcium'];
         $i['vitamin_a'] = empty($i['vitamin_a']) ? 0 : $i['vitamin_a'];
         $i['zinc'] = empty($i['zinc']) ? 0 : $i['zinc'];
         $i['magnesium'] = empty($i['magnesium']) ? 0 : $i['magnesium'];
         $i['vitamin_c'] = empty($i['vitamin_c']) ? 0 : $i['vitamin_c'];
         $i['fiber_dietary'] = empty($i['fiber_dietary']) ? 0 : $i['fiber_dietary'];
         $i['selenium'] = empty($i['selenium']) ? 0 : $i['selenium'];
         $i['copper'] = empty($i['copper']) ? 0 : $i['copper'];
         $i['carbohydrate'] = empty($i['carbohydrate']) ? 0 : $i['carbohydrate'];
         $i['phosphor'] = empty($i['phosphor']) ? 0 : $i['phosphor'];
         $i['fat'] = empty($i['fat']) ? 0 : $i['fat'];
         $i['niacin'] = empty($i['niacin']) ? 0 : $i['niacin'];
         $i['vitamin_e'] = empty($i['vitamin_e']) ? 0 : $i['vitamin_e'];
         $i['manganese'] = empty($i['manganese']) ? 0 : $i['manganese'];
         $i['cholesterol'] = empty($i['cholesterol']) ? 0 : $i['cholesterol'];
         $i['iron'] = empty($i['iron']) ? 0 : $i['iron'];
         $i['kalium'] = empty($i['kalium']) ? 0 : $i['kalium'];
         $i['natrium'] = empty($i['natrium']) ? 0 : $i['natrium'];
         $i['thiamine'] = empty($i['thiamine']) ? 0 : $i['thiamine'];
         $i['protein'] = empty($i['protein']) ? 0 : $i['protein'];
         $i['lactoflavin'] = empty($i['lactoflavin']) ? 0 : $i['lactoflavin'];
         $i['calory'] = empty($i['calory']) ? 0 : $i['calory'];
         $i['carotene'] = empty($i['carotene']) ? 0 : $i['carotene'];
         $tSql = 'insert into food values("%s","%s",%d,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"%s","%s",%s,"%s","%s")';
         $tSql = sprintf($tSql, '', $tRow['name'], $tTime, 0, $i['calcium'], $i['vitamin_a'], $i['zinc'], $i['magnesium'], $i['vitamin_c'], $i['fiber_dietary'], $i['selenium'], $i['copper'], $i['carbohydrate'], $i['phosphor'], $i['fat'], $i['niacin'], $i['vitamin_e'], $i['manganese'], $i['cholesterol'], $i['iron'], $i['kalium'], $i['natrium'], $i['thiamine'], $i['protein'], $i['lactoflavin'], $i['calory'], $i['carotene'], $tTag, $thumb_image_url, $tRow['cate_id'], $tRow['ctitle'], $large_image_url);
         $tFid = $tFMO->exec($tSql);
         foreach ($l as $tKey => $tVal) {
             if (empty($tVal)) {
                 continue;
             }
             $tData = array('title' => $tKey, 'fid' => $tFid, 'flag' => $tVal);
             $tFTMO->insert($tData);
         }
         $tUnitsArr = json_decode($tRow['units'], true);
         foreach ($tUnitsArr as $tURow) {
             $tData = array('amount' => $tURow['amount'], 'unit' => $tURow['unit'], 'fid' => $tFid, 'calory' => $tURow['calory'], 'weight' => $tURow['weight']);
             $tFUMO->insert($tData);
         }
         echo $ii . "\r\n";
     }
     die;
 }